]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-107838: In dataclasses, improve error message when a non-default field follows...
authorEric V. Smith <ericvsmith@users.noreply.github.com>
Thu, 10 Aug 2023 14:39:13 +0000 (09:39 -0500)
committerGitHub <noreply@github.com>
Thu, 10 Aug 2023 14:39:13 +0000 (10:39 -0400)
Add the name of the previous default argument field in an error message.

Lib/dataclasses.py
Lib/test/test_dataclasses.py

index 3c72c289e438822df2bf13dd1586529db9e2d971..21f3fa5c213f1f6a38adf2dc4edc4821bdfce975 100644 (file)
@@ -575,15 +575,15 @@ def _init_fn(fields, std_fields, kw_only_fields, frozen, has_post_init,
     # message, and future-proofs us in case we build up the function
     # using ast.
 
-    seen_default = False
+    seen_default = None
     for f in std_fields:
         # Only consider the non-kw-only fields in the __init__ call.
         if f.init:
             if not (f.default is MISSING and f.default_factory is MISSING):
-                seen_default = True
+                seen_default = f
             elif seen_default:
                 raise TypeError(f'non-default argument {f.name!r} '
-                                'follows default argument')
+                                f'follows default argument {seen_default.name!r}')
 
     locals = {f'__dataclass_type_{f.name}__': f.type for f in fields}
     locals.update({
index 6669f1c57e2e784f1eaa87e085d461d95fbf59eb..bd8d82438414e629434ead784c753f813b751ee5 100644 (file)
@@ -134,7 +134,7 @@ class TestCase(unittest.TestCase):
         # Non-defaults following defaults.
         with self.assertRaisesRegex(TypeError,
                                     "non-default argument 'y' follows "
-                                    "default argument"):
+                                    "default argument 'x'"):
             @dataclass
             class C:
                 x: int = 0
@@ -143,7 +143,7 @@ class TestCase(unittest.TestCase):
         # A derived class adds a non-default field after a default one.
         with self.assertRaisesRegex(TypeError,
                                     "non-default argument 'y' follows "
-                                    "default argument"):
+                                    "default argument 'x'"):
             @dataclass
             class B:
                 x: int = 0
@@ -156,7 +156,7 @@ class TestCase(unittest.TestCase):
         #  a field which didn't use to have a default.
         with self.assertRaisesRegex(TypeError,
                                     "non-default argument 'y' follows "
-                                    "default argument"):
+                                    "default argument 'x'"):
             @dataclass
             class B:
                 x: int
@@ -4521,7 +4521,7 @@ class TestKeywordArgs(unittest.TestCase):
 
         # Make sure we still check for non-kwarg non-defaults not following
         # defaults.
-        err_regex = "non-default argument 'z' follows default argument"
+        err_regex = "non-default argument 'z' follows default argument 'a'"
         with self.assertRaisesRegex(TypeError, err_regex):
             @dataclass
             class A: