gh-123935: Fix typo in `_get_slots` in `dataclasses.py` (#123941)

This commit is contained in:
sobolevn 2024-09-12 10:29:06 +03:00 committed by GitHub
parent 43303e362e
commit ac918ccad7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 1 deletions

View File

@ -1208,7 +1208,7 @@ def _get_slots(cls):
slots = [] slots = []
if getattr(cls, '__weakrefoffset__', -1) != 0: if getattr(cls, '__weakrefoffset__', -1) != 0:
slots.append('__weakref__') slots.append('__weakref__')
if getattr(cls, '__dictrefoffset__', -1) != 0: if getattr(cls, '__dictoffset__', -1) != 0:
slots.append('__dict__') slots.append('__dict__')
yield from slots yield from slots
case str(slot): case str(slot):

View File

@ -3664,6 +3664,25 @@ class TestSlots(unittest.TestCase):
self.assertEqual(A().__dict__, {}) self.assertEqual(A().__dict__, {})
A() 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 @support.cpython_only
def test_slots_with_wrong_init_subclass(self): def test_slots_with_wrong_init_subclass(self):
# TODO: This test is for a kinda-buggy behavior. # TODO: This test is for a kinda-buggy behavior.

View File

@ -0,0 +1,2 @@
Fix parent slots detection for dataclasses that inherit from classes with
``__dictoffset__``.