mirror of
https://github.com/pybind/pybind11.git
synced 2026-03-14 20:27:47 +00:00
bugfix: py contains raises errors when appropiate (#4209)
* bugfix: contains now throws an exception if the key is not hashable * Fix tests and improve robustness * Remove todo * Workaround PyPy corner case * PyPy xfail * Fix typo * fix xfail * Make clang-tidy happy * Remove redundant exc checking
This commit is contained in:
@@ -168,6 +168,31 @@ def test_dict(capture, doc):
|
||||
assert m.dict_keyword_constructor() == {"x": 1, "y": 2, "z": 3}
|
||||
|
||||
|
||||
class CustomContains:
|
||||
d = {"key": None}
|
||||
|
||||
def __contains__(self, m):
|
||||
return m in self.d
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"arg,func",
|
||||
[
|
||||
(set(), m.anyset_contains),
|
||||
(dict(), m.dict_contains),
|
||||
(CustomContains(), m.obj_contains),
|
||||
],
|
||||
)
|
||||
@pytest.mark.xfail("env.PYPY and sys.pypy_version_info < (7, 3, 10)", strict=False)
|
||||
def test_unhashable_exceptions(arg, func):
|
||||
class Unhashable:
|
||||
__hash__ = None
|
||||
|
||||
with pytest.raises(TypeError) as exc_info:
|
||||
func(arg, Unhashable())
|
||||
assert "unhashable type:" in str(exc_info.value)
|
||||
|
||||
|
||||
def test_tuple():
|
||||
assert m.tuple_no_args() == ()
|
||||
assert m.tuple_ssize_t() == ()
|
||||
|
||||
Reference in New Issue
Block a user