Skip to content

Commit 11755f3

Browse files
committed
Restore hashing for named timezones
1 parent 0d71391 commit 11755f3

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

‎src/pendulum/tz/timezone.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ def __new__(cls, key: str) -> Self:
6969
def __eq__(self, other: object) -> bool:
7070
return isinstance(other, Timezone) and self.key == other.key
7171

72+
def __hash__(self) -> int:
73+
return hash(self.key)
74+
7275
@property
7376
def name(self) -> str:
7477
return self.key

‎tests/tz/test_timezone.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,29 @@ def test_equality():
4747
assert timezone("Europe/Paris") != timezone("Europe/Berlin")
4848

4949

50+
@pytest.mark.parametrize("name", ["UTC", "Europe/Paris", "America/New_York"])
51+
def test_equal_timezones_are_interchangeable_mapping_keys(name):
52+
first = pendulum.Timezone.no_cache(name)
53+
second = pendulum.Timezone.no_cache(name)
54+
55+
assert first is not second
56+
assert first == second
57+
assert hash(first) == hash(second)
58+
assert {first: "value"}[second] == "value"
59+
assert len({first, second}) == 1
60+
61+
62+
def test_distinct_timezones_are_distinct_mapping_keys():
63+
paris = timezone("Europe/Paris")
64+
berlin = timezone("Europe/Berlin")
65+
66+
values = {paris: "Paris", berlin: "Berlin"}
67+
68+
assert len(values) == 2
69+
assert values[pendulum.Timezone.no_cache("Europe/Paris")] == "Paris"
70+
assert values[pendulum.Timezone.no_cache("Europe/Berlin")] == "Berlin"
71+
72+
5073
def test_skipped_time_with_pre_rule():
5174
dt = datetime(2013, 3, 31, 2, 30, 45, 123456, fold=0)
5275
tz = timezone("Europe/Paris")

0 commit comments

Comments
 (0)