[Python-checkins] bpo-43176: Fix processing of empty dataclasses (GH-24484) (GH-25205)

ericvsmith webhook-mailer at python.org
Tue Apr 6 09:46:38 EDT 2021


https://github.com/python/cpython/commit/8a34a0793bcb830350dac675524310bb285e5e4f
commit: 8a34a0793bcb830350dac675524310bb285e5e4f
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: ericvsmith <ericvsmith at users.noreply.github.com>
date: 2021-04-06T09:46:30-04:00
summary:

bpo-43176: Fix processing of empty dataclasses (GH-24484) (GH-25205)

When a dataclass inherits from an empty base, all immutability checks are omitted. This PR fixes this and adds tests for it.

Automerge-Triggered-By: GH:ericvsmith
(cherry picked from commit 376ffc6ac491da74920aed1b8e35bc371cb766ac)

Co-authored-by: Iurii Kemaev <6885137+hbq1 at users.noreply.github.com>

Co-authored-by: Iurii Kemaev <6885137+hbq1 at users.noreply.github.com>

files:
A Misc/NEWS.d/next/Library/2021-02-09-07-24-29.bpo-43176.bocNQn.rst
M Lib/dataclasses.py
M Lib/test/test_dataclasses.py

diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py
index 21c7e0c9e2871..e015254df78b4 100644
--- a/Lib/dataclasses.py
+++ b/Lib/dataclasses.py
@@ -836,7 +836,7 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen):
         # Only process classes that have been processed by our
         # decorator.  That is, they have a _FIELDS attribute.
         base_fields = getattr(b, _FIELDS, None)
-        if base_fields:
+        if base_fields is not None:
             has_dataclass_bases = True
             for f in base_fields.values():
                 fields[f.name] = f
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py
index 31caae9d2f393..e65db176d9d34 100644
--- a/Lib/test/test_dataclasses.py
+++ b/Lib/test/test_dataclasses.py
@@ -2568,6 +2568,30 @@ class D(C):
         self.assertEqual(d.i, 0)
         self.assertEqual(d.j, 10)
 
+    def test_inherit_nonfrozen_from_empty_frozen(self):
+        @dataclass(frozen=True)
+        class C:
+            pass
+
+        with self.assertRaisesRegex(TypeError,
+                                    'cannot inherit non-frozen dataclass from a frozen one'):
+            @dataclass
+            class D(C):
+                j: int
+
+    def test_inherit_nonfrozen_from_empty(self):
+        @dataclass
+        class C:
+            pass
+
+        @dataclass
+        class D(C):
+            j: int
+
+        d = D(3)
+        self.assertEqual(d.j, 3)
+        self.assertIsInstance(d, C)
+
     # Test both ways: with an intermediate normal (non-dataclass)
     #  class and without an intermediate class.
     def test_inherit_nonfrozen_from_frozen(self):
diff --git a/Misc/NEWS.d/next/Library/2021-02-09-07-24-29.bpo-43176.bocNQn.rst b/Misc/NEWS.d/next/Library/2021-02-09-07-24-29.bpo-43176.bocNQn.rst
new file mode 100644
index 0000000000000..66a175d21255c
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-02-09-07-24-29.bpo-43176.bocNQn.rst
@@ -0,0 +1 @@
+Fixed processing of empty dataclasses.
\ No newline at end of file



More information about the Python-checkins mailing list