Add additional info to TypeError when C++->Python casting fails (#3605)

* Add additional info to TypeInfo when C++->Python casting fails

* Fix typo

* Address reviewer comments
This commit is contained in:
Aaron Gokaslan
2022-01-10 21:18:00 -05:00
committed by GitHub
parent b66328b043
commit ef070f7750
3 changed files with 40 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import pytest
import env
from pybind11_tests import ConstructorStats
from pybind11_tests import operators as m
@@ -135,8 +136,9 @@ def test_overriding_eq_reset_hash():
assert m.Comparable(15) is not m.Comparable(15)
assert m.Comparable(15) == m.Comparable(15)
with pytest.raises(TypeError):
hash(m.Comparable(15)) # TypeError: unhashable type: 'm.Comparable'
with pytest.raises(TypeError) as excinfo:
hash(m.Comparable(15))
assert str(excinfo.value).startswith("unhashable type:")
for hashable in (m.Hashable, m.Hashable2):
assert hashable(15) is not hashable(15)
@@ -144,3 +146,10 @@ def test_overriding_eq_reset_hash():
assert hash(hashable(15)) == 15
assert hash(hashable(15)) == hash(hashable(15))
def test_return_set_of_unhashable():
with pytest.raises(TypeError) as excinfo:
m.get_unhashable_HashMe_set()
if not env.PY2:
assert str(excinfo.value.__cause__).startswith("unhashable type:")