Skip to content

Commit 860186b

Browse files
avboagblurb-it[bot]Eclips4carljm
authored andcommitted
pythongh-105866: fix dataclass with slots=True, weakref_slot=True (python#105870)
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Kirill Podoprigora <[email protected]> Co-authored-by: Carl Meyer <[email protected]>
1 parent 873b95e commit 860186b

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

Lib/dataclasses.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1159,8 +1159,10 @@ def _dataclass_setstate(self, state):
11591159

11601160
def _get_slots(cls):
11611161
match cls.__dict__.get('__slots__'):
1162+
# A class which does not define __slots__ at all is equivalent
1163+
# to a class defining __slots__ = ('__dict__', '__weakref__')
11621164
case None:
1163-
return
1165+
yield from ('__dict__', '__weakref__')
11641166
case str(slot):
11651167
yield slot
11661168
# Slots may be any iterable, but we cannot handle an iterator

Lib/test/test_dataclasses/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3498,6 +3498,17 @@ class A(Base):
34983498
self.assertIs(a.__weakref__, a_ref)
34993499

35003500

3501+
def test_dataclass_derived_weakref_slot(self):
3502+
class A:
3503+
pass
3504+
3505+
@dataclass(slots=True, weakref_slot=True)
3506+
class B(A):
3507+
pass
3508+
3509+
B()
3510+
3511+
35013512
class TestDescriptors(unittest.TestCase):
35023513
def test_set_name(self):
35033514
# See bpo-33141.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed ``_get_slots`` bug which caused error when defining dataclasses with slots and a weakref_slot.

0 commit comments

Comments
 (0)