-
-
Notifications
You must be signed in to change notification settings - Fork 19.2k
CLN: Add allow_slice to is_hashable function #62567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 28 commits
9a1c452
ee2bf48
70a8357
bae4e48
c324360
37fec55
859f2f8
90cb69e
4544845
0e4d675
03c1b22
8ded755
067b830
8fcecee
871d621
d145d0b
2b0ba79
2a6e7a1
7b83c16
0a57d73
c684c43
06af37c
fb3e85f
272a17b
4440d70
fd30506
62793a4
2a3a0a8
1331063
095f473
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -376,10 +376,15 @@ def is_named_tuple(obj: object) -> bool: | |
| return isinstance(obj, abc.Sequence) and hasattr(obj, "_fields") | ||
|
|
||
|
|
||
| def is_hashable(obj: object) -> TypeGuard[Hashable]: | ||
| def is_hashable(obj: object, allow_slice: bool | None = None) -> TypeGuard[Hashable]: | ||
| """ | ||
| Return True if hash(obj) will succeed, False otherwise. | ||
|
|
||
| If `allow_slice` is False, objects that are slices or tuples containing slices | ||
| will always return False, even if hash(obj) would succeed. | ||
| If `allow_slice` is True or None, slices and tuples containing slices are treated as | ||
| hashable if hash(obj) does not raise TypeError. | ||
|
|
||
| Some types will pass a test against collections.abc.Hashable but fail when | ||
| they are actually hashed with hash(). | ||
|
|
||
|
|
@@ -390,13 +395,17 @@ def is_hashable(obj: object) -> TypeGuard[Hashable]: | |
| ---------- | ||
| obj : object | ||
| The object to check for hashability. Any Python object can be passed here. | ||
| allow_slice : bool or None | ||
| If True or None, return True if the object is hashable (including slices). | ||
| If False, return True if the object is hashable and not a slice. | ||
|
|
||
| Returns | ||
| ------- | ||
| bool | ||
| True if object can be hashed (i.e., does not raise TypeError when | ||
| passed to hash()), and False otherwise (e.g., if object is mutable | ||
| like a list or dictionary). | ||
| passed to hash()) and passes the slice check according to 'allow_slice'. | ||
| False otherwise (e.g., if object is mutable like a list or dictionary | ||
| or if allow_slice is False and object is a slice or contains a slice). | ||
|
|
||
| See Also | ||
| -------- | ||
|
|
@@ -422,6 +431,17 @@ def is_hashable(obj: object) -> TypeGuard[Hashable]: | |
| # Reconsider this decision once this numpy bug is fixed: | ||
| # https://github.com/numpy/numpy/issues/5562 | ||
|
|
||
| def _contains_slice(x: object) -> bool: | ||
|
||
| # Check if object is a slice or a tuple containing a slice | ||
| if isinstance(x, tuple): | ||
| return any(isinstance(v, slice) for v in x) | ||
| elif isinstance(x, slice): | ||
| return True | ||
| return False | ||
|
|
||
| if allow_slice is False and _contains_slice(obj): | ||
| return False | ||
|
|
||
| try: | ||
| hash(obj) | ||
| except TypeError: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -213,7 +213,7 @@ def __init__( # pyright: ignore[reportInconsistentConstructor] | |
| ) | ||
|
|
||
| try: | ||
| self._book = Workbook(self._handles.handle, **engine_kwargs) # type: ignore[arg-type] | ||
|
||
| self._book = Workbook(self._handles.handle, **engine_kwargs) | ||
| except TypeError: | ||
| self._handles.handle.close() | ||
| raise | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is None needed?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None is a default value. So for exsiting callers, I don't need to set allow_slice. Unless you want me to change all the callers of is_hahsable and set the allow_slice to be True (if they don't have extra checks for slice). Please let me know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just set the default to True.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, set the default to be True now.