Repository navigation
fix: distinguish keyless Timezones built from different files in __eq__/__hash__ - #1102
Open
waw666waw666 wants to merge 1 commit into
Open
waw666waw666 wants to merge 1 commit into
waw666waw666 wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix: distinguish keyless Timezones built from different files in eq/hash
Why
Timezone.__eq__and__hash__only look at thekey:But instances built via
Timezone.from_file()have no key (keyisNone).That is exactly what
get_local_timezone()'s last-resort fallback builds when/etc/localtimeis a copied file rather than a symlink, and what_tz_from_env()builds for aTZfile path. So any two keyless timezonescompare equal and hash equal, no matter which zone they actually are:
This is the leftover gap next to #1040, which made keyless
from_file()instances picklable by keeping the raw TZif bytes, but never fixed equality
or hashing. The
__eq__/__hash__pair (#502, #1008, #1009, #1019) wasdesigned for named timezones and never considered keyless ones.
What changed
src/pendulum/tz/timezone.py: when either side has no key,__eq__compares
(key, _file_bytes)instead of just the key, and__hash__hashes
(Timezone, _file_bytes)instead of(Timezone, key)._file_bytesis the raw TZif data already kept by DateTime can't be pickled when the container's timezone was set by copying /etc/localtime #1040. Named timezones still compare
by key only, and a keyless instance still never equals a named one.
How it was tested
Added two regression tests in
tests/tz/test_timezone.py(
test_from_file_timezones_with_different_bytes_are_not_equalandtest_from_file_timezones_with_same_bytes_are_equal) using thetzdatapackage's zoneinfo files, covering inequality, distinct hashes, set size,
same-bytes equality, and equality surviving a pickle round-trip:
ruff checkandruff format --checkpass on the changed files.