-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Description
Bug report
Bug description:
The problem
Trying to define a function in which a positional parameter follows a parameter with a default value, we get a SyntaxError
as expected:
>>> def f(a=1, b):
File "<python-input-0>", line 1
def f(a=1, b):
^
SyntaxError: parameter without a default follows parameter with a default
But the message that comes with it is not as accurate as it could be, as we can easily construct a valid function signature in which a parameter without a default follows a parameter with a default so long as both are keyword-only parameters:
>>> def f(*, a=1, b):
... pass
...
>>>
(I didn't use a=1, *, b
for this counterexample because then one could argue that, if "follows" is interpreted as "follows immediately", we did resolve the situation described in the error message, making it formally accurate, albeit confusing.)
Proposed fix
So, in my opinion, the message should be changed to something more like positional parameter without a default follows parameter with a default
, reflecting the true nature of the problem.
Other benefits
It's not just a matter of accuracy for accuracy's sake, however: This change would also hint to people who don't know or have forgotten about keyword-only parameters that there is another possibility of resolving the issue than the ones implied by the current message.
Additional context
- As a result of "SyntaxError: non-default argument follows default argument" confuses #91210, the error message was changed from
non-default argument follows default argument
to the one above starting in Python 3.12 (via gh-91210: Improve error message when non-default param follows default #95933). So there is precedent for changing this message to make it more accurate. - Keyword-only parameters were introduced in Python 3.0 with PEP 3102, which makes me think I must be missing some obvious reason why their consequences can't be reflected in the error message.
dataclasses
has an exception message with the same issue, but as that one also still has the "argument" vs "parameter" issue mentioned above, it's probably appropriate to create a separate ticket for that.
CPython versions tested on:
3.14
Operating systems tested on:
Linux