|
34 | 34 | missing as libmissing, |
35 | 35 | ops as libops, |
36 | 36 | ) |
| 37 | +from pandas.compat import PY312 |
37 | 38 | from pandas.compat.numpy import np_version_gt2 |
38 | 39 | from pandas.errors import Pandas4Warning |
39 | 40 |
|
@@ -452,16 +453,57 @@ class UnhashableClass2: |
452 | 453 | def __hash__(self): |
453 | 454 | raise TypeError("Not hashable") |
454 | 455 |
|
| 456 | + # Temporary helper for Python 3.11 compatibility. |
| 457 | + # This can be removed once support for Python 3.11 is dropped. |
| 458 | + class HashableSlice: |
| 459 | + def __init__(self, start, stop, step=None): |
| 460 | + self.slice = slice(start, stop, step) |
| 461 | + |
| 462 | + def __eq__(self, other): |
| 463 | + return isinstance(other, HashableSlice) and self.slice == other.slice |
| 464 | + |
| 465 | + def __hash__(self): |
| 466 | + return hash((self.slice.start, self.slice.stop, self.slice.step)) |
| 467 | + |
| 468 | + def __repr__(self): |
| 469 | + return ( |
| 470 | + f"HashableSlice({self.slice.start}, {self.slice.stop}, " |
| 471 | + f"{self.slice.step})" |
| 472 | + ) |
| 473 | + |
455 | 474 | hashable = (1, 3.14, np.float64(3.14), "a", (), (1,), HashableClass()) |
456 | 475 | not_hashable = ([], UnhashableClass1()) |
457 | 476 | abc_hashable_not_really_hashable = (([],), UnhashableClass2()) |
| 477 | + hashable_slice = HashableSlice(1, 2) |
| 478 | + tuple_with_slice = (slice(1, 2), 3) |
458 | 479 |
|
459 | 480 | for i in hashable: |
460 | 481 | assert inference.is_hashable(i) |
| 482 | + assert inference.is_hashable(i, allow_slice=True) |
| 483 | + assert inference.is_hashable(i, allow_slice=False) |
461 | 484 | for i in not_hashable: |
462 | 485 | assert not inference.is_hashable(i) |
| 486 | + assert not inference.is_hashable(i, allow_slice=True) |
| 487 | + assert not inference.is_hashable(i, allow_slice=False) |
463 | 488 | for i in abc_hashable_not_really_hashable: |
464 | 489 | assert not inference.is_hashable(i) |
| 490 | + assert not inference.is_hashable(i, allow_slice=True) |
| 491 | + assert not inference.is_hashable(i, allow_slice=False) |
| 492 | + |
| 493 | + assert inference.is_hashable(hashable_slice) |
| 494 | + assert inference.is_hashable(hashable_slice, allow_slice=True) |
| 495 | + assert inference.is_hashable(hashable_slice, allow_slice=False) |
| 496 | + |
| 497 | + if PY312: |
| 498 | + for obj in [slice(1, 2), tuple_with_slice]: |
| 499 | + assert inference.is_hashable(obj) |
| 500 | + assert inference.is_hashable(obj, allow_slice=True) |
| 501 | + assert not inference.is_hashable(obj, allow_slice=False) |
| 502 | + else: |
| 503 | + for obj in [slice(1, 2), tuple_with_slice]: |
| 504 | + assert not inference.is_hashable(obj) |
| 505 | + assert not inference.is_hashable(obj, allow_slice=True) |
| 506 | + assert not inference.is_hashable(obj, allow_slice=False) |
465 | 507 |
|
466 | 508 | # numpy.array is no longer collections.abc.Hashable as of |
467 | 509 | # https://github.com/numpy/numpy/pull/5326, just test |
|
0 commit comments