You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
datetime.time comparisons and hashing mishandle sub-minute UTC offsets #155023
If both comparands are aware and have different tzinfo attributes, the comparands are first adjusted by subtracting their UTC offsets.
UTC offsets have supported seconds and microseconds since Python 3.7 (gh-49538), but parts of the datetime.time implementation handle them incorrectly.
The C implementation can consider different normalized times equal and give them different hashes:
fromdatetimeimporttime, timedelta, timezonea=time(0, tzinfo=timezone(timedelta(microseconds=1)))
b=time(0, tzinfo=timezone(timedelta(microseconds=2)))
print(a==b) # True, expected Falseprint(hash(a) ==hash(b)) # False, despite a == b
The pure Python implementation also compares valid values incorrectly and raises an internal assertion while hashing:
The comparison should use the complete normalized value wall time - UTC offset. The pure Python comparison uses minute floor division for the offset, while the C comparison omits the offset's microseconds. The pure Python hash also assumes a whole-minute offset.
I am working on a PR with regression tests for both implementations.
Activity
StanFromIreland commentedon Aug 1, 2026
Duplicate of #99772