Skip to content

fix: Preserve explicit signs for years when combined with decades - #1330

Merged
AdrianAtZyte merged 4 commits into
masterfrom
fix/1304
Jun 8, 2026
Merged

AdrianAtZyte merged 4 commits into
masterfrom
fix/1304

Conversation

@serhii73

@serhii73 serhii73 commented May 7, 2026

Copy link
Copy Markdown
Collaborator

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

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

codecov Bot commented May 7, 2026 •

Copy link
Copy Markdown

Codecov Report

βœ… All modified and coverable lines are covered by tests.
βœ… Project coverage is 97.11%. Comparing base (373ede9) to head (9f0bcf8).
⚠️ Report is 10 commits behind head on master.

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.
πŸ“’ 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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 the decades explicit-sign flag to years when years does 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.

@serhii73
serhii73 requested review from AdrianAtZyte and wRAR May 7, 2026 10:23
Comment thread tests/test_freshness_date_parser.py Resolved
Comment thread tests/test_freshness_date_parser.py Resolved
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>
@AdrianAtZyte
AdrianAtZyte merged commit 5c6d97f into master Jun 8, 2026
18 checks passed
@serhii73
serhii73 deleted the fix/1304 branch June 8, 2026 09:58
@serhii73 serhii73 mentioned this pull request Jun 15, 2026
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.

Explicit signs lost when combining decades and years in freshness date parser

4 participants