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

gh-129545 Improve Syntax Error Message for Default Parameter Order #130937

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

sharktide
Copy link

@sharktide sharktide commented Mar 7, 2025

Fixes and closes #129545

Overview:

The SyntaxError: parameter without a default follows parameter with a default error message has been modified to SyntaxError: positional parameter without a default follows parameter with a default to clarify error messages.

As mentioned in #129545

@smheidrich

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.)

As also mentioned by @smheidrich:

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.

The new error message, should be accurate for most cases and should not be confusing. However, if there are cases where the original error message might be more appropriate ex when dealing with keyword-only parameters or other specific syntax rules but the new change would cause minimal confusion compared to the problem that this pr intends to solve.

@bedevere-app
Copy link

bedevere-app bot commented Mar 7, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@sharktide
Copy link
Author

note: will add news entry tmw

Copy link
Member

@lysnikolaou lysnikolaou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @sharktide! I'm good with this change. I've left one comment about a formatting issue.

Comment on lines 27 to 31
check_syntax_error(self, "def f(a, b = 5, /, c): pass", "positional parameter without a default follows parameter with a default ")
check_syntax_error(self, "def f(a = 5, b, /, c): pass", "positional parameter without a default follows parameter with a default ")
check_syntax_error(self, "def f(a = 5, b=1, /, c, *, d=2): pass", "positional parameter without a default follows parameter with a default ")
check_syntax_error(self, "def f(a = 5, b, /): pass", "positional parameter without a default follows parameter with a default ")
check_syntax_error(self, "def f(a, /, b = 5, c): pass", "positional parameter without a default follows parameter with a default ")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the additional trailing spaces here. They shouldn't be needed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lysnikolaou Done ready for re-review/merge

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lysnikolaou Does a small change need a news entry? I didn't know if to put one or not. Since you commented it looks good other than the formatting, I am assuming not, but if so, could you please apply the "skip news" label?
the bedevere-bot keeps notifying me of a missing news entry

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sharktide A news entry would be best here, since there's a user-facing change, so please add one.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add it under the topic c-api

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lysnikolaou Added the news entry, everything done

@bedevere-app
Copy link

bedevere-app bot commented Mar 7, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@sharktide sharktide requested a review from lysnikolaou March 7, 2025 13:01
@bedevere-app
Copy link

bedevere-app bot commented Mar 7, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Copy link
Member

@lysnikolaou lysnikolaou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One editorial comment on the news blurb. Also, a better category for it would be Core and Builtins. Maybe move it there?

@sharktide
Copy link
Author

One editorial comment on the news blurb. Also, a better category for it would be Core and Builtins. Maybe move it there?

ok

@sharktide
Copy link
Author

@lysnikolaou I moved the news entry to Core and Builtins as well as took your change suggestion

@sharktide sharktide requested a review from lysnikolaou March 7, 2025 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SyntaxError: parameter without a default follows parameter with a default is inaccurate
2 participants