This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Keyword can't be an expression?
Type: Stage: resolved
Components: Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: gvanrossum, iritkatriel, lys.nikolaou, ncoghlan, pablogsal, r.david.murray, rhettinger, serhiy.storchaka, veky
Priority: normal Keywords: patch

Created on 2017-07-05 17:21 by veky, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 23753 merged pablogsal, 2020-12-13 15:22
Messages (13)
msg297772 - (view) Author: Vedran Čačić (veky) * Date: 2017-07-05 17:21
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.
msg297798 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-07-06 04:10
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 :(
msg297800 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-07-06 04:21
Issue29951.
msg297805 - (view) Author: Vedran Čačić (veky) * Date: 2017-07-06 05:23
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.
msg297828 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-07-06 13:46
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 ','.
msg297899 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-07-07 17:52
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.
msg324138 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2018-08-27 01:42
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.
msg324139 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2018-08-27 01:46
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.
msg324168 - (view) Author: Vedran Čačić (veky) * Date: 2018-08-27 11:03
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.)
msg382547 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-12-04 23:37
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 "=="?
msg382918 - (view) Author: Lysandros Nikolaou (lys.nikolaou) * (Python committer) Date: 2020-12-12 18:10
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 `=` sign or whatever comes after it? I think I prefer the former.
msg382933 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-12-13 15:20
+1 for the `=` sign
msg382935 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-12-13 16:47
New changeset 43c4fb6c90c013a00cb820cb61e4990cd8ec7f5e by Pablo Galindo in branch 'master':
bpo-30858: Improve error location for expressions with assignments (GH-23753)
https://github.com/python/cpython/commit/43c4fb6c90c013a00cb820cb61e4990cd8ec7f5e
History
Date User Action Args
2022-04-11 14:58:48adminsetgithub: 75041
2020-12-13 16:47:04pablogsalsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-12-13 16:47:00pablogsalsetmessages: + msg382935
2020-12-13 15:22:27pablogsalsetkeywords: + patch
stage: patch review
pull_requests: + pull_request22610
2020-12-13 15:20:08pablogsalsetmessages: + msg382933
2020-12-12 18:10:07lys.nikolaousetnosy: + gvanrossum, lys.nikolaou, pablogsal
messages: + msg382918
2020-12-04 23:37:31iritkatrielsetnosy: + iritkatriel
messages: + msg382547
2018-08-27 11:03:32vekysetmessages: + msg324168
2018-08-27 01:46:49ncoghlansetmessages: + msg324139
2018-08-27 01:42:32ncoghlansetnosy: + ncoghlan
messages: + msg324138
2017-07-07 17:52:52rhettingersetnosy: + rhettinger
messages: + msg297899
2017-07-06 13:46:12r.david.murraysetmessages: + msg297828
2017-07-06 05:23:44vekysetmessages: + msg297805
2017-07-06 04:21:37serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg297800
2017-07-06 04:10:59r.david.murraysetnosy: + r.david.murray
messages: + msg297798
2017-07-05 17:21:20vekycreate