Skip to content

Honor PREFER_DATES_FROM for a date_formats entry with no year - #1350

Open
chuenchen309 wants to merge 1 commit into
scrapinghub:masterfrom
chuenchen309:fix/prefer-dates-from-yearless-formats
Open

Honor PREFER_DATES_FROM for a date_formats entry with no year#1350
chuenchen309 wants to merge 1 commit into
scrapinghub:masterfrom
chuenchen309:fix/prefer-dates-from-yearless-formats

Conversation

@chuenchen309

Copy link
Copy Markdown

parse_with_formats applied PREFER_DATES_FROM only to a two-digit-year format
(through _apply_century_preference, added in #1342). A date_formats entry
with no year directive at all took the current year unconditionally:

import dateparser
from datetime import datetime
RB = datetime(2026, 6, 15)

dateparser.parse("December 25", date_formats=["%B %d"],
                 settings={"RELATIVE_BASE": RB, "PREFER_DATES_FROM": "past"})
# -> 2026-12-25   (a future date, under PREFER_DATES_FROM="past")

The result contradicts the setting, and disagrees with the NLP path, which
parses the same string+settings correctly to 2025-12-25. docs/settings.rst
states PREFER_DATES_FROM applies "if date string is missing some part" (its
example omits the year), with no carve-out for date_formats. This is the
other half of #1342 — that PR wired the setting into the adjacent two-digit-year
branch but left the year-less branch.

Fix: generalise the #1342 helper to shift by ±N years and call it with 1
for the year-less branch (100 stays for the two-digit branch), so both share
the same direction logic and Feb-29 leap-year handling. Added a regression test.


This PR was authored by an AI coding agent (Claude Code) running on this account:
the AI found the bug, ran the repro, wrote the test, and wrote this description.
The human account holder reviews every change and is accountable for it. The
verification is real and re-runnable from the diff. If this isn't the kind of
contribution you want, say so and I'll close it.

parse_with_formats applied PREFER_DATES_FROM only to a two-digit-year format
(via _apply_century_preference, added in scrapinghub#1342). A format with no year
directive at all just took the current year unconditionally, so
parse("December 25", date_formats=["%B %d"], settings={"PREFER_DATES_FROM":
"past"}) returned a future date -- contradicting the setting, and disagreeing
with the NLP path, which parses the same string correctly.

Generalise the helper to shift by ±N years and call it with 1 for the
year-less branch (100 stays for the two-digit branch), so both share the same
direction and Feb-29 leap-year handling.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: chuenchen309 <48723787+chuenchen309@users.noreply.github.com>
@AdrianAtZyte

Copy link
Copy Markdown
Contributor

Ask your human to explain the fix to me in their words.

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