mirror of
https://github.com/python/cpython.git
synced 2025-08-09 11:29:45 +00:00
[3.12] gh-105866: fix dataclass with slots=True, weakref_slot=True (GH-105870) (GH-116978)
(cherry picked from commit a22d05f04c
)
Co-authored-by: Aviel Boag <avboag@gmail.com>
Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
Co-authored-by: Carl Meyer <carl@oddbird.net>
This commit is contained in:
parent
e1f890828e
commit
23f97327fe
3 changed files with 15 additions and 1 deletions
|
@ -1168,8 +1168,10 @@ def _dataclass_setstate(self, state):
|
||||||
|
|
||||||
def _get_slots(cls):
|
def _get_slots(cls):
|
||||||
match cls.__dict__.get('__slots__'):
|
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:
|
case None:
|
||||||
return
|
yield from ('__dict__', '__weakref__')
|
||||||
case str(slot):
|
case str(slot):
|
||||||
yield slot
|
yield slot
|
||||||
# Slots may be any iterable, but we cannot handle an iterator
|
# Slots may be any iterable, but we cannot handle an iterator
|
||||||
|
|
|
@ -3403,6 +3403,17 @@ class TestSlots(unittest.TestCase):
|
||||||
self.assertIs(a.__weakref__, a_ref)
|
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):
|
class TestDescriptors(unittest.TestCase):
|
||||||
def test_set_name(self):
|
def test_set_name(self):
|
||||||
# See bpo-33141.
|
# See bpo-33141.
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fixed ``_get_slots`` bug which caused error when defining dataclasses with slots and a weakref_slot.
|
Loading…
Add table
Add a link
Reference in a new issue