@@ -376,7 +376,7 @@ def is_named_tuple(obj: object) -> bool:
376376 return isinstance (obj , abc .Sequence ) and hasattr (obj , "_fields" )
377377
378378
379- def is_hashable (obj : object ) -> TypeGuard [Hashable ]:
379+ def is_hashable (obj : object , allow_slice : bool = None ) -> TypeGuard [Hashable ]:
380380 """
381381 Return True if hash(obj) will succeed, False otherwise.
382382
@@ -390,13 +390,17 @@ def is_hashable(obj: object) -> TypeGuard[Hashable]:
390390 ----------
391391 obj : object
392392 The object to check for hashability. Any Python object can be passed here.
393+ allow_slice: bool or None
394+ - If True: return True if the object is hashable (including slices).
395+ - If False: return True if the object is hashable and not a slice.
396+ - If None: return True of the object is hashable. without checking for slice type.
393397
394398 Returns
395399 -------
396400 bool
397401 True if object can be hashed (i.e., does not raise TypeError when
398- passed to hash()), and False otherwise (e.g., if object is mutable
399- like a list or dictionary).
402+ passed to hash()) and allow_slice is True or None, and False otherwise
403+ (e.g., if object is mutable like a list or dictionary).
400404
401405 See Also
402406 --------
@@ -427,6 +431,8 @@ def is_hashable(obj: object) -> TypeGuard[Hashable]:
427431 except TypeError :
428432 return False
429433 else :
434+ if allow_slice is False and isinstance (obj , slice ):
435+ return False
430436 return True
431437
432438
0 commit comments