Skip to content

Accumulate repeated units in parse_timedelta - #583

Open
shreyans-chowdry wants to merge 1 commit into
mahmoud:masterfrom
shreyans-chowdry:fix-repeated-duration-units
Open

shreyans-chowdry wants to merge 1 commit into
mahmoud:masterfrom
shreyans-chowdry:fix-repeated-duration-units

Conversation

@shreyans-chowdry

@shreyans-chowdry shreyans-chowdry commented Oct 16, 2026 •

Copy link
Copy Markdown

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 -q passes 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

  • Harness/tooling: OpenAI Codex desktop (source investigation, patch, tests, and submission).
  • Model: GPT-6, as identified by the agent's runtime instructions.
  • Original prompt: “can u research for some of the popular repos and first take the top 5 out of them and then one by one analyze all the repos and make an open source contribution on my behalf from my github.com/shreyans-chowdry (make sure that the changes you are suggesting are good and the maintainer/owner of the repo might consider for the pr merging)”

@Wkhanex Wkhanex left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
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.

2 participants