From cd9307b94059f86c7e8c8ba77457198abe19c137 Mon Sep 17 00:00:00 2001 From: Malthe Borch Date: Mon, 20 Mar 2023 12:00:19 +0100 Subject: [PATCH] Add equality test --- 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 592703d8..09bf6c8f 100644 --- a/src/pendulum/tz/timezone.py +++ b/src/pendulum/tz/timezone.py @@ -8,7 +8,7 @@ from abc import abstractmethod from typing import TYPE_CHECKING from typing import TypeVar -from typing import cast +from typing import Any, cast from pendulum.tz.exceptions import AmbiguousTime from pendulum.tz.exceptions import InvalidTimezone @@ -66,6 +66,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")