From 6a7be1e1424ea9ad34de455087edf9503e1474ef Mon Sep 17 00:00:00 2001 From: Jonathan Rotter Date: Fri, 19 Apr 2024 02:44:23 -0500 Subject: [PATCH] compat fix for testHashStuff in test_class.py (#5240) * compat fix for testHashStuff in test_class.py * accidently fixed 3 more tests --- Lib/test/test_class.py | 2 -- Lib/test/test_hash.py | 4 ---- Lib/test/test_weakset.py | 2 -- vm/src/builtins/type.rs | 8 ++++++++ 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_class.py b/Lib/test/test_class.py index a6c99fbdd..24ba57f3d 100644 --- a/Lib/test/test_class.py +++ b/Lib/test/test_class.py @@ -489,8 +489,6 @@ class ClassTests(unittest.TestCase): for f in [float, complex, str, repr, bytes, bin, oct, hex, bool, index]: self.assertRaises(TypeError, f, BadTypeClass()) - # TODO: RUSTPYTHON - @unittest.expectedFailure def testHashStuff(self): # Test correct errors from hash() on objects with comparisons but # no __hash__ diff --git a/Lib/test/test_hash.py b/Lib/test/test_hash.py index 02d55e701..9fae17db7 100644 --- a/Lib/test/test_hash.py +++ b/Lib/test/test_hash.py @@ -132,8 +132,6 @@ class HashInheritanceTestCase(unittest.TestCase): for obj in self.fixed_expected: self.assertEqual(hash(obj), _FIXED_HASH_VALUE) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_error_hash(self): for obj in self.error_expected: self.assertRaises(TypeError, hash, obj) @@ -144,8 +142,6 @@ class HashInheritanceTestCase(unittest.TestCase): for obj in objects: self.assertIsInstance(obj, Hashable) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_not_hashable(self): for obj in self.error_expected: self.assertNotIsInstance(obj, Hashable) diff --git a/Lib/test/test_weakset.py b/Lib/test/test_weakset.py index b93ecfdb7..587b17b34 100644 --- a/Lib/test/test_weakset.py +++ b/Lib/test/test_weakset.py @@ -203,8 +203,6 @@ class TestWeakSet(unittest.TestCase): t = WeakSet(s) self.assertNotEqual(id(s), id(t)) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_hash(self): self.assertRaises(TypeError, hash, self.s) diff --git a/vm/src/builtins/type.rs b/vm/src/builtins/type.rs index 391d022d1..e71497d60 100644 --- a/vm/src/builtins/type.rs +++ b/vm/src/builtins/type.rs @@ -733,6 +733,14 @@ impl PyType { .entry(identifier!(vm, __qualname__)) .or_insert_with(|| vm.ctx.new_str(name.as_str()).into()); + if attributes.get(identifier!(vm, __eq__)).is_some() + && attributes.get(identifier!(vm, __hash__)).is_none() + { + // if __eq__ exists but __hash__ doesn't, overwrite it with None so it doesn't inherit the default hash + // https://docs.python.org/3/reference/datamodel.html#object.__hash__ + attributes.insert(identifier!(vm, __hash__), vm.ctx.none.clone().into()); + } + // All *classes* should have a dict. Exceptions are *instances* of // classes that define __slots__ and instances of built-in classes // (with exceptions, e.g function)