Repository navigation
Fix from_format AttributeError for ordinal tokens - #1075
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 instead of a ValueError
for every ordinal token: 'Do' in locales without ordinal suffixes
(bg, de, ru, ...), because the Do pattern builder called .values() on
locale.get('custom.ordinal') which is None there; and Qo/Mo/DDDo/do/
e/eo/Wo/wo in every locale, whose None entries went to
locale.translation(None).values().
- Do now falls back to plain \d+ for locales without ordinals,
mirroring Locale.ordinalize(), so format/from_format round-trips
work in all locales.
- Mo, Qo and DDDo get real parse support (month/quarter/day-of-year
are all consumed by _check_parsed).
- The remaining None tokens (do, e, eo, Wo, wo) raise the regular
'Unsupported token' ValueError.
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.
Fixes #1074.
Summary
from_formatcrashed with a rawAttributeError: 'NoneType' object has no attribute 'values'for every ordinal token:from_format("7 Juni 1982", "Do MMMM YYYY", locale="de")(no ordinals in de)AttributeError1982-06-07from_format("2nd 1982", "Qo YYYY", locale="en")AttributeError(any locale)1982-04-01from_format("6th 1982", "Mo YYYY")AttributeError1982-06-01from_format("158th 1982", "DDDo YYYY")AttributeError1982-06-07from_format(..., "do/e/eo/Wo/wo ...")AttributeErrorValueError: Unsupported token: ...Meanwhile
format()happily emits all of these (format("Qo YYYY")→'2nd 1982'), so no ordinal token could ever be round-tripped.Root cause
src/pendulum/formatting/formatter.py:Doentry in_LOCALIZABLE_TOKENScallslocale.get("custom.ordinal").values()—Nonefor locales without ordinals (bg, de, ru, pl, uk, da, sv, ...).Locale.ordinalize()(theformat()side) already falls back to the plain number there, so the parse pattern must accept plain digits too.Qo/Mo/DDDo/do/e/eo/Wo/woareNonein_LOCALIZABLE_TOKENS;_replace_tokenspasses that tolocale.translation(None).values()and crashes before reaching its ownValueError("Unsupported token: ...")guard.Fix
_ordinal_candidates(locale): builds\d+<suffix>patterns fromcustom.ordinal, falling back to\d+for locales without ordinals — mirroringLocale.ordinalize(), soformat()/from_formatround-trip in every locale.Do,Mo,Qo,DDDonow parse to their fields (day/month/quarter/day_of_year), all of which_check_parsedalready consumes.do,e,eo,Wo,wonow raise the regularValueError("Unsupported token: X")instead ofAttributeError.Verification
tests/datetime/test_from_format.py(Dowith/without locale ordinals incl.de,Mo,Qo,DDDo, and the cleanValueErrorfor the unsupported tokens).tests/datetime/test_from_format.pyis byte-identical before/after (those 49 failures are thetime-machine-gated travel tests;time_machineis unavailable in this environment — diff of failing test names verified withgit stash).tests/formatting+tests/parsing: identical failure set before/after (all previously-passing tests still pass).