Description
Summary
pendulum.from_format cannot parse any of the ordinal tokens, raising a raw AttributeError instead of a ValueError — and for Do the crash is locale-dependent:
1. Do in locales without ordinal suffixes (bg, de, ru, pl, uk, da, sv, ...) — the crash comes from Formatter._LOCALIZABLE_TOKENS:
>>> pendulum.datetime(1982, 6, 7).format("Do MMMM YYYY", locale="de")
'7 Juni 1982' # format() is fine
>>> pendulum.from_format("7 Juni 1982", "Do MMMM YYYY", locale="de")
AttributeError: 'NoneType' object has no attribute 'values'Locale.get("custom.ordinal") returns None for these locales and the Do entry does locale.get("custom.ordinal").values(). Note Locale.ordinalize() (used by format()) already falls back to the plain number when the locale has no ordinals, so the parse pattern should accept plain digits — exactly what format() emits.
2. Qo, Mo, DDDo, do, e, eo, Wo, wo in every locale — these tokens are None in _LOCALIZABLE_TOKENS, so _replace_tokens does locale.translation(None).values():
>>> pendulum.datetime(1982, 6, 7).format("Qo YYYY", locale="en")
'2nd 1982'
>>> pendulum.from_format("2nd 1982", "Qo YYYY", locale="en")
AttributeError: 'NoneType' object has no attribute 'values'The AttributeError leaks from a public API instead of the regular ValueError("Unsupported token: ...") that _replace_tokens is designed to raise for tokens without candidates.
Root cause
src/pendulum/formatting/formatter.py:
- The
Dolambda calls.values()onlocale.get("custom.ordinal")without aNoneguard (line ~79-81). - The
Noneentries for the other ordinal tokens are passed tolocale.translation(None)which returnsNone(lines ~70-77), and.values()crashes. The intendedValueError(f"Unsupported token: {token}")guard at the bottom of_replace_tokensis never reached.
Expected behavior
from_formatmust accept whateverformat()emits: forDoin a locale without ordinals that means plain digits (\d+); for locales with ordinals the existing\d+<suffix>patterns.- Tokens that are deliberately not parse-supported should raise the existing
ValueError("Unsupported token: X"), notAttributeError. Mo,QoandDDDoare straightforwardly parseable (month / quarter / day of year —_check_parsedalready consumes all three fields), so they get real support, givingformat/from_formatround-trips for them.
Environment
- pendulum 3.1.0 (current
master, e06dace), Python 3.13, Linux.
Activity