-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
"SyntaxError: non-default argument follows default argument" confuses #91210
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
Comments
Running the code below will produce a SyntaxError with the message: "non-default argument follows default argument". def test(a=None, b):
return b if a is None else a The issue is that This change appears simple enough and although it is not something I've found to be troublesome, it will help users to use correct keywords (arguments vs. parameters) when searching on the internet. |
The problem is more subtle. The thing is, "default parameter" doesn't make sense in this context. Yes, a and b are parameter, but a is not "default parameter" in any sensible way. It is _None_ which is the default argument for parameter a, while parameter b has no default argument. In other words, if the function weren't called with a first argument, the default argument would be used. So, the fix should be a bit more complicated. Maybe, "a parameter without a default follows a parameter with a default"? |
Yes I agree, that would be the best. |
Current message same in 3.11 and 3.9, but I am not sure about backporting to old parser. However merges a change, if any, can decide. |
In addition to this, we fall back to the generic >>> def test(a=None, b):
File "<stdin>", line 1
def test(a=None, b):
^
SyntaxError: non-default argument follows default argument
>>> def test(a, /, b=None, c):
File "<stdin>", line 1
def test(a, /, b=None, c):
^
SyntaxError: invalid syntax @gpshead you still intend to work on this? I can open a PR for it in case you won't. |
I've lost track of why I assigned this to myself (I didn't leave myself a comment? tsk tsk), so no, feel free to take it! |
…default - Improve error message when parameter without a default follows one with a default - Show same error message when positional-only params precede the default/non-default sequence
GH-95933) - Improve error message when parameter without a default follows one with a default - Show same error message when positional-only params precede the default/non-default sequence
Thanks, looks like this has been fixed :-) |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: