Repository navigation
fix: parse ordinal tokens in from_format, raise ValueError for the rest - #1089
Open
rahul05ranjan wants to merge 1 commit into
Open
rahul05ranjan wants to merge 1 commit into
rahul05ranjan wants to merge 1 commit into
Conversation
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
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.
from_formatcrashed with a rawAttributeErroron every ordinal token, instead of either parsing or raising the regularValueError("Unsupported token: ..."):This makes
from_formataccept whatformat()emits:Doin a locale without acustom.ordinaltable (de, bg, ru, pl, uk, …) accepts plain digits — exactly whatformat()emits there, sinceLocale.ordinalizefalls back to the plain number.Mo,QoandDDDoget 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.do,e,eo,Wo,wo) now raiseValueError: Unsupported token: …— the exception_replace_tokenswas designed to raise — instead ofAttributeErrorleaking fromlocale.translation(None).values().Fixes #1074.
Verification (system Python 3.13;
python-dateutil/tzdata/time-machinewheels on the path, nothing installed):tests/datetime/test_from_format.py(ordinal round-trips across locales, the plain-digitDofallback, parametrizedValueErrorfor the five unsupported tokens): all fail onmaster@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.pyhas one pre-existing date-sensitive failure (Europe/Parisoffset), confirmed identical on unpatchedmaster.