@@ -451,17 +451,47 @@ class UnhashableClass1:
451
451
class UnhashableClass2 :
452
452
def __hash__ (self ):
453
453
raise TypeError ("Not hashable" )
454
+
455
+ class HashableSlice :
456
+ def __init__ (self , start , stop , step = None ):
457
+ self .slice = slice (start , stop , step )
458
+
459
+ def __eq__ (self , other ):
460
+ return isinstance (other , HashableSlice ) and self .slice == other .slice
461
+
462
+ def __hash__ (self ):
463
+ return hash ((self .slice .start , self .slice .stop , self .slice .step ))
464
+
465
+ def __repr__ (self ):
466
+ return f"HashableSlice({ self .slice .start } , { self .slice .stop } , { self .slice .step } )"
467
+
454
468
455
469
hashable = (1 , 3.14 , np .float64 (3.14 ), "a" , (), (1 ,), HashableClass ())
456
- not_hashable = ([], UnhashableClass1 ())
470
+ not_hashable = ([], UnhashableClass1 (), slice ( 1 , 2 , 3 ) )
457
471
abc_hashable_not_really_hashable = (([],), UnhashableClass2 ())
472
+ hashable_slice = (HashableSlice (1 , 2 ), HashableSlice (1 , 2 , 3 ))
473
+ tuple_with_slice = ((slice (1 , 2 ), 3 ), 1 , "a" )
458
474
459
475
for i in hashable :
460
476
assert inference .is_hashable (i )
477
+ assert inference .is_hashable (i , allow_slice = True )
478
+ assert inference .is_hashable (i , allow_slice = False )
461
479
for i in not_hashable :
462
480
assert not inference .is_hashable (i )
481
+ assert not inference .is_hashable (i , allow_slice = True )
482
+ assert not inference .is_hashable (i , allow_slice = False )
463
483
for i in abc_hashable_not_really_hashable :
464
484
assert not inference .is_hashable (i )
485
+ assert not inference .is_hashable (i , allow_slice = True )
486
+ assert not inference .is_hashable (i , allow_slice = False )
487
+ for i in hashable_slice :
488
+ assert inference .is_hashable (i )
489
+ assert inference .is_hashable (i , allow_slice = True )
490
+ assert inference .is_hashable (i , allow_slice = False )
491
+
492
+ assert not inference .is_hashable (tuple_with_slice )
493
+ assert not inference .is_hashable (tuple_with_slice , allow_slice = True )
494
+ assert not inference .is_hashable (tuple_with_slice , allow_slice = False )
465
495
466
496
# numpy.array is no longer collections.abc.Hashable as of
467
497
# https://github.com/numpy/numpy/pull/5326, just test
0 commit comments