Skip to content

Fixes #814 - #818

Open
tony1233200 wants to merge 1 commit into
mahmoud:masterfrom
tony1233200:fix/814-parse-timedelta-compact
Open

tony1233200 wants to merge 1 commit into
mahmoud:masterfrom
tony1233200:fix/814-parse-timedelta-compact

Conversation

@tony1233200

Copy link
Copy Markdown

The change

parse_timedelta("1h30m") used to return exactly one hour: the trailing
\w* in _PARSE_TD_RE was meant to swallow the rest of a spelled-out
unit word (hours, minutes, …), but \w also matches digits, so on
"1h30m" it consumed "30m" as though it were part of the unit name
h, leaving nothing for finditer to find as a second match. Every
unit after the first in a compact string was silently discarded.

This PR replaces \w* with [a-zA-Z]* in boltons/timeutils.py, so
only letters can follow the unit's first letter and the next numeric
value starts a new match. Spelled-out units ("1 hour 30 minutes")
and space-separated forms ("1h 30m") behave exactly as before.

Adds test_parse_timedelta_compact_multi_unit covering the six
previously wrong inputs from the issue ("1h30m", "1h30m15s",
"2d4h", "1m30s", "1w2d", "3d12h") plus the spelled-out and
space-separated controls.

The backstory

I ran into this while parsing compact duration strings (timeouts and
TTLs) out of config files: "1h30m" came back as one hour with no
error, so a cache TTL expired 30 minutes early and nothing in the log
explained why. The issue's table matched what I was seeing exactly,
so I traced it to the regex and fixed it at the source rather than
working around it at the call site.

AI assistance

  • Harness/tooling: Muse (Meta AI agent)
  • Model(s): Muse Spark
  • Original prompt(s), as close to verbatim as I can manage: "You are a patch lane for the 100-PR scouting round. Prepare a patch (do not open the PR) for mahmoud/boltons parse_timedelta silently drops every unit after the first in compact strings like "1h30m" #814: parse_timedelta("1h30m") silently parses only the first unit. Read the full issue first, clone with --depth 1, pass the quality gate — minimal repro (fails without the fix, passes with it), full related tests plus lint, follow the repo's CONTRIBUTING/AGENTS.md — and deliver a patch file, PR body, and repro script."

Verification

  • Repro script (repro_814.py, 14 cases + garbage check): 6/15 failed
    before the fix ("1h30m" -> 1:00:00, "2d4h" -> 2 days, 0:00:00,
    …), 0/15 failed after.
  • New test fails on the unfixed regex and passes with the fix
    (verified by stashing the one-line change and re-running).
  • pytest --doctest-modules boltons/timeutils.py tests/test_timeutils.py: 25 passed
  • Full suite pytest --doctest-modules boltons tests: 776 passed, 22 subtests passed
  • No lint config ships in this repo (no ruff/flake8/black in
    pyproject.toml, tox.ini, or CI workflows); py_compile clean on both
    changed files. CI runs pytest via tox.

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.

1 participant