Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 11a6b73

Browse files
authored
Fix hash's error message when hash method is None (#4161)
1 parent 5a5e047 commit 11a6b73

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

Lib/test/test_dataclasses.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,8 +2536,6 @@ class C:
25362536
self.assertEqual(hash(C(4)), hash((4,)))
25372537
self.assertEqual(hash(C(42)), hash((42,)))
25382538

2539-
# TODO: RUSTPYTHON
2540-
@unittest.expectedFailure
25412539
def test_hash_no_args(self):
25422540
# Test dataclasses with no hash= argument. This exists to
25432541
# make sure that if the @dataclass parameter name is changed

vm/src/protocol/object.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,10 +517,19 @@ impl PyObject {
517517
}
518518

519519
pub fn hash(&self, vm: &VirtualMachine) -> PyResult<PyHash> {
520+
let hash = self.get_class_attr(identifier!(vm, __hash__)).unwrap();
521+
if vm.is_none(&hash) {
522+
return Err(vm.new_exception_msg(
523+
vm.ctx.exceptions.type_error.to_owned(),
524+
format!("unhashable type: '{}'", self.class().name()),
525+
));
526+
}
527+
520528
let hash = self
521529
.class()
522530
.mro_find_map(|cls| cls.slots.hash.load())
523-
.unwrap(); // hash always exist
531+
.unwrap();
532+
524533
hash(self, vm)
525534
}
526535

0 commit comments

Comments
 (0)