]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-123935: Fix typo in `_get_slots` in `dataclasses.py` (#123941)
authorsobolevn <mail@sobolevn.me>
Thu, 12 Sep 2024 07:29:06 +0000 (10:29 +0300)
committerGitHub <noreply@github.com>
Thu, 12 Sep 2024 07:29:06 +0000 (10:29 +0300)
Lib/dataclasses.py
Lib/test/test_dataclasses/__init__.py
Misc/NEWS.d/next/Library/2024-09-11-13-33-19.gh-issue-123935.fRZ_56.rst [new file with mode: 0644]

index 141aa41c74d7ed15c9b56100adefad61ecde10af..ac7d40cf2cac2edae1c14e8564834360ac9259f1 100644 (file)
@@ -1208,7 +1208,7 @@ def _get_slots(cls):
             slots = []
             if getattr(cls, '__weakrefoffset__', -1) != 0:
                 slots.append('__weakref__')
-            if getattr(cls, '__dictrefoffset__', -1) != 0:
+            if getattr(cls, '__dictoffset__', -1) != 0:
                 slots.append('__dict__')
             yield from slots
         case str(slot):
index da696ad961cfd7763e1b36cf6be57b5d2843b664..6934e88d9d338cf1ff01bddd840b2ce92488f186 100644 (file)
@@ -3664,6 +3664,25 @@ class TestSlots(unittest.TestCase):
         self.assertEqual(A().__dict__, {})
         A()
 
+    @support.cpython_only
+    def test_dataclass_slot_dict_ctype(self):
+        # https://github.com/python/cpython/issues/123935
+        from test.support import import_helper
+        # Skips test if `_testcapi` is not present:
+        _testcapi = import_helper.import_module('_testcapi')
+
+        @dataclass(slots=True)
+        class HasDictOffset(_testcapi.HeapCTypeWithDict):
+            __dict__: dict = {}
+        self.assertNotEqual(_testcapi.HeapCTypeWithDict.__dictoffset__, 0)
+        self.assertEqual(HasDictOffset.__slots__, ())
+
+        @dataclass(slots=True)
+        class DoesNotHaveDictOffset(_testcapi.HeapCTypeWithWeakref):
+            __dict__: dict = {}
+        self.assertEqual(_testcapi.HeapCTypeWithWeakref.__dictoffset__, 0)
+        self.assertEqual(DoesNotHaveDictOffset.__slots__, ('__dict__',))
+
     @support.cpython_only
     def test_slots_with_wrong_init_subclass(self):
         # TODO: This test is for a kinda-buggy behavior.
diff --git a/Misc/NEWS.d/next/Library/2024-09-11-13-33-19.gh-issue-123935.fRZ_56.rst b/Misc/NEWS.d/next/Library/2024-09-11-13-33-19.gh-issue-123935.fRZ_56.rst
new file mode 100644 (file)
index 0000000..de720c3
--- /dev/null
@@ -0,0 +1,2 @@
+Fix parent slots detection for dataclasses that inherit from classes with
+``__dictoffset__``.