Skip to content

Commit b623446

Browse files
committed
Qualify timezone hashes and cover subclass equality
1 parent 11755f3 commit b623446

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

‎src/pendulum/tz/timezone.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def __eq__(self, other: object) -> bool:
7070
return isinstance(other, Timezone) and self.key == other.key
7171

7272
def __hash__(self) -> int:
73-
return hash(self.key)
73+
return hash((Timezone, self.key))
7474

7575
@property
7676
def name(self) -> str:

‎tests/tz/test_timezone.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,21 @@ def test_equal_timezones_are_interchangeable_mapping_keys(name):
5959
assert len({first, second}) == 1
6060

6161

62+
@pytest.mark.parametrize("name", ["UTC", "Europe/Paris", "America/New_York"])
63+
def test_timezone_subclasses_are_interchangeable_mapping_keys(name):
64+
class CustomTimezone(pendulum.Timezone):
65+
pass
66+
67+
base = pendulum.Timezone.no_cache(name)
68+
derived = CustomTimezone.no_cache(name)
69+
70+
assert base == derived
71+
assert derived == base
72+
assert hash(base) == hash(derived)
73+
assert {base: "value"}[derived] == "value"
74+
assert len({base, derived}) == 1
75+
76+
6277
def test_distinct_timezones_are_distinct_mapping_keys():
6378
paris = timezone("Europe/Paris")
6479
berlin = timezone("Europe/Berlin")

0 commit comments

Comments
 (0)