Skip to content

Commit 9e93556

Browse files
[3.12] gh-123935: Fix typo in _get_slots in dataclasses.py (GH-123941) (#123992)
gh-123935: Fix typo in `_get_slots` in `dataclasses.py` (GH-123941) (cherry picked from commit ac918cc) Co-authored-by: sobolevn <[email protected]>
1 parent cbfeb6a commit 9e93556

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
@@ -1176,7 +1176,7 @@ def _get_slots(cls):
11761176
slots = []
11771177
if getattr(cls, '__weakrefoffset__', -1) != 0:
11781178
slots.append('__weakref__')
1179-
if getattr(cls, '__dictrefoffset__', -1) != 0:
1179+
if getattr(cls, '__dictoffset__', -1) != 0:
11801180
slots.append('__dict__')
11811181
yield from slots
11821182
case str(slot):

Lib/test/test_dataclasses/__init__.py

+19
Original file line numberDiff line numberDiff line change
@@ -3560,6 +3560,25 @@ class A(WithDictSlot): ...
35603560
self.assertEqual(A().__dict__, {})
35613561
A()
35623562

3563+
@support.cpython_only
3564+
def test_dataclass_slot_dict_ctype(self):
3565+
# https://github.com/python/cpython/issues/123935
3566+
from test.support import import_helper
3567+
# Skips test if `_testcapi` is not present:
3568+
_testcapi = import_helper.import_module('_testcapi')
3569+
3570+
@dataclass(slots=True)
3571+
class HasDictOffset(_testcapi.HeapCTypeWithDict):
3572+
__dict__: dict = {}
3573+
self.assertNotEqual(_testcapi.HeapCTypeWithDict.__dictoffset__, 0)
3574+
self.assertEqual(HasDictOffset.__slots__, ())
3575+
3576+
@dataclass(slots=True)
3577+
class DoesNotHaveDictOffset(_testcapi.HeapCTypeWithWeakref):
3578+
__dict__: dict = {}
3579+
self.assertEqual(_testcapi.HeapCTypeWithWeakref.__dictoffset__, 0)
3580+
self.assertEqual(DoesNotHaveDictOffset.__slots__, ('__dict__',))
3581+
35633582
@support.cpython_only
35643583
def test_slots_with_wrong_init_subclass(self):
35653584
# 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)