https://github.com/python/cpython/commit/a22d05f04c074dbb4f71e7837f54c0bb693db75d
commit: a22d05f04c074dbb4f71e7837f54c0bb693db75d
branch: main
author: Aviel Boag <avb...@gmail.com>
committer: carljm <c...@oddbird.net>
date: 2024-03-18T18:53:14-06:00
summary:

gh-105866: fix dataclass with slots=True, weakref_slot=True (#105870)

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Kirill Podoprigora <kirill.ba...@mail.ru>
Co-authored-by: Carl Meyer <c...@oddbird.net>

files:
A Misc/NEWS.d/next/Library/2023-06-16-19-17-06.gh-issue-105866.0NBveV.rst
M Lib/dataclasses.py
M Lib/test/test_dataclasses/__init__.py

diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py
index 45ce5a98b51ae0..e511eff4125038 100644
--- a/Lib/dataclasses.py
+++ b/Lib/dataclasses.py
@@ -1159,8 +1159,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 ede74b0dd15ccf..e27abac5111394 100644
--- a/Lib/test/test_dataclasses/__init__.py
+++ b/Lib/test/test_dataclasses/__init__.py
@@ -3498,6 +3498,17 @@ class A(Base):
         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 00000000000000..28eae1232742f7
--- /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.

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to