Honor REQUIRE_PARTS for ambiguous month-number inputs by retrying year-biased DATE_ORDER#1298
Conversation
…ear-biased `DATE_ORDER`
There was a problem hiding this comment.
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_ORDERvalues (MYD,YMD) whenREQUIRE_PARTSincludes"year"but not"day"andDATE_ORDERwas not explicitly set - Restructures
_try_parsermethod to iterate through multipleDATE_ORDERcandidates and handle parse failures gracefully - Adds comprehensive test coverage for the new behavior, including edge cases for explicit
DATE_ORDERand 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 Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1298 +/- ##
==========================================
+ Coverage 96.60% 96.63% +0.03%
==========================================
Files 235 235
Lines 2889 2915 +26
==========================================
+ Hits 2791 2817 +26
Misses 98 98 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Attempt to fix #1235.
Problem
When parsing inputs like
Oct-23withsettings={"REQUIRE_PARTS": ["month", "year"]},dateparser.parsecan returnNoneeven though a valid interpretation exists (October 2023). The failure occurs because the initialDATE_ORDER(often locale-derived or the default) may interpret the trailing number as a day rather than a year, causingREQUIRE_PARTSvalidation to reject the result.Fix
When all of the following are true:
REQUIRE_PARTSincludes"year"and does not require"day",DATE_ORDER,We retry parsing the same translated string with year-biased
DATE_ORDERcandidates (MYD, thenYMD) after the initial attempt. This allows ambiguousMon-23style inputs to resolve to month–year when that is the only way to satisfy the requested parts.Scope / compatibility
REQUIRE_PARTS.DATE_ORDERis explicitly set by the caller.REQUIRE_PARTSrequiresday(month–day remains valid).