A minimal reproducible example for this:
>>> if True: True: print('x')
...
File "<stdin>", line 1
SyntaxError: illegal target for annotation
The problem is that the second True
was intended to be another if
condition, but is instead just an expression. The parser treats : print('x')
as a type annotation for True
; but type annotations have to be applied to individual variable names, not expressions.
The specific error is only possible because print('x')
is an expression, and thus a valid type annotation. If it were another kind of statement, this would cause a generic syntax error:
>>> if True: True: pass
File "<stdin>", line 1
if True: True: pass
^
SyntaxError: invalid syntax
Either way, there was a typo here - the second True:
was meant to be if True:
. However, this does not fix the problem by itself:
>>> if True: if True: pass
File "<stdin>", line 1
if True: if True: pass
^
SyntaxError: invalid syntax
It is not permitted to put multiple if
s on the same line; nor can elif
and else
be paired with the matching if
on the same line:
>>> if True: pass; else: pass
File "<stdin>", line 1
if True: pass; else: pass
^
SyntaxError: invalid syntax
Putting multiple statements on a line - by using semicolons, and/or by putting the "suite" of an if
etc. statement on the same line - is strongly discouraged in Python. It has multiple limitations, and makes the code much harder to read.
To make the example work, use indented blocks in the normal way:
if True:
if True:
pass