From f2029c57778fba4faed6fcbdbd10eb677099dc22 Mon Sep 17 00:00:00 2001 From: Aditya Date: Mon, 16 Nov 2026 09:38:39 +0530 Subject: [PATCH 1/2] Fix ordinal day parsing without locale suffixes Signed-off-by: Aditya --- src/pendulum/formatting/formatter.py | 6 +++++- tests/datetime/test_from_format.py | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/pendulum/formatting/formatter.py b/src/pendulum/formatting/formatter.py index b09977d0d..3990d508b 100644 --- a/src/pendulum/formatting/formatter.py +++ b/src/pendulum/formatting/formatter.py @@ -77,7 +77,8 @@ class Formatter: "Mo": None, "DDDo": None, "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() ), "dddd": "days.wide", "ddd": "days.abbreviated", @@ -676,6 +677,9 @@ def _replace_tokens(self, token: str, locale: Locale) -> str: if token in self._LOCALIZABLE_TOKENS: values = self._LOCALIZABLE_TOKENS[token] + if values is None: + raise ValueError(f"Unsupported token: {token}") + if callable(values): candidates = values(locale) else: diff --git a/tests/datetime/test_from_format.py b/tests/datetime/test_from_format.py index ecf59632a..46f755a8b 100644 --- a/tests/datetime/test_from_format.py +++ b/tests/datetime/test_from_format.py @@ -220,3 +220,21 @@ def test_from_format_2_digit_year(): d = pendulum.from_format("99", "YY") assert d.year == 1999 + + +@pytest.mark.parametrize( + "locale", ["de", "bg", "ru", "pl", "uk", "da", "sv", "en", "fr"] +) +@pytest.mark.parametrize("day", [1, 2, 3, 11, 21, 31]) +def test_from_format_ordinal_day_round_trip(locale, day): + date = pendulum.datetime(2024, 1, day) + fmt = "YYYY-MM-Do" + text = date.format(fmt, locale=locale) + + assert pendulum.from_format(text, fmt, locale=locale) == date + + +@pytest.mark.parametrize("token", ["Qo", "Mo", "DDDo", "do", "e", "eo", "Wo", "wo"]) +def test_from_format_unsupported_localized_token(token): + with pytest.raises(ValueError, match=f"^Unsupported token: {token}$"): + pendulum.from_format("1", token) From 3091abec7ffd413ae8fef35262e4daad1e729c15 Mon Sep 17 00:00:00 2001 From: Aditya Singh Bakawat Date: Wed, 18 Nov 2026 00:49:45 +0530 Subject: [PATCH 2/2] Fix ordinal parsing fallback for partial locale suffix mappings --- src/pendulum/formatting/formatter.py | 10 +++++++--- tests/datetime/test_from_format.py | 10 ++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/pendulum/formatting/formatter.py b/src/pendulum/formatting/formatter.py index 3990d508b..810edc5d5 100644 --- a/src/pendulum/formatting/formatter.py +++ b/src/pendulum/formatting/formatter.py @@ -76,9 +76,13 @@ class Formatter: "MMM": "months.abbreviated", "Mo": None, "DDDo": None, - "Do": lambda locale: tuple( - 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+", ), "dddd": "days.wide", "ddd": "days.abbreviated", diff --git a/tests/datetime/test_from_format.py b/tests/datetime/test_from_format.py index 46f755a8b..9cc2246a3 100644 --- a/tests/datetime/test_from_format.py +++ b/tests/datetime/test_from_format.py @@ -223,12 +223,14 @@ def test_from_format_2_digit_year(): @pytest.mark.parametrize( - "locale", ["de", "bg", "ru", "pl", "uk", "da", "sv", "en", "fr"] + "locale", ["de", "bg", "ru", "pl", "uk", "da", "sv", "en", "fr", "it"] ) -@pytest.mark.parametrize("day", [1, 2, 3, 11, 21, 31]) -def test_from_format_ordinal_day_round_trip(locale, day): +@pytest.mark.parametrize("day", [1, 2, 3, 8, 11, 21, 31]) +@pytest.mark.parametrize( + "fmt", ["YYYY-MM-Do", "Do MMMM YYYY", "MMMM Do, YYYY", "Do.MM.YYYY"] +) +def test_from_format_ordinal_day_round_trip(locale, day, fmt): date = pendulum.datetime(2024, 1, day) - fmt = "YYYY-MM-Do" text = date.format(fmt, locale=locale) assert pendulum.from_format(text, fmt, locale=locale) == date