Skip to content

Commit 299b6d0

Browse files
miss-islingtonavboagEclips4carljm
authored
[3.11] gh-105866: fix dataclass with slots=True, weakref_slot=True (GH-105870) (GH-116979)
(cherry picked from commit a22d05f) Co-authored-by: Aviel Boag <[email protected]> Co-authored-by: Kirill Podoprigora <[email protected]> Co-authored-by: Carl Meyer <[email protected]>
1 parent b23fae3 commit 299b6d0

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
@@ -1132,8 +1132,10 @@ def _dataclass_setstate(self, state):
11321132

11331133
def _get_slots(cls):
11341134
match cls.__dict__.get('__slots__'):
1135+
# A class which does not define __slots__ at all is equivalent
1136+
# to a class defining __slots__ = ('__dict__', '__weakref__')
11351137
case None:
1136-
return
1138+
yield from ('__dict__', '__weakref__')
11371139
case str(slot):
11381140
yield slot
11391141
# 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
@@ -3287,6 +3287,17 @@ class A(Base):
32873287
self.assertIs(a.__weakref__, a_ref)
32883288

32893289

3290+
def test_dataclass_derived_weakref_slot(self):
3291+
class A:
3292+
pass
3293+
3294+
@dataclass(slots=True, weakref_slot=True)
3295+
class B(A):
3296+
pass
3297+
3298+
B()
3299+
3300+
32903301
class TestDescriptors(unittest.TestCase):
32913302
def test_set_name(self):
32923303
# 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)