Skip to content

Commit 2561a11

Browse files
committed
fix: distinguish keyless Timezones built from different files in __eq__/__hash__
1 parent e06dace commit 2561a11

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

‎src/pendulum/tz/timezone.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,18 @@ def _from_pickled_file(cls, data: bytes, key: str | None) -> Self:
9999
return cls.from_file(io.BytesIO(data), key=key)
100100

101101
def __eq__(self, other: object) -> bool:
102-
return isinstance(other, Timezone) and self.key == other.key
102+
if not isinstance(other, Timezone):
103+
return False
104+
if self.key is None or other.key is None:
105+
# Keyless instances (built via from_file) have no name to compare,
106+
# so fall back to the raw TZif bytes: two different zones must not
107+
# compare equal just because neither has a key.
108+
return self.key == other.key and self._file_bytes == other._file_bytes
109+
return self.key == other.key
103110

104111
def __hash__(self) -> int:
112+
if self.key is None:
113+
return hash((Timezone, self._file_bytes))
105114
return hash((Timezone, self.key))
106115

107116
@property

‎tests/tz/test_timezone.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,51 @@ def _paris_tzif_bytes() -> bytes:
486486
return resources.files("tzdata.zoneinfo").joinpath("Europe", "Paris").read_bytes()
487487

488488

489+
def _new_york_tzif_bytes() -> bytes:
490+
from importlib import resources
491+
492+
return (
493+
resources.files("tzdata.zoneinfo").joinpath("America", "New_York").read_bytes()
494+
)
495+
496+
497+
def test_from_file_timezones_with_different_bytes_are_not_equal():
498+
# Two keyless Timezones built from different zone files must not compare
499+
# equal: before this fix, __eq__ only looked at the key, which is None for
500+
# both, so any two from_file() timezones collapsed into one.
501+
from io import BytesIO
502+
503+
import pendulum.tz.timezone as timezone_module
504+
505+
paris = timezone_module.Timezone.from_file(BytesIO(_paris_tzif_bytes()))
506+
new_york = timezone_module.Timezone.from_file(BytesIO(_new_york_tzif_bytes()))
507+
508+
assert paris.key is None
509+
assert new_york.key is None
510+
assert paris != new_york
511+
assert hash(paris) != hash(new_york)
512+
assert len({paris, new_york}) == 2
513+
514+
# A keyless instance still never equals a named one.
515+
assert paris != timezone_module.Timezone("Europe/Paris")
516+
517+
518+
def test_from_file_timezones_with_same_bytes_are_equal():
519+
from io import BytesIO
520+
521+
import pendulum.tz.timezone as timezone_module
522+
523+
first = timezone_module.Timezone.from_file(BytesIO(_paris_tzif_bytes()))
524+
second = timezone_module.Timezone.from_file(BytesIO(_paris_tzif_bytes()))
525+
526+
assert first == second
527+
assert hash(first) == hash(second)
528+
assert len({first, second}) == 1
529+
530+
# Equality survives a pickle round-trip.
531+
assert pickle.loads(pickle.dumps(first)) == first
532+
533+
489534
def test_from_file_without_a_key_can_be_pickled():
490535
# get_local_timezone()'s last-resort fallback builds a Timezone this same way, with no key.
491536
from io import BytesIO

0 commit comments

Comments
 (0)