From 7bdddc80ae89a250fe6ab5235c886af924ce013e Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Sat, 28 Nov 2026 22:25:57 +0100 Subject: [PATCH] Remove more pytz compat code --- babel/dates.py | 8 ++++---- docs/dates.rst | 3 +-- tests/test_dates.py | 6 ++++++ tests/test_localtime.py | 1 - 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/babel/dates.py b/babel/dates.py index fa0d568db..d75bb4ea4 100644 --- a/babel/dates.py +++ b/babel/dates.py @@ -234,7 +234,8 @@ def get_timezone(zone: str | datetime.tzinfo | None = None) -> datetime.tzinfo: try: return zoneinfo.ZoneInfo(zone) - except zoneinfo.ZoneInfoNotFoundError as e: + except (zoneinfo.ZoneInfoNotFoundError, ValueError) as e: + # ValueError is raised for invalid zone names (e.g. absolute paths) raise LookupError(f"Unknown timezone {zone}") from e @@ -467,7 +468,7 @@ def get_timezone_gmt( >>> get_timezone_gmt(dt, locale='en', width='iso8601_short') '+00' >>> tz = get_timezone('America/Los_Angeles') - >>> dt = _localize(tz, datetime(2007, 4, 1, 15, 30)) + >>> dt = datetime(2007, 4, 1, 15, 30, tzinfo=tz) >>> get_timezone_gmt(dt, locale='en') 'GMT-07:00' >>> get_timezone_gmt(dt, 'short', locale='en') @@ -893,9 +894,8 @@ def format_time( For any pattern requiring the display of the time-zone a timezone has to be specified explicitly: - >>> t = datetime(2007, 4, 1, 15, 30) >>> tzinfo = get_timezone('Europe/Paris') - >>> t = _localize(tzinfo, t) + >>> t = datetime(2007, 4, 1, 15, 30, tzinfo=tzinfo) >>> format_time(t, format='full', tzinfo=tzinfo, locale='fr_FR') '15:30:00 heure d’été d’Europe centrale' >>> format_time(t, "hh 'o''clock' a, zzzz", tzinfo=get_timezone('US/Eastern'), diff --git a/docs/dates.rst b/docs/dates.rst index e0e58a26c..ae9ef8494 100644 --- a/docs/dates.rst +++ b/docs/dates.rst @@ -351,9 +351,8 @@ display a list of time-zones to the user. .. code-block:: pycon >>> from datetime import datetime - >>> from babel.dates import _localize - >>> dt = _localize(tz, datetime(2007, 8, 15)) + >>> dt = datetime(2007, 8, 15, tzinfo=tz) >>> get_timezone_name(dt, locale=Locale.parse('de_DE')) 'Mitteleuropäische Sommerzeit' >>> get_timezone_name(tz, locale=Locale.parse('de_DE')) diff --git a/tests/test_dates.py b/tests/test_dates.py index d9813b8f2..e9b618ca1 100644 --- a/tests/test_dates.py +++ b/tests/test_dates.py @@ -290,6 +290,12 @@ def test_get_timezone_unknown(): dates.get_timezone("DoesNot/Exist") +@pytest.mark.parametrize('zone', ['/UTC', 'Europe/Berlin/', '']) +def test_get_timezone_invalid(zone): + with pytest.raises(LookupError): + dates.get_timezone(zone) + + def test_get_timezone_gmt(timezone_getter): dt = datetime(2007, 4, 1, 15, 30) assert dates.get_timezone_gmt(dt, locale='en') == 'GMT+00:00' diff --git a/tests/test_localtime.py b/tests/test_localtime.py index 16aa8bfa1..6ad228a0f 100644 --- a/tests/test_localtime.py +++ b/tests/test_localtime.py @@ -15,7 +15,6 @@ reason="Issue 1092 is not applicable on Windows", ) def test_issue_1092(monkeypatch): - pytest.importorskip("pytz", reason="pytz is not installed") monkeypatch.setenv("TZ", "/UTC") # Malformed timezone name. with pytest.raises(LookupError): get_localzone()