Skip to content

fix: parse ordinal tokens in from_format, raise ValueError for the rest - #1089

Open
rahul05ranjan wants to merge 1 commit into
python-pendulum:masterfrom
rahul05ranjan:fix/from-format-ordinal-tokens
Open

rahul05ranjan wants to merge 1 commit into
python-pendulum:masterfrom
rahul05ranjan:fix/from-format-ordinal-tokens

Conversation

@rahul05ranjan

Copy link
Copy Markdown

from_format crashed with a raw AttributeError on every ordinal token, instead of either parsing or raising the regular ValueError("Unsupported token: ..."):

>>> pendulum.from_format("7 Juni 1982", "Do MMMM YYYY", locale="de")
AttributeError: 'NoneType' object has no attribute 'values'   # de has no ordinal suffixes
>>> pendulum.from_format("2nd 1982", "Qo YYYY", locale="en")
AttributeError: 'NoneType' object has no attribute 'values'   # Qo/Mo/DDDo are None in _LOCALIZABLE_TOKENS

This makes from_format accept what format() emits:

  • Do in a locale without a custom.ordinal table (de, bg, ru, pl, uk, …) accepts plain digits — exactly what format() emits there, since Locale.ordinalize falls back to the plain number.
  • Mo, Qo and DDDo get real parsing: the \d+<suffix> candidates built from the locale's ordinal table, populating month / quarter / day-of-year — all three fields are already consumed downstream.
>>> pendulum.from_format("7 Juni 1982", "Do MMMM YYYY", locale="de")
1982-06-07 00:00:00+00:00
>>> pendulum.from_format("2nd 1982", "Qo YYYY", locale="en").quarter
2
>>> dt.format("DDDo YYYY")    # round-trips
'158th 1982'
  • The tokens that are deliberately not parseable (do, e, eo, Wo, wo) now raise ValueError: Unsupported token: … — the exception _replace_tokens was designed to raise — instead of AttributeError leaking from locale.translation(None).values().

Fixes #1074.

Verification (system Python 3.13; python-dateutil/tzdata/time-machine wheels on the path, nothing installed):

  • 9 new tests in tests/datetime/test_from_format.py (ordinal round-trips across locales, the plain-digit Do fallback, parametrized ValueError for the five unsupported tokens): all fail on master@e06dace, all pass with this change.
  • tests/datetime, tests/formatting, tests/parsing, tests/date, tests/time, tests/interval: 1102 passed with the fix; tests/test_main.py has one pre-existing date-sensitive failure (Europe/Paris offset), confirmed identical on unpatched master.

from_format crashed with a raw AttributeError on every ordinal token:
the None entries for do/e/eo/Wo/wo in _LOCALIZABLE_TOKENS were passed
to locale.translation(None), and the Do lambda called .values() on the
None custom.ordinal table of locales like de, bg or ru.

- Mo, Qo and DDDo now build the same \d+<suffix> candidates format()
  emits and populate month/quarter/day_of_year, so format()/from_format()
  round-trips work for them.
- Do falls back to plain digits in locales without ordinal suffixes,
  exactly what format() (via Locale.ordinalize) emits there.
- The deliberately unsupported ordinal tokens raise the intended
ValueError('Unsupported token: ...') instead of AttributeError.

Fixes python-pendulum#1074

Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

from_format raises AttributeError for ordinal tokens (Do in locales without ordinals; Qo/Mo/DDDo/e/eo/do/Wo/wo everywhere)

1 participant