Skip to content

Commit 095f473

Browse files
committed
refactory
1 parent 1331063 commit 095f473

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

pandas/core/dtypes/inference.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,13 @@ 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, allow_slice: bool | None = None) -> TypeGuard[Hashable]:
379+
def is_hashable(obj: object, allow_slice: bool = True) -> TypeGuard[Hashable]:
380380
"""
381381
Return True if hash(obj) will succeed, False otherwise.
382382
383383
If `allow_slice` is False, objects that are slices or tuples containing slices
384384
will always return False, even if hash(obj) would succeed.
385-
If `allow_slice` is True or None, slices and tuples containing slices are treated as
385+
If `allow_slice` is True, slices and tuples containing slices are treated as
386386
hashable if hash(obj) does not raise TypeError.
387387
388388
Some types will pass a test against collections.abc.Hashable but fail when
@@ -395,8 +395,8 @@ def is_hashable(obj: object, allow_slice: bool | None = None) -> TypeGuard[Hasha
395395
----------
396396
obj : object
397397
The object to check for hashability. Any Python object can be passed here.
398-
allow_slice : bool or None
399-
If True or None, return True if the object is hashable (including slices).
398+
allow_slice : bool
399+
If True, return True if the object is hashable (including slices).
400400
If False, return True if the object is hashable and not a slice.
401401
402402
Returns
@@ -431,16 +431,11 @@ def is_hashable(obj: object, allow_slice: bool | None = None) -> TypeGuard[Hasha
431431
# Reconsider this decision once this numpy bug is fixed:
432432
# https://github.com/numpy/numpy/issues/5562
433433

434-
def _contains_slice(x: object) -> bool:
435-
# Check if object is a slice or a tuple containing a slice
436-
if isinstance(x, tuple):
437-
return any(isinstance(v, slice) for v in x)
438-
elif isinstance(x, slice):
439-
return True
440-
return False
441-
442-
if allow_slice is False and _contains_slice(obj):
443-
return False
434+
if allow_slice is False:
435+
if isinstance(obj, tuple) and any(isinstance(v, slice) for v in obj):
436+
return False
437+
elif isinstance(obj, slice):
438+
return False
444439

445440
try:
446441
hash(obj)

0 commit comments

Comments
 (0)