-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: raise on non-hashable in __contains__ #30902
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
Conversation
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.
lgtm
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.
@jbrockmendel the issue title includes BUG, is there a related issue?
@@ -409,7 +408,9 @@ cdef class DatetimeEngine(Int64Engine): | |||
cdef _get_box_dtype(self): | |||
return 'M8[ns]' | |||
|
|||
def __contains__(self, object val): | |||
def __contains__(self, val: object) -> bool: |
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.
same
@@ -717,7 +718,9 @@ cdef class BaseMultiIndexCodesEngine: | |||
|
|||
return indexer | |||
|
|||
def __contains__(self, object val): | |||
def __contains__(self, val: object) -> bool: |
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.
same
@@ -390,6 +390,7 @@ def __contains__(self, key) -> bool: | |||
if is_scalar(key) and isna(key): | |||
return self.hasnans | |||
|
|||
hash(key) |
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.
I assume this is to check that key is hashable. can you not type key as Hashable?
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.
that's not a run-time check
Co-Authored-By: Simon Hawkins <[email protected]>
mypy didnt like my attempt at annotating, so reverted |
152de93
to
ee0093f
Compare
is this because __contains__ is a special method and this code is unsafe. Is this not also a breaking change? |
restored annotations. turns out the trouble was that |
I think mypy should only complain if the class is a subclass of collections.abc.Container so rather than Any, can Hashable be used? |
When I use
|
Yea I guess might be tough to get that to work with mypy since we are catching the |
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.
looks fine. can you add assert hash(key) in the cython code?
A big part of the motivation here is to avoid duplicated hash calls |
do asserts not get stripped? isn't that the point? (its not a big deal as long as you have a comment, but the asserts are to provide developer guarantees) also needs a rebase |
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.
Nice clean-up!
thanks @jbrockmendel |
By checking before we get to the
Engine.__contains__
call, we can avoid a redundant call tohash