Repository navigation
fix: Preserve explicit signs for years when combined with decades - #1330
Conversation
When both decades and years are present (e.g. '-1 decade +2 years'),
the code in get_kwargs() unconditionally overwrote explicit_signs['years']
with the decades sign value:
if 'decades' in explicit_signs:
explicit_signs['years'] = explicit_signs['decades']
This destroyed the user-supplied +/- that was recorded for years during
the PATTERN match loop, causing _parse_date() to mishandle the sign of
the merged years value.
Fix: only propagate the decades sign to years when years did not carry
its own explicit sign (explicit_signs.get('years') is False/absent).
Regression tests added for:
- '-1 decade' (only decades, negative)
- '-1 decade 2 years' (decades negative, years implicit)
- '-1 decade +2 years' (decades negative, years explicitly positive)
- '+2 years' (only years, explicitly positive future)
- '1 decade +2 years' (decades implicit, years explicitly positive)
- '+1 decade -2 years' (decades explicitly positive, years explicitly negative)
Fixes #1304
Codecov Reportβ
All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1330 +/- ##
=======================================
Coverage 97.10% 97.11%
=======================================
Files 235 235
Lines 2904 2909 +5
=======================================
+ Hits 2820 2825 +5
Misses 84 84 β View full report in Codecov by Harness. π New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Fixes a sign-handling bug in the freshness/relative-date parser where combining decades and years could overwrite an explicitly provided sign on years (e.g. -1 decade +2 years), leading to incorrect direction when computing the final relative delta.
Changes:
- Update
FreshnessDateDataParser.get_kwargs()to only propagate thedecadesexplicit-sign flag toyearswhenyearsdoes not already have its own explicit sign. - Add regression tests covering mixed-sign and explicit-sign combinations for decades+years (and standalone explicitly-signed years).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
dateparser/freshness_date_parser.py |
Preserves an explicit +/- on years when merging decades into years, preventing incorrect sign inversion. |
tests/test_freshness_date_parser.py |
Adds regression coverage for the decades+years explicit-sign interaction, including mixed-sign cases. |
π‘ Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
AdrianAtZyte and wRAR flagged three of the #1304 regression cases as relying on an ambiguous reading of mixed implicit ago/future direction and explicit per-component signs: - "-1 decade -2 years" - "1 decade +2 years" - "1 decade -2 years" For example, "1 decade -2 years ago" can reasonably mean either a decade *less* two years (8 years ago) or a decade *plus* two more years (12). These expressions are uncommon and shouldn't be locked into regression tests, since their result depends on interpretation rather than on the behaviour #1304 actually fixes. The retained cases still fully cover the bug β that an explicit sign on a component is preserved when ``decades`` is folded into ``years`` rather than being overwritten by the decade's sign: - "-1 decade" single explicit sign - "-1 decade 2 years" explicit decade + unsigned year (follows ago) - "-1 decade +2 years" explicit signs disagree (the bug condition) - "+2 years" single explicit sign - "in 1 decade +2 years" disambiguated future form - "+1 decade -2 years" explicit signs disagree (the bug condition) The two "explicit signs disagree" cases are the strongest checks: if the decade sign overwrote the year sign, the result would flip. Implementation is unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
When both decades and years are present (e.g. '-1 decade +2 years'), the code in get_kwargs() unconditionally overwrote explicit_signs['years'] with the decades sign value:
This destroyed the user-supplied +/- that was recorded for years during the PATTERN match loop, causing _parse_date() to mishandle the sign of the merged years value.
Fix: only propagate the decades sign to years when years did not carry its own explicit sign (explicit_signs.get('years') is False/absent).
Regression tests added for:
Fixes #1304