Fix hash's error message when hash method is None (#4161)

This commit is contained in:
Gyubong
2022-10-01 22:46:14 +09:00
committed by GitHub
parent 5a5e047eab
commit 11a6b73fbf
2 changed files with 10 additions and 3 deletions

View File

@@ -2536,8 +2536,6 @@ class TestHash(unittest.TestCase):
self.assertEqual(hash(C(4)), hash((4,)))
self.assertEqual(hash(C(42)), hash((42,)))
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_hash_no_args(self):
# Test dataclasses with no hash= argument. This exists to
# make sure that if the @dataclass parameter name is changed

View File

@@ -517,10 +517,19 @@ impl PyObject {
}
pub fn hash(&self, vm: &VirtualMachine) -> PyResult<PyHash> {
let hash = self.get_class_attr(identifier!(vm, __hash__)).unwrap();
if vm.is_none(&hash) {
return Err(vm.new_exception_msg(
vm.ctx.exceptions.type_error.to_owned(),
format!("unhashable type: '{}'", self.class().name()),
));
}
let hash = self
.class()
.mro_find_map(|cls| cls.slots.hash.load())
.unwrap(); // hash always exist
.unwrap();
hash(self, vm)
}