Repository navigation
fix: Preserve explicit signs for years when combined with decades - #1330
Merged
Merged
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:
|
Contributor
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.
wRAR
approved these changes
May 8, 2026
AdrianAtZyte
reviewed
Jun 4, 2026
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) <[email protected]>
AdrianAtZyte
approved these changes
Jun 8, 2026
wRAR
approved these changes
Jun 8, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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