Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
smheidrich opened this issue Feb 1, 2025 · 2 comments · May be fixed by #130937
Open

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

smheidrich opened this issue Feb 1, 2025 · 2 comments · May be fixed by #130937
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@smheidrich
Copy link
Contributor

smheidrich commented Feb 1, 2025

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

@smheidrich smheidrich added the type-bug An unexpected behavior, bug, or error label Feb 1, 2025
@terryjreedy terryjreedy added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement and removed type-bug An unexpected behavior, bug, or error labels Feb 1, 2025
@sharktide
Copy link

I'll work on it

sharktide added a commit to sharktide/cpython that referenced this issue Mar 7, 2025
@sharktide
Copy link

Finished the code and tested, will make a pr when I have time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants