Skip to content

Commit 1e7315e

Browse files
committed
Add test for timedelta hash equality inariance
1 parent c126eeb commit 1e7315e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

pandas/tests/scalar/timedelta/test_timedelta.py

+26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
""" test the scalar Timedelta """
22
from datetime import timedelta
3+
import sys
4+
from typing import Hashable
35

46
from hypothesis import (
57
given,
@@ -918,6 +920,30 @@ def test_timedelta_hash_equality(self):
918920
ns_td = Timedelta(1, "ns")
919921
assert hash(ns_td) != hash(ns_td.to_pytimedelta())
920922

923+
@pytest.mark.xfail(
924+
reason="pd.Timedelta violates the Python hash invariant (GH#44504).",
925+
raises=AssertionError,
926+
strict=True,
927+
)
928+
@given(st.integers(min_value=(-sys.maxsize - 1) / 500, max_value=sys.maxsize / 500))
929+
def test_hash_equality_invariance(self, half_microseconds: int) -> None:
930+
# GH#44504
931+
932+
def _upholds_hash_equality_invariance(v1: Hashable, v2: Hashable, /) -> bool:
933+
if v1 != v2:
934+
return True
935+
936+
# See: https://docs.python.org/3/glossary.html#term-hashable
937+
# Hashable objects which compare equal must have the same hash value.
938+
return hash(v1) == hash(v2)
939+
940+
nanoseconds = half_microseconds * 500
941+
942+
pandas_timedelta = Timedelta(nanoseconds)
943+
numpy_timedelta = np.timedelta64(nanoseconds)
944+
945+
assert _upholds_hash_equality_invariance(pandas_timedelta, numpy_timedelta)
946+
921947
def test_implementation_limits(self):
922948
min_td = Timedelta(Timedelta.min)
923949
max_td = Timedelta(Timedelta.max)

0 commit comments

Comments
 (0)