From 41c5a827e0ed2de3a75d460a67ad3b0dff90aa4d Mon Sep 17 00:00:00 2001 From: Aviel Boag Date: Fri, 16 Jun 2023 22:02:11 +0300 Subject: [PATCH 1/7] Fixed _get_slots bug. --- Lib/dataclasses.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py index e766a7b554afe1..c3d6fb35df90ae 100644 --- a/Lib/dataclasses.py +++ b/Lib/dataclasses.py @@ -1172,8 +1172,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 From 14ee9ddbf0c68d717459da3272150614dca012d7 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 16 Jun 2023 19:17:07 +0000 Subject: [PATCH 2/7] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-06-16-19-17-06.gh-issue-105866.0NBveV.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-06-16-19-17-06.gh-issue-105866.0NBveV.rst 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..99c993d51251fd --- /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. From 4e831937faa472ebcd7240a2d98d6d58dcdf06f4 Mon Sep 17 00:00:00 2001 From: Aviel Boag Date: Fri, 16 Jun 2023 23:02:04 +0300 Subject: [PATCH 3/7] Update Misc/NEWS.d/next/Library/2023-06-16-19-17-06.gh-issue-105866.0NBveV.rst Co-authored-by: Kirill Podoprigora --- .../next/Library/2023-06-16-19-17-06.gh-issue-105866.0NBveV.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 99c993d51251fd..28eae1232742f7 100644 --- 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 @@ -1 +1 @@ -Fixed `_get_slots` bug which caused error when defining dataclasses with slots and a weakref_slot. +Fixed ``_get_slots`` bug which caused error when defining dataclasses with slots and a weakref_slot. From 2e67cb5f8c5b75a93f25cb1f1a70590fff309a9b Mon Sep 17 00:00:00 2001 From: Aviel Boag Date: Sat, 17 Jun 2023 00:38:26 +0300 Subject: [PATCH 4/7] Added test case --- Lib/test/test_dataclasses.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index 6669f1c57e2e78..3d10757dc1a117 100644 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -4542,6 +4542,16 @@ def test_make_dataclass(self): self.assertTrue(fields(B)[0].kw_only) self.assertFalse(fields(B)[1].kw_only) + def test_dataclass_derived_slots(self): + class A: + pass + + @dataclass(slots=True, weakref_slot=True) + class B(A): + pass + + B() + if __name__ == '__main__': unittest.main() From 184f74c1f12da6548e4939af9fd2191dd1bf6476 Mon Sep 17 00:00:00 2001 From: Aviel Boag Date: Sat, 17 Jun 2023 00:41:46 +0300 Subject: [PATCH 5/7] Formatting fix. --- Lib/test/test_dataclasses.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index 3d10757dc1a117..46ce5f5e982f9c 100644 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -4545,11 +4545,11 @@ def test_make_dataclass(self): def test_dataclass_derived_slots(self): class A: pass - + @dataclass(slots=True, weakref_slot=True) class B(A): pass - + B() From 59687e7a3a9586a08b6989f92add799ed819d4ed Mon Sep 17 00:00:00 2001 From: Aviel Boag Date: Sat, 17 Jun 2023 00:44:39 +0300 Subject: [PATCH 6/7] Fix name --- Lib/test/test_dataclasses.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index 46ce5f5e982f9c..0ac2dc21f6342c 100644 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -4542,7 +4542,7 @@ def test_make_dataclass(self): self.assertTrue(fields(B)[0].kw_only) self.assertFalse(fields(B)[1].kw_only) - def test_dataclass_derived_slots(self): + def test_dataclass_derived_weakref_slot(self): class A: pass From f85761d7c010a39bb2c30a29e1093893f3d0ca22 Mon Sep 17 00:00:00 2001 From: Aviel Boag Date: Sat, 17 Jun 2023 01:26:09 +0300 Subject: [PATCH 7/7] Moved test to a more appropriate location. --- Lib/test/test_dataclasses.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index 0ac2dc21f6342c..1e969b7d09a8e7 100644 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -3400,6 +3400,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. @@ -4542,16 +4553,6 @@ def test_make_dataclass(self): self.assertTrue(fields(B)[0].kw_only) self.assertFalse(fields(B)[1].kw_only) - def test_dataclass_derived_weakref_slot(self): - class A: - pass - - @dataclass(slots=True, weakref_slot=True) - class B(A): - pass - - B() - if __name__ == '__main__': unittest.main()