Skip to content

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

Merged
merged 15 commits into from
Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions pandas/_libs/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ cdef class IndexEngine:
self.over_size_threshold = n >= _SIZE_CUTOFF
self.clear_mapping()

def __contains__(self, object val):
def __contains__(self, val: object) -> bool:
# We assume before we get here:
# - val is hashable
self._ensure_mapping_populated()
hash(val)
return val in self.mapping

cpdef get_value(self, ndarray arr, object key, object tz=None):
Expand Down Expand Up @@ -415,7 +416,9 @@ cdef class DatetimeEngine(Int64Engine):
raise TypeError(scalar)
return scalar.value

def __contains__(self, object val):
def __contains__(self, val: object) -> bool:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

# We assume before we get here:
# - val is hashable
cdef:
int64_t loc, conv

Expand Down Expand Up @@ -712,7 +715,9 @@ cdef class BaseMultiIndexCodesEngine:

return indexer

def __contains__(self, object val):
def __contains__(self, val: object) -> bool:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

# We assume before we get here:
# - val is hashable
# Default __contains__ looks in the underlying mapping, which in this
# case only contains integer representations.
try:
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
import operator
from textwrap import dedent
from typing import Dict, FrozenSet, Hashable, Optional, Union
from typing import Any, Dict, FrozenSet, Hashable, Optional, Union
import warnings

import numpy as np
Expand Down Expand Up @@ -4145,7 +4145,7 @@ def is_type_compatible(self, kind) -> bool:
"""

@Appender(_index_shared_docs["contains"] % _index_doc_kwargs)
def __contains__(self, key) -> bool:
def __contains__(self, key: Any) -> bool:
hash(key)
try:
return key in self._engine
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,11 +385,12 @@ def _wrap_setop_result(self, other, result):
return self._shallow_copy(result, name=name)

@Appender(_index_shared_docs["contains"] % _index_doc_kwargs)
def __contains__(self, key) -> bool:
def __contains__(self, key: Any) -> bool:
# if key is a NaN, check if any NaN is in self.
if is_scalar(key) and isna(key):
return self.hasnans

hash(key)
Copy link
Member

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?

Copy link
Contributor

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

return contains(self, key, container=self._engine)

def __array__(self, dtype=None) -> np.ndarray:
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Base and utility classes for tseries type pandas objects.
"""
import operator
from typing import List, Optional, Set
from typing import Any, List, Optional, Set

import numpy as np

Expand Down Expand Up @@ -153,7 +153,8 @@ def equals(self, other) -> bool:
return np.array_equal(self.asi8, other.asi8)

@Appender(_index_shared_docs["contains"] % _index_doc_kwargs)
def __contains__(self, key):
def __contains__(self, key: Any) -> bool:
hash(key)
try:
res = self.get_loc(key)
except (KeyError, TypeError, ValueError):
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def _engine(self):
right = self._maybe_convert_i8(self.right)
return IntervalTree(left, right, closed=self.closed)

def __contains__(self, key) -> bool:
def __contains__(self, key: Any) -> bool:
"""
return a boolean if this key is IN the index
We *only* accept an Interval
Expand All @@ -387,6 +387,7 @@ def __contains__(self, key) -> bool:
-------
bool
"""
hash(key)
if not isinstance(key, Interval):
return False

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime
from sys import getsizeof
from typing import Hashable, List, Optional, Sequence, Union
from typing import Any, Hashable, List, Optional, Sequence, Union
import warnings

import numpy as np
Expand Down Expand Up @@ -973,7 +973,7 @@ def _shallow_copy_with_infer(self, values, **kwargs):
return self._shallow_copy(values, **kwargs)

@Appender(_index_shared_docs["contains"] % _index_doc_kwargs)
def __contains__(self, key) -> bool:
def __contains__(self, key: Any) -> bool:
hash(key)
try:
self.get_loc(key)
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any

import numpy as np

Expand Down Expand Up @@ -461,7 +461,8 @@ def equals(self, other) -> bool:
except (TypeError, ValueError):
return False

def __contains__(self, other) -> bool:
def __contains__(self, other: Any) -> bool:
hash(other)
if super().__contains__(other):
return True

Expand Down
7 changes: 4 additions & 3 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime, timedelta
from typing import Any
import weakref

import numpy as np
Expand Down Expand Up @@ -369,18 +370,18 @@ def _engine(self):
return self._engine_type(period, len(self))

@Appender(_index_shared_docs["contains"])
def __contains__(self, key) -> bool:
def __contains__(self, key: Any) -> bool:
if isinstance(key, Period):
if key.freq != self.freq:
return False
else:
return key.ordinal in self._engine
else:
hash(key)
try:
self.get_loc(key)
return True
except (TypeError, KeyError):
# TypeError can be reached if we pass a tuple that is not hashable
except KeyError:
return False

@cache_readonly
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import timedelta
import operator
from sys import getsizeof
from typing import Optional, Union
from typing import Any, Optional
import warnings

import numpy as np
Expand Down Expand Up @@ -332,7 +332,7 @@ def is_monotonic_decreasing(self) -> bool:
def has_duplicates(self) -> bool:
return False

def __contains__(self, key: Union[int, np.integer]) -> bool:
def __contains__(self, key: Any) -> bool:
hash(key)
try:
key = ensure_python_int(key)
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,3 +883,11 @@ def test_getitem_2d_deprecated(self):
res = idx[:, None]

assert isinstance(res, np.ndarray), type(res)

def test_contains_requires_hashable_raises(self):
idx = self.create_index()
with pytest.raises(TypeError, match="unhashable type"):
[] in idx

with pytest.raises(TypeError):
{} in idx._engine