Repository navigation
Accumulate repeated units in parse_timedelta - #583
shreyans-chowdry wants to merge 1 commit into
Conversation
Wkhanex
left a comment
There was a problem hiding this comment.
Verified 267175d against datetime.timedelta arithmetic for all 25 pairs from {-2, -0.5, 0, 0.5, 3}, across weeks/days/hours/minutes/seconds. Each duration was expressed once with a short unit and once with its long alias, then checked in both orders. These 250 cases and a mixed-unit cancellation case all matched the independently added timedeltas.
This checks repeated aliases, fractional negatives, cancellation and order independence, beyond the five examples in the PR. All 251 assertions and the five focused repository tests pass on CPython 3.12.14; the base overwrites the first occurrence in the repeated-week case. Full-suite and other-runtime checks were not run.
Review prepared with Codex assistance; the checks described above were executed locally.
The change
parse_timedelta('1h 30m 2h')currently returns 2 hours and 30 minutes because the second hours component overwrites the first. Accumulate components for the same unit so the result is 3 hours and 30 minutes. This also handles repeated long-form units, fractions, and cancellation (2s -2s).The change is one assignment plus a docstring example and parameterized regression tests. Four new cases fail before the fix. After it,
python -m pytest --doctest-modules boltons tests -qpasses all 683 tests on Python 3.12.4/macOS.The backstory
This contribution comes from Shreyans Chowdry's request to research popular repositories and contribute useful fixes with a reasonable chance of being accepted. Codex found this behavior while examining Boltons; it was not reported from a production application, and no prior Boltons usage or independent human review is claimed. Boltons was selected for its self-contained utilities and testable contracts.
AI assistance