Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.12] gh-123935: Fix typo in _get_slots in dataclasses.py (GH-123941) #123992

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,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):
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_dataclasses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3560,6 +3560,25 @@ class A(WithDictSlot): ...
self.assertEqual(A().__dict__, {})
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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix parent slots detection for dataclasses that inherit from classes with
``__dictoffset__``.
Loading