From 505ea1d607e040bd848d516454cc0ea1fdcab4ef 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 | 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")