Skip to content

Fix parse_date and parse_time to raise ParseError instead of ValueError - #1282

Merged
akx merged 3 commits into
python-babel:masterfrom
binggao1230:fix-parse-date-time-value-error
Nov 4, 2026
Merged

akx merged 3 commits into
python-babel:masterfrom
binggao1230:fix-parse-date-time-value-error

Conversation

@binggao1230

Copy link
Copy Markdown
Contributor

Problem

parse_date and parse_time both catch ParseError as their documented error type, but when the parsed numbers produce an out-of-range date or time value, the internal datetime.date / datetime.time constructor raises a raw ValueError that escapes to the caller uncaught.

Reported in #1178. Example:

>>> from babel.dates import parse_date
>>> parse_date('2005-12-31T01:02:03Z')
# Before: ValueError: day is out of range for month
# After: ParseError: String '2005-12-31T01:02:03Z' does not match format 'MMM d, y' (day is out of range for month)
>>> from babel.dates import parse_time
>>> parse_time('25:00')
# Before: ValueError: hour must be in 0..23
# After: ParseError: String '25:00' does not match format 'h:mm:ss a' (hour must be in 0..23)

Fix

Wrap the final datetime.date(year, month, day) call in parse_date and datetime.time(hour, minute, second) call in parse_time with try/except ValueError, re-raising as ParseError with an informative message that includes the input string and the expected format pattern.

All 2245 existing tests pass (2 xfailed unchanged).

Fixes #1178.

@akx akx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This would need a test.

Also, I think it would be useful to raise from the ValueError, not None.

And... maybe the message needs some work? The exception is, after all, that the numbers we parsed out of the string don't represent a Gregorian date.

@binggao1230

Copy link
Copy Markdown
Contributor Author

All three applied in 0e92abc:

  • both errors now chain the underlying ValueError (from e) instead of from None
  • the message says the parsed numbers do not represent a valid Gregorian date/time
  • test_parse_date_out_of_range and test_parse_time_out_of_range cover the new path (day 30 in February, hour 25); 2279 pass.

@akx akx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you!

When parse_date or parse_time receives an input string whose numbers
produce an out-of-range date or time (e.g. month 13, hour 25), the
final datetime.date / datetime.time constructor raises a raw ValueError.
Callers that catch ParseError (the documented error type for this module)
never see the exception.

Wrap both constructors in try/except and re-raise as ParseError with a
message that includes the input string and the expected format pattern.

Fixes python-babel#1178.
@akx
akx force-pushed the fix-parse-date-time-value-error branch from 0e92abc to af9656f Compare November 4, 2026 09:11
@akx
akx enabled auto-merge (squash) November 4, 2026 09:11
@codspeed

codspeed Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 88 untouched benchmarks


Comparing binggao1230:fix-parse-date-time-value-error (54a67f3) with master (853db81)

Open in CodSpeed

@codecov

codecov Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.63%. Comparing base (853db81) to head (54a67f3).
⚠️ Report is 5 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1282   +/-   ##
=======================================
  Coverage   93.62%   93.63%           
=======================================
  Files          27       27           
  Lines        4959     4965    +6     
=======================================
+ Hits         4643     4649    +6     
  Misses        316      316           
Flag Coverage Δ
macos-14-3.10 92.66% <100.00%> (+<0.01%) ⬆️
macos-14-3.11 92.60% <100.00%> (+<0.01%) ⬆️
macos-14-3.12 92.87% <100.00%> (+<0.01%) ⬆️
macos-14-3.13 92.87% <100.00%> (+<0.01%) ⬆️
macos-14-3.14 92.84% <100.00%> (+<0.01%) ⬆️
macos-14-3.15 92.84% <100.00%> (+<0.01%) ⬆️
macos-14-3.8 92.54% <100.00%> (+<0.01%) ⬆️
macos-14-3.9 92.60% <100.00%> (+<0.01%) ⬆️
macos-14-pypy3.10 92.66% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.10 92.68% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.11 92.62% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.12 92.89% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.13 92.89% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.14 92.87% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.15 92.87% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.8 92.56% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-3.9 92.62% <100.00%> (+<0.01%) ⬆️
ubuntu-24.04-pypy3.10 92.68% <100.00%> (+<0.01%) ⬆️
windows-2022-3.10 92.68% <100.00%> (+<0.01%) ⬆️
windows-2022-3.11 92.62% <100.00%> (+<0.01%) ⬆️
windows-2022-3.12 92.88% <100.00%> (+<0.01%) ⬆️
windows-2022-3.13 92.88% <100.00%> (+<0.01%) ⬆️
windows-2022-3.14 92.86% <100.00%> (+<0.01%) ⬆️
windows-2022-3.15 92.86% <100.00%> (+<0.01%) ⬆️
windows-2022-3.8 92.65% <100.00%> (+<0.01%) ⬆️
windows-2022-3.9 92.61% <100.00%> (+<0.01%) ⬆️
windows-2022-pypy3.10 92.68% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@akx
akx merged commit 344a3b4 into python-babel:master Nov 4, 2026
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

parse_date raises ValueError: day is out of range for month for iso8601 string

2 participants