summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_dataclasses.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2022-06-08 01:17:56 (GMT)
committerGitHub <noreply@github.com>2022-06-08 01:17:56 (GMT)
commit121ab58e03924161df6fd0002ef7bfd45404cb2e (patch)
treebb10d99cf6f42afb5df885120a0b1dc772bd3990 /Lib/test/test_dataclasses.py
parentf26d1b5b5354348c37b7835aa1344d5d0f6f6750 (diff)
downloadcpython-121ab58e03924161df6fd0002ef7bfd45404cb2e.zip
cpython-121ab58e03924161df6fd0002ef7bfd45404cb2e.tar.gz
cpython-121ab58e03924161df6fd0002ef7bfd45404cb2e.tar.bz2
GH-93521: For dataclasses, filter out `__weakref__` slot if present in bases (GH-93535)
(cherry picked from commit 5849af7a80166e9e82040e082f22772bd7cf3061) Co-authored-by: Bluenix <bluenixdev@gmail.com>
Diffstat (limited to 'Lib/test/test_dataclasses.py')
-rw-r--r--Lib/test/test_dataclasses.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py
index 6a36da1..822e730 100644
--- a/Lib/test/test_dataclasses.py
+++ b/Lib/test/test_dataclasses.py
@@ -3109,6 +3109,54 @@ class TestSlots(unittest.TestCase):
"weakref_slot is True but slots is False"):
B = make_dataclass('B', [('a', int),], weakref_slot=True)
+ def test_weakref_slot_subclass_weakref_slot(self):
+ @dataclass(slots=True, weakref_slot=True)
+ class Base:
+ field: int
+
+ # A *can* also specify weakref_slot=True if it wants to (gh-93521)
+ @dataclass(slots=True, weakref_slot=True)
+ class A(Base):
+ ...
+
+ # __weakref__ is in the base class, not A. But an instance of A
+ # is still weakref-able.
+ self.assertIn("__weakref__", Base.__slots__)
+ self.assertNotIn("__weakref__", A.__slots__)
+ a = A(1)
+ weakref.ref(a)
+
+ def test_weakref_slot_subclass_no_weakref_slot(self):
+ @dataclass(slots=True, weakref_slot=True)
+ class Base:
+ field: int
+
+ @dataclass(slots=True)
+ class A(Base):
+ ...
+
+ # __weakref__ is in the base class, not A. Even though A doesn't
+ # specify weakref_slot, it should still be weakref-able.
+ self.assertIn("__weakref__", Base.__slots__)
+ self.assertNotIn("__weakref__", A.__slots__)
+ a = A(1)
+ weakref.ref(a)
+
+ def test_weakref_slot_normal_base_weakref_slot(self):
+ class Base:
+ __slots__ = ('__weakref__',)
+
+ @dataclass(slots=True, weakref_slot=True)
+ class A(Base):
+ field: int
+
+ # __weakref__ is in the base class, not A. But an instance of
+ # A is still weakref-able.
+ self.assertIn("__weakref__", Base.__slots__)
+ self.assertNotIn("__weakref__", A.__slots__)
+ a = A(1)
+ weakref.ref(a)
+
class TestDescriptors(unittest.TestCase):
def test_set_name(self):