mirror of https://github.com/python/cpython.git
gh-107838: In dataclasses, improve error message when a non-default field follows a default field. (gh-107842)
Add the name of the previous default argument field in an error message.
This commit is contained in:
parent
37d8b904f8
commit
e4275f4df3
|
@ -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
|
# message, and future-proofs us in case we build up the function
|
||||||
# using ast.
|
# using ast.
|
||||||
|
|
||||||
seen_default = False
|
seen_default = None
|
||||||
for f in std_fields:
|
for f in std_fields:
|
||||||
# Only consider the non-kw-only fields in the __init__ call.
|
# Only consider the non-kw-only fields in the __init__ call.
|
||||||
if f.init:
|
if f.init:
|
||||||
if not (f.default is MISSING and f.default_factory is MISSING):
|
if not (f.default is MISSING and f.default_factory is MISSING):
|
||||||
seen_default = True
|
seen_default = f
|
||||||
elif seen_default:
|
elif seen_default:
|
||||||
raise TypeError(f'non-default argument {f.name!r} '
|
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 = {f'__dataclass_type_{f.name}__': f.type for f in fields}
|
||||||
locals.update({
|
locals.update({
|
||||||
|
|
|
@ -134,7 +134,7 @@ class C:
|
||||||
# Non-defaults following defaults.
|
# Non-defaults following defaults.
|
||||||
with self.assertRaisesRegex(TypeError,
|
with self.assertRaisesRegex(TypeError,
|
||||||
"non-default argument 'y' follows "
|
"non-default argument 'y' follows "
|
||||||
"default argument"):
|
"default argument 'x'"):
|
||||||
@dataclass
|
@dataclass
|
||||||
class C:
|
class C:
|
||||||
x: int = 0
|
x: int = 0
|
||||||
|
@ -143,7 +143,7 @@ class C:
|
||||||
# A derived class adds a non-default field after a default one.
|
# A derived class adds a non-default field after a default one.
|
||||||
with self.assertRaisesRegex(TypeError,
|
with self.assertRaisesRegex(TypeError,
|
||||||
"non-default argument 'y' follows "
|
"non-default argument 'y' follows "
|
||||||
"default argument"):
|
"default argument 'x'"):
|
||||||
@dataclass
|
@dataclass
|
||||||
class B:
|
class B:
|
||||||
x: int = 0
|
x: int = 0
|
||||||
|
@ -156,7 +156,7 @@ class C(B):
|
||||||
# a field which didn't use to have a default.
|
# a field which didn't use to have a default.
|
||||||
with self.assertRaisesRegex(TypeError,
|
with self.assertRaisesRegex(TypeError,
|
||||||
"non-default argument 'y' follows "
|
"non-default argument 'y' follows "
|
||||||
"default argument"):
|
"default argument 'x'"):
|
||||||
@dataclass
|
@dataclass
|
||||||
class B:
|
class B:
|
||||||
x: int
|
x: int
|
||||||
|
@ -4521,7 +4521,7 @@ class A:
|
||||||
|
|
||||||
# Make sure we still check for non-kwarg non-defaults not following
|
# Make sure we still check for non-kwarg non-defaults not following
|
||||||
# defaults.
|
# 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):
|
with self.assertRaisesRegex(TypeError, err_regex):
|
||||||
@dataclass
|
@dataclass
|
||||||
class A:
|
class A:
|
||||||
|
|
Loading…
Reference in New Issue