Repository navigation
Conversation
parse_date() only read numbers, so dates in formats that spell out the
month, which is the default medium format of en_US and most other
locales, raised an IndexError, e.g. parse_date('Nov 24, 2026',
locale='en_US').
Look for the locale's month names, then English ones, when the pattern
has a textual month field. Names must be separate words unless the
pattern attaches letters to the month (Hebrew, Basque, ...), and a match
inside a day name is skipped (in Welsh "Mawrth" is Tuesday and March).
Input with too few numbers now raises ParseError instead of IndexError.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Found a silent regression for numeric patterns containing a quoted month token, on ec8d521: from datetime import date
from babel.dates import format_date, parse_date
pattern = "yyyy-MM-dd 'MMM November'"
text = format_date(date(2026, 11, 24), format=pattern, locale='en_US')
print(text) # 2026-11-24 MMM November
print(parse_date(text, format=pattern, locale='en_US'))
# parent: 2026-11-24
# this head: 2026-11-11The This is a custom-pattern regression, not a failure in the predefined locale formats. Related quoted-field handling is already discussed in #1442 and #1341; this adds a separate activation path to account for. No claim of downstream usage. Checked with AI assistance. The head's native date tests pass (2618 passed, 2 xfailed on Python 3.14). |
parse_date()only reads numbers, so it can't parse dates whose format spells the month out. That includes the defaultmediumformat ofen_USand most other locales, so Babel can't parse its own output:This resolves the FIXME in
parse_date(). When the pattern has a textual month field (MMM/MMMM/LLL/LLLL), it looks for the locale's month names (format and stand-alone, wide and abbreviated), then the English ones, and uses the remaining numbers for the year and day. Numeric patterns go through the same code as before.Some locale details I had to handle:
d בMMM y(he) orMMMM'ren'(eu).Input with too few numbers (e.g. "Nov 2026") now raises
ParseErrorinstead ofIndexError. #1282 fixed theValueErrorcase. #1365 was closed as already done in #1282, but theIndexErrorstill happens on master.To check this, I formatted 7 dates in
short/medium/long/fullfor every locale in CLDR and parsed them back:None of them parse to a wrong date, before or after. The remaining ParseErrors come from two locales:
mgo, whose day names contain a digit ("Aneg 2"), andnnh. Parsing a month-name date takes about 35 µs, because the regexes are cached per locale.Tests: added cases for the locales above, a format/parse round trip for a few locales in all four formats, and the
ParseErrorcases. The full suite passes on Python 3.14, andruff checkis clean.Prepared with AI assistance (Claude Code).
🤖 Generated with Claude Code