Skip to content

Fix naturaltime overflow for large durations - #456

Open
blouirts123 wants to merge 1 commit into
python-humanize:mainfrom
blouirts123:fix/naturaltime-large-durations
Open

blouirts123 wants to merge 1 commit into
python-humanize:mainfrom
blouirts123:fix/naturaltime-large-durations

Conversation

@blouirts123

Copy link
Copy Markdown

naturaltime(timedelta(days=4_000_000)) raises OverflowError: date value out of range even though the duration is valid. The function constructs a calendar date that it does not need to format timedelta or numeric-seconds inputs.

Format those inputs directly, preserving timedelta sign, numeric rounding and the future argument. Add regressions for signed large durations, timedelta bounds and numeric future overrides. This concerns naturaltime; the adjacent #448 addresses precisedelta.

Validation: 751 tests passed, 112 translation tests skipped on Windows Python 3.13.14 using a proper editable install; full mypy and changed-file hooks pass. Independent review and 176 focused naturaltime tests pass. The all-files hook run has a local cached validate-pyproject missing-schema error; the same pinned validator installed directly validates pyproject.toml successfully.

@blouirts123
blouirts123 marked this pull request as draft October 16, 2026 04:50
@blouirts123
blouirts123 marked this pull request as ready for review October 16, 2026 05:23
@carlitogwapito

Copy link
Copy Markdown

Even a one-second duration can hit this calendar-arithmetic problem when when is at a datetime endpoint. I added a 25-line test-only companion on this branch, with no implementation changes.

Two useful guards: small durations should still format with datetime.min and datetime.max references, and the default tense should stay different for negative timedeltas versus numeric seconds. The eight cases cover both references and four signed input forms, without a frozen clock.

I ran the unchanged cases against the complete time, number, and i18n source modules on Windows/Python 3.14.4. All eight pass with this branch. On the base, four raise the expected OverflowError in _date_and_delta; the other four pass as controls.

This is component verification, not a package-build result: I used an import namespace to avoid inventing the Git snapshot's missing generated _version.py. The package root initializer, full suite, translations, aware-datetime paths, prek/mypy and version matrix weren't run. The offered file passes the repo-configured Ruff lint/format checks. From a properly installed checkout with the file added, the command is pytest -q tests/test_naturaltime_reference_bounds.py. Take whichever cases help.

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