Repository navigation
Fix dehumanize() silently returning wrong times for fractional inputs - #1406
Open
2025sl93041-gkb wants to merge 1 commit into
Open
2025sl93041-gkb wants to merge 1 commit into
2025sl93041-gkb wants to merge 1 commit into
Conversation
`dehumanize()` used `re.compile(r"\d+")` to extract numbers from the input string. This pattern only matches whole integers, so a decimal like "1.5" was never matched as a unit — each digit ran independently, producing a wildly wrong result (e.g. "1.5 hours ago" shifted by -5 h instead of -1.5 h). Fix: change the number regex to `r"\d+(?:\.\d+)?"` everywhere it is used (the standalone `num_pattern` and the per-timeframe format call), and switch storage and parsing to `float` so that fractional values propagate correctly to `shift()`. Closes arrow-py#1341
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1406 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 10 10
Lines 2315 2315
Branches 358 358
=========================================
Hits 2315 2315 ☔ View full report in Codecov by Harness. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
dehumanize()gives wrong results for any fractional input. For example:Expected:
−1.5 hand−0.5 daysrespectively. No exception is raised — the result is just quietly wrong.Reported in #1341.
Root Cause
The number-matching regex
r"\d+"only matches whole digit sequences. For an input like"1.5 hours ago":re.compile(r"\d+").search("1.5 hours ago")returns"1"(stops at the decimal point).5is then matched on its own as5in the timeframe searchBoth matches write to the same
time_object_info["hours"]key, but the second match overwrites the first with5, giving a shift of-5 hours.Fix
Three changes to
dehumanize()inarrow/arrow.py:num_patternfromr"\d+"tor"\d+(?:\.\d+)?"so the decimal part is captured as one token.search_string.format(r"\d+")to use the same decimal-aware pattern so locale timeframe patterns also match fractional values.time_object_infoinitial values and allint()casts tofloatso the fractional amount is preserved through toshift().Testing
Added
test_fractional_unitstotests/test_arrow.pycovering hours, days, and weeks in both past and future directions. Full suite: 1903 passed, 1 warning.