From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Tue, 19 Mar 2024 10:57:45 +0000 (+0100) Subject: [3.11] gh-105866: fix dataclass with slots=True, weakref_slot=True (GH-105870) (GH... X-Git-Tag: v3.11.9~38 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=299b6d06e8848e021e5c617597c222cfded92fce;p=thirdparty%2FPython%2Fcpython.git [3.11] gh-105866: fix dataclass with slots=True, weakref_slot=True (GH-105870) (GH-116979) (cherry picked from commit a22d05f04c074dbb4f71e7837f54c0bb693db75d) Co-authored-by: Aviel Boag Co-authored-by: Kirill Podoprigora Co-authored-by: Carl Meyer --- diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py index 148daf45330c..d8d735736b70 100644 --- a/Lib/dataclasses.py +++ b/Lib/dataclasses.py @@ -1132,8 +1132,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 diff --git a/Lib/test/test_dataclasses/__init__.py b/Lib/test/test_dataclasses/__init__.py index 08c685ee756b..d78f22989646 100644 --- a/Lib/test/test_dataclasses/__init__.py +++ b/Lib/test/test_dataclasses/__init__.py @@ -3287,6 +3287,17 @@ class TestSlots(unittest.TestCase): 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. diff --git a/Misc/NEWS.d/next/Library/2023-06-16-19-17-06.gh-issue-105866.0NBveV.rst b/Misc/NEWS.d/next/Library/2023-06-16-19-17-06.gh-issue-105866.0NBveV.rst new file mode 100644 index 000000000000..28eae1232742 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-06-16-19-17-06.gh-issue-105866.0NBveV.rst @@ -0,0 +1 @@ +Fixed ``_get_slots`` bug which caused error when defining dataclasses with slots and a weakref_slot.