Skip to content

Conversation

@MrinalJain17
Copy link

Attempt to fix #1235.

Problem

When parsing inputs like Oct-23 with settings={"REQUIRE_PARTS": ["month", "year"]}, dateparser.parse can return None even though a valid interpretation exists (October 2023). The failure occurs because the initial DATE_ORDER (often locale-derived or the default) may interpret the trailing number as a day rather than a year, causing REQUIRE_PARTS validation to reject the result.

Fix

When all of the following are true:

  • REQUIRE_PARTS includes "year" and does not require "day",
  • the caller did not explicitly set DATE_ORDER,

We retry parsing the same translated string with year-biased DATE_ORDER candidates (MYD, then YMD) after the initial attempt. This allows ambiguous Mon-23 style inputs to resolve to month–year when that is the only way to satisfy the requested parts.

Scope / compatibility

  • No behavior change for callers that do not use REQUIRE_PARTS.
  • No behavior change when DATE_ORDER is explicitly set by the caller.
  • No behavior change when REQUIRE_PARTS requires day (month–day remains valid).

@Gallaecio Gallaecio requested a review from Copilot January 9, 2026 08:41
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes an issue where dateparser.parse returns None for ambiguous month-number inputs like "Oct-23" when REQUIRE_PARTS specifies ["month", "year"]. The fix introduces a retry mechanism with year-biased DATE_ORDER candidates when the initial parse fails to satisfy the required parts constraint.

Key changes:

  • Adds retry logic with year-biased DATE_ORDER values (MYD, YMD) when REQUIRE_PARTS includes "year" but not "day" and DATE_ORDER was not explicitly set
  • Restructures _try_parser method to iterate through multiple DATE_ORDER candidates and handle parse failures gracefully
  • Adds comprehensive test coverage for the new behavior, including edge cases for explicit DATE_ORDER and month-day requirements

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
tests/test_clean_api.py Adds three test cases covering month-year parsing with REQUIRE_PARTS, explicit DATE_ORDER preservation, and month-day requirement scenarios
dateparser/date.py Refactors _try_parser to support multiple DATE_ORDER candidates with proper retry logic and state restoration

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov
Copy link

codecov bot commented Jan 9, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.61%. Comparing base (920d725) to head (3fa3bf1).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1298      +/-   ##
==========================================
+ Coverage   96.60%   96.61%   +0.01%     
==========================================
  Files         235      235              
  Lines        2889     2899      +10     
==========================================
+ Hits         2791     2801      +10     
  Misses         98       98              

☔ View full report in Codecov by Sentry.
📢 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.

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.

Inconsistent parsing of a date like "Oct-23"

1 participant