-
-
Notifications
You must be signed in to change notification settings - Fork 29.2k
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
Keyword can't be an expression? #75041
Comments
Look at this (from https://www.quora.com/Is-end1-a-keyword-in-Python-3-6-1): print(end1 + end2 + end3 + end4 + end5 + end6 + end=' ')
^
SyntaxError: keyword can't be an expression Wouldn't it be better if the message said "keyword for an argument must be a simple name"? Or something like that. Newbies, when they think of keywords, they think something from keyword.kwlist, not something used to pass arguments to a function in a particular way. |
I think the current error message is more informative than your suggestion, myself. However, the message could say "keyword argument name" instead of just "keyword", which I think would be quite a bit clearer. I seem to remember another discussion where I suggested disambiguating "keyword" by using "keyword argument name", but I have no idea how to phrase a search to find that issue :( |
I agree it's more _correct_. But it's also more confusing. As far as I can tell, when writing error messages, we tend to minimize confusion, not maximize correctness. Of course, it would be great to have both. Your "keyword argument name" seems pretty good. |
If I saw your message, I would think "what is a 'simple name'?". There's no glossary entry for that, nor is it a concept used elsewhere in the documentation as far as I remember. One could instead use "single identifier", but the problem with both of those is that "end" *is* a simple name/single identifier. The error isn't that an identifier was not used, the error is that an expression was used. We unfortunately can't read the mind of the programmer to know that the *actual* error was using a '+' instead of a ','. |
FWIW, I prefer the current error message and my students don't seem to have issues with it. No matter what wording we put in, someone will always find a way to misread it, in part because the "end6 + end=' '" example isn't a simple mistake, it reflects an incorrect mental model that isn't easily fixed by an error message. |
I just ran into this, and found the existing error message *incredibly* confusing. My immediate reaction was "There's no keyword in that line, what are you complaining about?". An error message that said "Keyword argument name must be an identifier" would have been *far* more useful, and far less confusing. |
The current error message is also outright incorrect, since simple names *are* valid expressions - the actual problem being reported is that binary expressions (veky's case) and strings (my case) *aren't* identifiers. |
Nick, thanks for validating my pain. :-| In the meantime, I found out Python already knows more finely what kind of expression something is, and uses that knowledge in the error messages. Look: >>> a + b = 4
SyntaxError: can't assign to operator
>>> a(b) = 4
SyntaxError: can't assign to function call
>>> 'a' = 4
SyntaxError: can't assign to literal So, how about "keyword argument name cannot be an operator / function call / literal"? (Of course, if you ask me, "operator" is also unfortunate, but at least there is a precedent.) |
I get this now on 3.10: >>> print(end1 + end2 + end3 + end4 + end5 + end6 + end=' ')
File "<stdin>", line 1
print(end1 + end2 + end3 + end4 + end5 + end6 + end=' ')
^
SyntaxError: expression cannot contain assignment, perhaps you meant "=="? |
I like the current error message. What bugs me a little is the position of the caret, which I think is a bit misleading. Guido, Pablo, should we move the caret to point at the |
+1 for the |
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: