Skip to content

fix: naturaldelta includes months when years > 1 and months=True - #350

Open
amitmishra11 wants to merge 2 commits into
python-humanize:mainfrom
amitmishra11:fix/naturaldelta-months-multiyear
Open

fix: naturaldelta includes months when years > 1 and months=True#350
amitmishra11 wants to merge 2 commits into
python-humanize:mainfrom
amitmishra11:fix/naturaldelta-months-multiyear

Conversation

@amitmishra11

Copy link
Copy Markdown

Fixes#108

Changes proposed in this pull request:

  • The final branch of naturaldelta (years > 1) now respects months=True by appending the month count when 0 < num_months < 12, and rolls over to the next year when num_months == 12 — consistent with the years == 1 branch.
  • Updated existing test expectations that encoded the old (incorrect) behavior of ignoring months for multi-year deltas.

Summary

naturaldelta(timedelta(days=int(4.6*365)), months=True) returned "4 years" instead of "4 years, 7 months". A # TODO comment in the source already identified this gap.

Changes

  • src/humanize/time.py: resolve the TODO at the end of naturaldelta; handle num_months == 12 rollover for the years > 1 branch
  • tests/test_time.py: update stale expectations to reflect the corrected output (e.g. "2 years" -> "2 years, 1 month", "27 years" -> "27 years, 5 months")

Testing

pytest tests/test_time.py -q # 383 passed

Fixes#108

Amit Mishraand others added 2 commits July 11, 2026 11:24
The final branch of naturaldelta (years > 1) always returned a bare
year count even when months=True. A TODO comment in the code already
identified this gap. Now, when use_months is True and the remaining
days round to 1-11 months, the output includes the month count
('4 years, 7 months'). When they round to 12 months the year count
is incremented instead, consistent with the years == 1 branch.
Test expectations updated to reflect the corrected output.
Fixespython-humanize#108
@codecov

codecovBot commented Jul 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 71.42857% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.34%. Comparing base (c3a124c) to head (98b061a).

Files with missing linesPatch %Lines
src/humanize/time.py71.42%2 Missing ⚠️
Additional details and impacted files
@@ Coverage Diff @@## main #350 +/- ##
==========================================
- Coverage 99.56% 99.34% -0.22% 
==========================================
Files 12 12 Lines 913 920 +7 ==========================================
+ Hits 909 914 +5 - Misses 4 6 +2 
FlagCoverage Δ
macos-latest97.39% <71.42%> (-0.20%)⬇️
ubuntu-latest97.39% <71.42%> (-0.20%)⬇️
windows-latest95.54% <71.42%> (-0.19%)⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed-hq

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 15 untouched benchmarks


Comparing amitmishra11:fix/naturaldelta-months-multiyear (98b061a) with main (c3a124c)

Open in CodSpeed

@amitmishra11

Copy link
Copy Markdown
Author

Can anyone from the team review this PR please @EdwardBetts@joemaller@bbolli@aborruso

Sign up for freeto 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.

Missing option to display years and months when years > 1

1 participant

@amitmishra11