Skip to content

Raise ValueError for negative numbers in dehumanize()#1302

Open
awath-abdat wants to merge 1 commit into
arrow-py:masterfrom
awath-abdat:fix-dehumanize-negative-values
Open

Raise ValueError for negative numbers in dehumanize()#1302
awath-abdat wants to merge 1 commit into
arrow-py:masterfrom
awath-abdat:fix-dehumanize-negative-values

Conversation

@awath-abdat

Copy link
Copy Markdown

Pull Request Checklist

  • Added tests for changed code.
  • All tests pass when run locally.
  • All linting checks pass when run locally.
  • Updated documentation for changed code.
  • Code is up-to-date with the master branch.

Description of Changes

Closes: #1278

dehumanize() silently dropped the minus sign from inputs like "in -1 hours" because the number-extraction regex (\d+) only matches unsigned digits. The result was that "in -1 hours" produced the same output as "in 1 hours" — a quietly wrong datetime in the opposite direction from what a user would expect.

Since humanized time strings express direction via "ago"/"in" rather than arithmetic signs, negative numbers are never valid input. This PR adds an early validation check that raises ValueError when the input contains a negative number (e.g. -1, - 2).

Added test_negative_numbers covering three variants: "in -1 hours", "-3 minutes ago", and "in -2 days". All 23 dehumanize tests pass.

dehumanize() silently dropped the minus sign from inputs like
"in -1 hours" because the number-extraction regex (\d+) only
matches unsigned digits. This produced quietly wrong results.
@codecov

codecov Bot commented Jun 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (2224255) to head (c0cd576).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #1302   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           10        10           
  Lines         2315      2317    +2     
  Branches       358       359    +1     
=========================================
+ Hits          2315      2317    +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dehumanize() silently ignores negative time values, returning wrong results

1 participant