8000 [3.13] gh-123935: Fix typo in `_get_slots` in `dataclasses.py` (GH-12… · python/cpython@ae3f347 · GitHub
[go: up one dir, main page]

Skip to content

Commit ae3f347

Browse files
[3.13] gh-123935: Fix typo in _get_slots in dataclasses.py (GH-123941) (#123991)
gh-123935: Fix typo in `_get_slots` in `dataclasses.py` (GH-123941) (cherry picked from commit ac918cc) Co-authored-by: sobolevn <mail@sobolevn.me>
1 parent a63e06d commit ae3f347

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Lib/dataclasses.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,7 @@ def _get_slots(cls):
12061206
slots = []
12071207
if getattr(cls, '__weakrefoffset__', -1) != 0:
12081208
slots.append('__weakref__')
1209-
if getattr(cls, '__dictrefoffset__', -1) != 0:
1209+
if getattr(cls, '__dictoffset__', -1) != 0:
12101210
slots.append('__dict__')
12111211
yield from slots
12121212
case str(slot):

Lib/test/test_dataclasses/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3707,6 +3707,25 @@ class A(WithDictSlot): ...
37073707
self.assertEqual(A().__dict__, {})
37083708
A()
37093709

3710+
@support.cpython_only
3711+
def test_dataclass_slot_dict_ctype(self):
3712+
# https://github.com/python/cpython/issues/123935
3713+
from test.support import import_helper
3714+
# Skips test if `_testcapi` is not present:
3715+
_testcapi = import_helper.import_module('_testcapi')
3716+
3717+
@dataclass(slots=True)
3718+
class HasDictOffset(_testcapi.HeapCTypeWithDict):
3719+
__dict__: dict = {}
3720+
self.assertNotEqual(_testcapi.HeapCTypeWithDict.__dictoffset__, 0)
3721+
self.assertEqual(HasDictOffset.__slots__, ())
3722+
3723+
@dataclass(slots=True)
3724+
class DoesNotHaveDictOffset(_testcapi.HeapCTypeWithWeakref):
3725+
__dict__: dict = {}
3726+
self.assertEqual(_testcapi.HeapCTypeWithWeakref.__dictoffset__, 0)
3727+
self.assertEqual(DoesNotHaveDictOffset.__slots__, ('__dict__',))
3728+
37103729
@support.cpython_only
37113730
def test_slots_with_wrong_init_subclass(self):
37123731
# TODO: This test is for a kinda-buggy behavior.
Lines changed: 2 additions & 0 deletions
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)
0