Skip to content

Fixed _get_slots bug, according to issue gh-105866 #105870

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 10 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1159,8 +1159,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
Original file line number Diff line number Diff line change
Expand Up @@ -3498,6 +3498,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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed ``_get_slots`` bug which caused error when defining dataclasses with slots and a weakref_slot.