Repository navigation
Conversation
parse_date indexes the numbers extracted from the input by the
positions of y/m/d in the locale pattern, but never checks how many
numbers were actually found. Strings with fewer than three numbers -
including month-name dates like 'Jan 3, 2026' (en_US medium format)
or '1. Januar 2026' - crash with a bare IndexError:
>>> parse_date('Jan 3, 2026', locale='en_US')
IndexError: list index out of range
Out-of-range fields likewise leak datetime.date()'s ValueError, which
is not the documented ParseError, so 'except ParseError' handlers miss
it:
>>> parse_date('01.32.2026', locale='de_DE')
ValueError: day 32 must be in range 1..31 for month 1 in year 2026
Raise ParseError for both cases, matching the existing
'No numbers were found in input' behaviour. parse_time already guards
its indexing (len(numbers) > 1 / > 2).
This branch has not been deployed
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.
Summary
parse_dateindexes the numbers extracted from the input string by the positions ofy/m/din the locale's date pattern, but never checks how many numbers were actually found. Input with fewer than three numbers — including month-name dates like the locale's own medium/long formats — crashes with a bareIndexError:Out-of-range fields likewise leak
datetime.date()'sValueError, whichexcept ParseErrorhandlers do not catch:This PR raises
ParseErrorfor both cases, matching the existingNo numbers were found in inputbehavior.parse_timealready guards its indexing (len(numbers) > 1/> 2), so this brings the two parsers in line. (Month-name support remains a separate, already-acknowledged FIXME; this only fixes the error contract.)Verification
test_parse_date_raises_parse_error_instead_of_index_errorandtest_parse_date_raises_parse_error_on_out_of_range_valuesfail on master (IndexError/ValueErrorraised instead ofParseError) and pass with this change.pytest tests/test_dates.py: 1193 passed with this change vs 1191 on master against the same environment (5 pre-existing failures identical on master — CLDR data drift in my local setup; no regressions).ruff checkclean.AI Disclosure
This PR was prepared with the assistance of an AI coding agent.
ZCode (GLM-based coding agent)