[New-bugs-announce] [issue35169] Improve error messages for assignment

Serhiy Storchaka report at bugs.python.org
Mon Nov 5 14:17:32 EST 2018


New submission from Serhiy Storchaka <storchaka+cpython at gmail.com>:

This is a follow up of issue34641.

>>> f(lambda x: x = 1)
  File "<stdin>", line 1
SyntaxError: lambda cannot contain assignment
>>> f(x.y = 1)
  File "<stdin>", line 1
SyntaxError: keyword can't be an expression

The error message "keyword can't be an expression" still looks confusing to me. This is because the term "keyword" is ambiguous. Usually it means reserved identifier like "if" or "def". Some keywords, like "None" and "True" can be expressions. Perhaps "keyword name can't be an expression" would be better.

But I think that in these cases it is most likely that "=" was used instead of "==". And it would be better to generalize the error message for lambdas and point on a possible typo.

>>> f(x.y = 1)
  File "<stdin>", line 1
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?

The proposed PR changes this error message. It makes also an error message for forbidden assignment more concrete: "cannot assign to __debug__" instead of "assignment to keyword" (the latter is actually incorrect, because __debug__ is not true keyword in tokenizer). This restores Python 2 error messages. Improved also other error messages for forbidden assigning: dict and set displays, f-string expressions are not literals.

----------
components: Interpreter Core
messages: 329313
nosy: benjamin.peterson, brett.cannon, gvanrossum, serhiy.storchaka, yselivanov
priority: normal
severity: normal
status: open
title: Improve error messages for assignment
type: enhancement
versions: Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35169>
_______________________________________


More information about the New-bugs-announce mailing list