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: Strange SyntaxError message / suggestions for "@x = 123"
Type: enhancement Stage: resolved
Components: Parser Versions: Python 3.10
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: aroberge, lys.nikolaou, pablogsal, quentel, schmave
Priority: normal Keywords:

Created on 2021-11-15 14:41 by quentel, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg406351 - (view) Author: Pierre Quentel (quentel) * Date: 2021-11-15 14:41
In CPython 3.10 :

Python 3.10.0 (tags/v3.10.0:b494f59, Oct  4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> @x = 123
  File "<stdin>", line 1
    @x = 123
     ^^^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

but both suggestions lead to a SyntaxError :

>>> @x == 123
...
  File "<stdin>", line 2

    ^
SyntaxError: invalid syntax
>>> @x := 123
...
  File "<stdin>", line 2

    ^
SyntaxError: invalid syntax
>>>


Maybe an error message such as "cannot assign to decorator" would be more appropriate ?
msg406392 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-11-16 09:23
I agree this is weird, but technically is not incorrect. For example, consider this:

>>> def foo(f):
...     return f
...

>>> @x = foo
  File "<stdin>", line 1
    @x = foo
     ^^^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

but with ':=' it works:

>>> @x := foo
... def g():
...   ...
...


Same for using '==' (although much work):

>>> def foo(f):
...     return f
...

>>> class A:
...   def __eq__(self, other):
...        return foo
...

>>> @A() = A()
  File "<stdin>", line 1
    @A() = A()
     ^^^
SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?

But it works with '==':

>>> @A() == A()
... def g():
...    ...
...
>>> g
<function g at 0x10d4f3c20>

This shows two things:

* Is technically syntactically valid, but semantically invalid. Notice that the suggestion is not false: the parser tells you that if you use '==' or ':=' it won't be a SyntaxError, and that is true.
* There are ridiculous cases when this can succeed, and actually some where it does even make sense (':=').

I am therefore closing this as 'not a bug', but please, feel free to comment back if you disagree :)
History
Date User Action Args
2022-04-11 14:59:52adminsetgithub: 89965
2021-11-16 09:23:24pablogsalsetstatus: open -> closed
resolution: not a bug
messages: + msg406392

stage: resolved
2021-11-15 20:23:05arobergesetnosy: + aroberge
2021-11-15 14:46:25schmavesetnosy: + schmave
2021-11-15 14:41:01quentelcreate