8000 gh-118033: Fix `__weakref__` not set for generic dataclasses by sobolevn · Pull Request #118099 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-118033: Fix __weakref__ not set for generic dataclasses #118099

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

Merged
merged 6 commits into from
May 9, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Address review
  • Loading branch information
sobolevn committed May 4, 2024
commit 7c09ec80e6c1f61f306d650d7f1a70d5efd6ba06
18 changes: 9 additions & 9 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1199,16 +1199,16 @@ def _dataclass_setstate(self, state):

def _get_slots(cls):
match cls.__dict__.get('__slots__'):
# A class which does not define __slots__ at all is equivalent
# to a class defining __slots__ = ('__dict__', '__weakref__')
# `__dictoffset__` and `__weakrefoffset__` can tell us whether
# the base type has dict/weakref slots, in a way that works correctly
# for both Python classes and C extension types. Extension types
# don't use `__slots__` for slot creation
case None:
# Except for special cases, inheriting from them do not set
# any slots at all:
slots = ['__dict__', '__weakref__']
if getattr(cls, '__weakrefoffset__', -1) == 0:
slots.remove('__weakref__')
if getattr(cls, '__dictrefoffset__', -1) == 0:
slots.remove('__dict__')
slots = []
if getattr(cls, '__weakrefoffset__', -1) != 0:
slots.append('__weakref__')
if getattr(cls, '__dictrefoffset__', -1) != 0:
slots.append('__dict__')
yield from slots
case str(slot):
yield slot
Expand Down
0