Skip to content

Commit 23f9732

Browse files
miss-islingtonavboagEclips4carljm
authored
[3.12] pythongh-105866: fix dataclass with slots=True, weakref_slot=True (pythonGH-105870) (pythonGH-116978)
(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 e1f8908 commit 23f9732

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

11691169
def _get_slots(cls):
11701170
match cls.__dict__.get('__slots__'):
1171+
# A class which does not define __slots__ at all is equivalent
1172+
# to a class defining __slots__ = ('__dict__', '__weakref__')
11711173
case None:
1172-
return
1174+
yield from ('__dict__', '__weakref__')
11731175
case str(slot):
11741176
yield slot
11751177
# 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
@@ -3403,6 +3403,17 @@ class A(Base):
34033403
self.assertIs(a.__weakref__, a_ref)
34043404

34053405

3406+
def test_dataclass_derived_weakref_slot(self):
3407+
class A:
3408+
pass
3409+
3410+
@dataclass(slots=True, weakref_slot=True)
3411+
class B(A):
3412+
pass
3413+
3414+
B()
3415+
3416+
34063417
class TestDescriptors(unittest.TestCase):
34073418
def test_set_name(self):
34083419
# 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)