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.11] gh-105866: fix dataclass with slots=True, weakref_slot=True (GH-105870) #116979

Merged
merged 1 commit into from Mar 19, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion Lib/dataclasses.py
Expand Up @@ -1132,8 +1132,10 @@ 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__')
case None:
return
yield from ('__dict__', '__weakref__')
case str(slot):
yield slot
# Slots may be any iterable, but we cannot handle an iterator
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_dataclasses/__init__.py
Expand Up @@ -3287,6 +3287,17 @@ class A(Base):
self.assertIs(a.__weakref__, a_ref)


def test_dataclass_derived_weakref_slot(self):
class A:
pass

@dataclass(slots=True, weakref_slot=True)
class B(A):
pass

B()


class TestDescriptors(unittest.TestCase):
def test_set_name(self):
# See bpo-33141.
Expand Down
@@ -0,0 +1 @@
Fixed ``_get_slots`` bug which caused error when defining dataclasses with slots and a weakref_slot.