Skip to content

SyntaxError: parameter without a default follows parameter with a default is inaccurate #129545

@smheidrich

Description

@smheidrich

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

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions