summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsobolevn <mail@sobolevn.me>2024-09-12 07:29:06 (GMT)
committerGitHub <noreply@github.com>2024-09-12 07:29:06 (GMT)
commitac918ccad707ab2d7dbb78a4796a7b8a874f334c (patch)
tree1f4806b3984e00e4ea9e2e71d5575468be2d81d7
parent43303e362e3a7e2d96747d881021a14c7f7e3d0b (diff)
downloadcpython-ac918ccad707ab2d7dbb78a4796a7b8a874f334c.zip
cpython-ac918ccad707ab2d7dbb78a4796a7b8a874f334c.tar.gz
cpython-ac918ccad707ab2d7dbb78a4796a7b8a874f334c.tar.bz2
gh-123935: Fix typo in `_get_slots` in `dataclasses.py` (#123941)
-rw-r--r--Lib/dataclasses.py2
-rw-r--r--Lib/test/test_dataclasses/__init__.py19
-rw-r--r--Misc/NEWS.d/next/Library/2024-09-11-13-33-19.gh-issue-123935.fRZ_56.rst2
3 files changed, 22 insertions, 1 deletions
diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py
index 141aa41..ac7d40c 100644
--- a/Lib/dataclasses.py
+++ b/Lib/dataclasses.py
@@ -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):
diff --git a/Lib/test/test_dataclasses/__init__.py b/Lib/test/test_dataclasses/__init__.py
index da696ad..6934e88 100644
--- a/Lib/test/test_dataclasses/__init__.py
+++ b/Lib/test/test_dataclasses/__init__.py
@@ -3665,6 +3665,25 @@ class TestSlots(unittest.TestCase):
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.
# Ideally, it should be fixed and `__init_subclass__`
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
index 0000000..de720c3
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-09-11-13-33-19.gh-issue-123935.fRZ_56.rst
@@ -0,0 +1,2 @@
+Fix parent slots detection for dataclasses that inherit from classes with
+``__dictoffset__``.