Repository navigation
Conversation
Signed-off-by: Aditya <adityasbakawat@gmail.com>
| "Do": lambda locale: tuple( | ||
| rf"\d+{o}" for o in locale.get("custom.ordinal").values() | ||
| rf"\d+{re.escape(o)}" | ||
| for o in (locale.get("custom.ordinal") or {"other": ""}).values() | ||
| ), |
There was a problem hiding this comment.
Italian still can't round-trip a few days. it has custom.ordinal = {"other": "°"} but its plural rule gives many for 8, 11, 80, 800, and Locale.ordinalize returns the bare number when the category has no suffix. So format() writes 8 / 11, and the only candidate here is \d+°:
d = pendulum.datetime(2024, 1, 8)
text = d.format("YYYY-MM-Do", locale="it") # '2024-01-8'
pendulum.from_format(text, "YYYY-MM-Do", locale="it")
# ValueError: String does not match format YYYY-MM-DoSince ordinalize can always fall back to the bare number, adding \d+ as the last candidate covers both this and the no-mapping case (the suffix candidates come first in the alternation, so 1st / 1er still match whole):
| "Do": lambda locale: tuple( | |
| rf"\d+{o}" for o in locale.get("custom.ordinal").values() | |
| rf"\d+{re.escape(o)}" | |
| for o in (locale.get("custom.ordinal") or {"other": ""}).values() | |
| ), | |
| "Do": lambda locale: ( | |
| *( | |
| rf"\d+{re.escape(o)}" | |
| for o in (locale.get("custom.ordinal") or {}).values() | |
| if o | |
| ), | |
| r"\d+", | |
| ), |
I checked format → from_format for all 30 bundled locales, days 1–31, with YYYY-MM-Do, Do MMMM YYYY, MMMM Do, YYYY and Do.MM.YYYY: with the current branch only it fails (days 8 and 11); with the change above 0 failures, and tests/datetime/test_from_format.py + tests/formatting still pass (145). Adding "it" to the locale list in your round-trip test would catch it.
There was a problem hiding this comment.
Thanks, 3091abe fixes it. I ran a format → parse round trip for all 30 bundled locales, days 1-31, in 5 formats (YYYY-MM-Do, Do MMMM YYYY, MMMM Do, YYYY, Do.MM.YYYY, Do MMM YYYY): 0 failures, Italian 8/11 included. The re.escape on the suffix is a good catch too.
from_format()crashes withAttributeErrorforDoin locales without custom ordinal suffixes, even when parsing the output offormat(). For example, a GermanYYYY-MM-Dostring cannot round-trip.Use a bare numeric pattern when the locale has no ordinal suffix mapping, and escape existing suffixes as literal text. Also reject localized tokens mapped to
Nonewith the existingValueError("Unsupported token: ...")diagnostic instead of looking up a nonexistent translation.Partially addresses #1074: this fixes
Doand the unsupported-token errors. Adding parsing support forMo,Qo, andDDDoremains separate work.Validation:
62 parametrized regression cases passed (9 locales across 6 days, plus 8 unsupported tokens). Before the fix, 50 failed and 12 passed.
Existing formatting tests: 22 passed.
Ruff 0.14.11 lint and format checks passed for both changed files.
Tests ran on Python 3.14 with
PYTHONPATH=srcandPENDULUM_EXTENSIONS=0. The full suite was not run: Windows Application Control blocks the nativetime-machinedependency used by the time-travel tests.Added tests for changed code.
Updated documentation for changed code (no new supported tokens or public API).