Skip to content

Commit ac918cc

Browse files
authored
gh-123935: Fix typo in _get_slots in dataclasses.py (#123941)
1 parent 43303e3 commit ac918cc

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Lib/dataclasses.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,7 @@ def _get_slots(cls):
12081208
slots = []
12091209
if getattr(cls, '__weakrefoffset__', -1) != 0:
12101210
slots.append('__weakref__')
1211-
if getattr(cls, '__dictrefoffset__', -1) != 0:
1211+
if getattr(cls, '__dictoffset__', -1) != 0:
12121212
slots.append('__dict__')
12131213
yield from slots
12141214
case str(slot):

Lib/test/test_dataclasses/__init__.py

+19
Original file line numberDiff line numberDiff line change
@@ -3664,6 +3664,25 @@ class A(WithDictSlot): ...
36643664
self.assertEqual(A().__dict__, {})
36653665
A()
36663666

3667+
@support.cpython_only
3668+
def test_dataclass_slot_dict_ctype(self):
3669+
# https://github.com/python/cpython/issues/123935
3670+
from test.support import import_helper
3671+
# Skips test if `_testcapi` is not present:
3672+
_testcapi = import_helper.import_module('_testcapi')
3673+
3674+
@dataclass(slots=True)
3675+
class HasDictOffset(_testcapi.HeapCTypeWithDict):
3676+
__dict__: dict = {}
3677+
self.assertNotEqual(_testcapi.HeapCTypeWithDict.__dictoffset__, 0)
3678+
self.assertEqual(HasDictOffset.__slots__, ())
3679+
3680+
@dataclass(slots=True)
3681+
class DoesNotHaveDictOffset(_testcapi.HeapCTypeWithWeakref):
3682+
__dict__: dict = {}
3683+
self.assertEqual(_testcapi.HeapCTypeWithWeakref.__dictoffset__, 0)
3684+
self.assertEqual(DoesNotHaveDictOffset.__slots__, ('__dict__',))
3685+
36673686
@support.cpython_only
36683687
def test_slots_with_wrong_init_subclass(self):
36693688
# TODO: This test is for a kinda-buggy behavior.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix parent slots detection for dataclasses that inherit from classes with
2+
``__dictoffset__``.

0 commit comments

Comments
 (0)