From 505ea1d607e040bd848d516454cc0ea1fdcab4ef Mon Sep 17 00:00:00 2001 From: Malthe Borch Date: Mon, 20 Mar 2023 12:00:19 +0100 Subject: [PATCH 1/3] Add equality test --- src/pendulum/tz/timezone.py | 4 ++++ tests/tz/test_timezone.py | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/src/pendulum/tz/timezone.py b/src/pendulum/tz/timezone.py index 592703d8..63a8cc6e 100644 --- a/src/pendulum/tz/timezone.py +++ b/src/pendulum/tz/timezone.py @@ -7,6 +7,7 @@ from abc import ABC from abc import abstractmethod from typing import TYPE_CHECKING +from typing import Any from typing import TypeVar from typing import cast @@ -66,6 +67,9 @@ def __new__(cls, key: str) -> Self: except zoneinfo.ZoneInfoNotFoundError: raise InvalidTimezone(key) + def __eq__(self, other: Any) -> bool: + return isinstance(other, PendulumTimezone) and self.key == other.key + @property def name(self) -> str: return self.key diff --git a/tests/tz/test_timezone.py b/tests/tz/test_timezone.py index 19cb79a5..35220995 100644 --- a/tests/tz/test_timezone.py +++ b/tests/tz/test_timezone.py @@ -42,6 +42,11 @@ def test_basic_convert(): assert dt.tzinfo.dst(dt) == timedelta(seconds=3600) +def test_equality(): + assert timezone("Europe/Paris") == timezone("Europe/Paris") + assert timezone("Europe/Paris") != timezone("Europe/Berlin") + + def test_skipped_time_with_pre_rule(): dt = datetime(2013, 3, 31, 2, 30, 45, 123456, fold=0) tz = timezone("Europe/Paris") From 64e2553f56152a43535e16decf98dcd64713819c Mon Sep 17 00:00:00 2001 From: Malthe Borch Date: Thu, 19 Dec 2024 07:23:30 +0000 Subject: [PATCH 2/3] Consistency Co-authored-by: Bartosz Sokorski --- src/pendulum/tz/timezone.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/pendulum/tz/timezone.py b/src/pendulum/tz/timezone.py index 63a8cc6e..f37827dc 100644 --- a/src/pendulum/tz/timezone.py +++ b/src/pendulum/tz/timezone.py @@ -7,7 +7,6 @@ from abc import ABC from abc import abstractmethod from typing import TYPE_CHECKING -from typing import Any from typing import TypeVar from typing import cast @@ -67,7 +66,7 @@ def __new__(cls, key: str) -> Self: except zoneinfo.ZoneInfoNotFoundError: raise InvalidTimezone(key) - def __eq__(self, other: Any) -> bool: + def __eq__(self, other: object) -> bool: return isinstance(other, PendulumTimezone) and self.key == other.key @property From 14b2b9e0694a822bc0aec9ebbb92b317f0bfac95 Mon Sep 17 00:00:00 2001 From: Malthe Borch Date: Thu, 26 Dec 2024 12:02:05 +0100 Subject: [PATCH 3/3] Implement equality per subclass --- src/pendulum/tz/timezone.py | 5 ++++- tests/tz/test_timezone.py | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/pendulum/tz/timezone.py b/src/pendulum/tz/timezone.py index f37827dc..7d60f897 100644 --- a/src/pendulum/tz/timezone.py +++ b/src/pendulum/tz/timezone.py @@ -67,7 +67,7 @@ def __new__(cls, key: str) -> Self: raise InvalidTimezone(key) def __eq__(self, other: object) -> bool: - return isinstance(other, PendulumTimezone) and self.key == other.key + return isinstance(other, Timezone) and self.key == other.key @property def name(self) -> str: @@ -175,6 +175,9 @@ def __init__(self, offset: int, name: str | None = None) -> None: self._offset = offset self._utcoffset = _datetime.timedelta(seconds=offset) + def __eq__(self, other: object) -> bool: + return isinstance(other, FixedTimezone) and self._offset == other._offset + @property def name(self) -> str: return self._name diff --git a/tests/tz/test_timezone.py b/tests/tz/test_timezone.py index 35220995..3f090168 100644 --- a/tests/tz/test_timezone.py +++ b/tests/tz/test_timezone.py @@ -404,6 +404,11 @@ def test_fixed_timezone(): assert tz2.dst(dt) == timedelta() +def test_fixed_equality(): + assert fixed_timezone(19800) == fixed_timezone(19800) + assert fixed_timezone(19800) != fixed_timezone(19801) + + def test_just_before_last_transition(): tz = pendulum.timezone("Asia/Shanghai") dt = datetime(1991, 4, 20, 1, 49, 8, fold=0)