Note
The content of this page is generated by running
trb_syntax_markdown.py located in the tests/
directory.
This needs to be done explicitly, independently of updating the
documentation using Sphinx.
Friendly version: 0.3.142
Python version: 3.8.10
---
## (1) Using 'and' in import statement
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\and_in_import_statement.py", line 1
from math import sin and cos
^
SyntaxError: invalid syntax
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\and_in_import_statement.py'
beyond the location indicated by ^.
-->1: from math import sin and cos
^
The Python keyword `and` can only be used for boolean expressions.
Perhaps you meant to write
`from math import sin , cos`
---
## (2) Using 'and' after comma in import statement
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\and_in_import_statement_2.py", line 1
from math import sin, tan, and cos
^
SyntaxError: invalid syntax
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\and_in_import_statement_2.py'
beyond the location indicated by ^.
-->1: from math import sin, tan, and cos
^
The Python keyword `and` can only be used for boolean expressions.
Perhaps you meant to write
`from math import sin, tan, cos`
---
## (3) Annotated name cannot be global
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\annotated_name_global.py", line 4
x:int = 1
^
SyntaxError: annotated name 'x' can't be global
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\annotated_name_global.py'
beyond the location indicated by ^.
1: # SyntaxError: annotated name 'x' can't be global
2: def foo():
3: global x
-->4: x:int = 1
^
The object named `x` is defined with type annotation
as a local variable. It cannot be declared to be a global variable.
---
## (4) Incorrect use of 'from module import ... as ...
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\as_instead_of_comma_in_import.py", line 2
from math import (sin, cos) as funcs
^
SyntaxError: invalid syntax
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\as_instead_of_comma_in_import.py'
beyond the location indicated by ^.
1: # issue 158
-->2: from math import (sin, cos) as funcs
^
I am guessing that you are trying to import at least one object
from module `math` and rename it using the Python keyword `as`;
this keyword can only be used to rename one object at a time
using a well defined syntax.
I suggest that you split up any such import statement with each object
renamed on a separate line as follows:
from math import object_1 as name_1
from math import object_2 as name_2 # if needed
---
## (5) Name assigned prior to global declaration
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\assign_name_before_global_1.py", line 7
global p
^
SyntaxError: name 'p' is assigned to before global declaration
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\assign_name_before_global_1.py'
beyond the location indicated by ^.
3:
4:
5: def fn():
6: p = 1
-->7: global p
^
You assigned a value to the variable `p`
before declaring it as a global variable.
---
## (6) Name used prior to global declaration
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\assign_name_before_global_2.py", line 7
global r
^
SyntaxError: name 'r' is used prior to global declaration
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\assign_name_before_global_2.py'
beyond the location indicated by ^.
3:
4:
5: def fn():
6: print(r)
-->7: global r
^
You used the variable `r`
before declaring it as a global variable.
---
## (7) Name used prior to nonlocal declaration
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\assign_name_before_nonlocal_1.py", line 9
nonlocal q
^
SyntaxError: name 'q' is used prior to nonlocal declaration
Did you forget to write `nonlocal` first?
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\assign_name_before_nonlocal_1.py'
beyond the location indicated by ^.
5: q = 1
6:
7: def g():
8: print(q)
--> 9: nonlocal q
^
You used the variable `q`
before declaring it as a nonlocal variable.
---
## (8) Name assigned prior to nonlocal declaration
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\assign_name_before_nonlocal_2.py", line 9
nonlocal s
^
SyntaxError: name 's' is assigned to before nonlocal declaration
Did you forget to add `nonlocal`?
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\assign_name_before_nonlocal_2.py'
beyond the location indicated by ^.
5: s = 1
6:
7: def g():
8: s = 2
--> 9: nonlocal s
^
You assigned a value to the variable `s`
before declaring it as a nonlocal variable.
---
## (9) Assign to conditional expression
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\assign_to_conditional.py", line 3
a if 1 else b = 1
^
SyntaxError: cannot assign to conditional expression
You can only assign objects to identifiers (variable names).
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\assign_to_conditional.py'
beyond the location indicated by ^.
1: """Should raise SyntaxError: can't [cannot] assign to conditional expression"""
2:
-->3: a if 1 else b = 1
^
On the left-hand side of an equal sign, you have a
conditional expression instead of the name of a variable.
A conditional expression has the following form:
variable = object if condition else other_object
---
## (10) Assignment to keyword (__debug__)
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\assign_to_debug.py", line 4
__debug__ = 1
^
SyntaxError: cannot assign to __debug__
You cannot assign a value to `__debug__`.
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\assign_to_debug.py'
beyond the location indicated by ^.
1: """Should raise SyntaxError: cannot assign to __debug__ in Py 3.8
2: and assignment to keyword before."""
3:
-->4: __debug__ = 1
^
`__debug__` is a constant in Python; you cannot assign it a different value.
---
## (11) Assignment to keyword (__debug__)
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\assign_to_debug2.py", line 4
a.__debug__ = 1
^
SyntaxError: cannot assign to __debug__
You cannot assign a value to `__debug__`.
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\assign_to_debug2.py'
beyond the location indicated by ^.
1: """Should raise SyntaxError: cannot assign to __debug__ in Py 3.8
2: and assignment to keyword before."""
3:
-->4: a.__debug__ = 1
^
`__debug__` is a constant in Python; you cannot assign it a different value.
---
## (12) Assignment to Ellipsis symbol
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\assign_to_ellipsis.py", line 4
... = 1
^
SyntaxError: cannot assign to Ellipsis
You cannot assign a value to the ellipsis symbol [`...`].
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\assign_to_ellipsis.py'
beyond the location indicated by ^.
1: """Should raise SyntaxError: cannot assign to Ellipsis in Py 3.8
2: and assignment to keyword before."""
3:
-->4: ... = 1
^
The ellipsis symbol `...` is a constant in Python;you cannot assign it a different value.
---
## (13) Cannot assign to f-string
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\assign_to_f_string.py", line 6
f'{x}' = 42
^
SyntaxError: cannot assign to f-string expression
You can only assign objects to identifiers (variable names).
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\assign_to_f_string.py'
beyond the location indicated by ^.
1: """Should raise
2: Python < 3.8: SyntaxError: can't assign to literal
3: Python >= 3.8: SyntaxError: cannot assign to f-string expression
4: """
5:
-->6: f'{x}' = 42
^
You wrote an expression that has the f-string `f'{x}'`
on the left-hand side of the equal sign.
An f-string should only appear on the right-hand side of an equal sign.
---
## (14) Cannot assign to function call: single = sign
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\assign_to_function_call_1.py", line 6
len('a') = 3
^
SyntaxError: cannot assign to function call
You can only assign objects to identifiers (variable names).
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\assign_to_function_call_1.py'
beyond the location indicated by ^.
1: """Should raise SyntaxError: can't assign to function call
2:
3: Python 3.8: SyntaxError: cannot assign to function call
4: """
5:
-->6: len('a') = 3
^
You wrote the expression
len('a') = 3
where `len('a')`, on the left-hand side of the equal sign, either is
or includes a function call and is not simply the name of a variable.
---
## (15) Cannot assign to function call: two = signs
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\assign_to_function_call_2.py", line 6
func(a, b=3) = 4
^
SyntaxError: cannot assign to function call
You can only assign objects to identifiers (variable names).
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\assign_to_function_call_2.py'
beyond the location indicated by ^.
1: """Should raise SyntaxError: can't assign to function call
2:
3: Python 3.8: SyntaxError: cannot assign to function call
4: """
5:
-->6: func(a, b=3) = 4
^
You wrote an expression like
func(...) = some value
where `func(...)`, on the left-hand side of the equal sign, is
a function call and not the name of a variable.
---
## (16) Assign to generator expression
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\assign_to_generator.py", line 3
(x for x in x) = 1
^
SyntaxError: cannot assign to generator expression
You can only assign objects to identifiers (variable names).
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\assign_to_generator.py'
beyond the location indicated by ^.
1: """Should raise SyntaxError: can't [cannot] assign to generator expression"""
2:
-->3: (x for x in x) = 1
^
On the left-hand side of an equal sign, you have a
generator expression instead of the name of a variable.
---
## (17) Cannot assign to literal - 4
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\assign_to_literal_dict.py", line 7
{1 : 2, 2 : 4} = 5
^
SyntaxError: cannot assign to dict display
You can only assign objects to identifiers (variable names).
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\assign_to_literal_dict.py'
beyond the location indicated by ^.
1: """Should raise SyntaxError:
2: Python 3.8: cannot assign to dict display
3: Python 3.6, 3.7: can't assign to literal
4:
5: """
6:
-->7: {1 : 2, 2 : 4} = 5
^
You wrote an expression like
{1 : 2, 2 : 4} = 5
where `{1 : 2, 2 : 4}`, on the left-hand side of the equal sign,
is or includes an actual object of type `dict`
and is not simply the name of a variable.
---
## (18) Cannot assign to literal int
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\assign_to_literal_int.py", line 3
1 = a
^
SyntaxError: cannot assign to literal
Perhaps you meant to write `a = 1`
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\assign_to_literal_int.py'
beyond the location indicated by ^.
1: """Should raise SyntaxError: can't assign to literal"""
2:
-->3: 1 = a
^
You wrote an expression like
1 = a
where `1`, on the left-hand side of the equal sign,
is or includes an actual object of type `int`
and is not simply the name of a variable.
Perhaps you meant to write:
a = 1
---
## (19) Cannot assign to literal int - 2
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\assign_to_literal_int_2.py", line 3
1 = 2
^
SyntaxError: cannot assign to literal
You can only assign objects to identifiers (variable names).
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\assign_to_literal_int_2.py'
beyond the location indicated by ^.
1: """Should raise SyntaxError: can't assign to literal"""
2:
-->3: 1 = 2
^
You wrote an expression like
1 = 2
where `1`, on the left-hand side of the equal sign,
is or includes an actual object of type `int`
and is not simply the name of a variable.
---
## (20) Cannot assign to literal - 5
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\assign_to_literal_int_3.py", line 4
1 = a = b
^
SyntaxError: cannot assign to literal
You can only assign objects to identifiers (variable names).
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\assign_to_literal_int_3.py'
beyond the location indicated by ^.
1: """Should raise SyntaxError: can't assign to literal
2: or (Python 3.8) cannot assign to literal"""
3:
-->4: 1 = a = b
^
You wrote an expression like
... = variable_name
where `...`, on the left-hand side of the equal sign,
is or includes an actual object
and is not simply the name of a variable.
---
## (21) Cannot assign to literal - 3
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\assign_to_literal_set.py", line 7
{1, 2, 3} = 4
^
SyntaxError: cannot assign to set display
You can only assign objects to identifiers (variable names).
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\assign_to_literal_set.py'
beyond the location indicated by ^.
1: """Should raise SyntaxError:
2: Python 3.8: cannot assign to set display
3: Python 3.6, 3.7: can't assign to literal
4:
5: """
6:
-->7: {1, 2, 3} = 4
^
You wrote an expression like
{1, 2, 3} = 4
where `{1, 2, 3}`, on the left-hand side of the equal sign,
is or includes an actual object of type `set`
and is not simply the name of a variable.
---
## (22) Assign to keyword def
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\assign_to_keyword_def.py", line 3
def = 2
^
SyntaxError: invalid syntax
Python keywords cannot be used as identifiers (variable names).
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\assign_to_keyword_def.py'
beyond the location indicated by ^.
1: """ Should raise SyntaxError"""
2:
-->3: def = 2
^
You were trying to assign a value to the Python keyword `def`.
This is not allowed.
---
## (23) Assign to keyword else
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\assign_to_keyword_else.py", line 3
else = 1
^
SyntaxError: invalid syntax
Python keywords cannot be used as identifiers (variable names).
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\assign_to_keyword_else.py'
beyond the location indicated by ^.
1: """ Should raise SyntaxError"""
2:
-->3: else = 1
^
You were trying to assign a value to the Python keyword `else`.
This is not allowed.
---
## (24) Assignment to keyword (None)
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\assign_to_keyword_none.py", line 4
None = 1
^
SyntaxError: cannot assign to None
You cannot assign a value to `None`.
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\assign_to_keyword_none.py'
beyond the location indicated by ^.
1: """Should raise SyntaxError: cannot assign to None in Py 3.8
2: and can't assign to keyword before."""
3:
-->4: None = 1
^
`None` is a constant in Python; you cannot assign it a different value.
---
## (25) Assign to math operation
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\assign_to_operation.py", line 4
a + 1 = 2
^
SyntaxError: cannot assign to operator
Perhaps you needed `==` instead of `=`.
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\assign_to_operation.py'
beyond the location indicated by ^.
1: """Should raise SyntaxError: can't assign to operator
2: or (Python 3.8) cannot assign to operator"""
3:
-->4: a + 1 = 2
^
You wrote an expression that includes some mathematical operations
on the left-hand side of the equal sign which should be
only used to assign a value to a variable.
---
## (26) def: missing parentheses
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\async_def_missing_parens.py", line 1
async def name:
^
SyntaxError: invalid syntax
Did you forget parentheses?
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\async_def_missing_parens.py'
beyond the location indicated by ^.
-->1: async def name:
^
Perhaps you forgot to include parentheses.
You might have meant to write
async def name():
---
## (27) Augmented assignment to literal
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\augmented_assignment_to_literal.py", line 1
if "word" := True:
^
SyntaxError: cannot use assignment expressions with literal
You can only assign objects to identifiers (variable names).
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\augmented_assignment_to_literal.py'
beyond the location indicated by ^.
-->1: if "word" := True:
^
You cannot use the augmented assignment operator `:=`,
sometimes called the walrus operator, with literals like `"word"`.
You can only assign objects to identifiers (variable names).
---
## (28) Walrus/Named assignment depending on Python version
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\augmented_assigment_with_true.py", line 4
(True := 1)
^
SyntaxError: cannot use assignment expressions with True
You cannot assign a value to `True`.
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\augmented_assigment_with_true.py'
beyond the location indicated by ^.
1: """Should raise SyntaxError: invalid syntax
2: or (Python 3.8) cannot use named assignment with True"""
3:
-->4: (True := 1)
^
`True` is a constant in Python; you cannot assign it a different value.
---
## (29) Backslash instead of slash
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\backslash_instead_of_slash.py", line 1
a = 3 \ 4.0
^
SyntaxError: unexpected character after line continuation character
Did you mean to divide by 4.0?
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\backslash_instead_of_slash.py'
beyond the location indicated by ^.
-->1: a = 3 \ 4.0
^
You are using the continuation character `\` outside of a string,
and it is followed by some other character(s).
I am guessing that you wanted to divide by the number 4.0
and wrote \ instead of /.
---
## (30) break outside loop
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\break_outside_loop.py", line 4
break
^
SyntaxError: 'break' outside loop
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\break_outside_loop.py'
beyond the location indicated by ^.
1: """Should raise SyntaxError: 'break' outside loop"""
2:
3: if True:
-->4: break
^
The Python keyword `break` can only be used inside a `for` loop or inside a `while` loop.
---
## (31) Cannot guess the cause
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\cannot_guess_the_cause.py", line 1
SyntaxErrors can be annoying!
^
SyntaxError: invalid syntax
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\cannot_guess_the_cause.py'
beyond the location indicated by ^.
-->1: SyntaxErrors can be annoying!
^
Currently, I cannot guess the likely cause of this error.
Try to examine closely the line indicated as well as the line
immediately above to see if you can identify some misspelled
word, or missing symbols, like (, ), [, ], :, etc.
Unless your code uses type annotations, which are beyond our scope,
if you think that this is something which should be handled
by friendly, please report this case to
https://github.com/aroberge/friendly/issues
---
## (32) Cannot use star operator
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\syntax\cannot_use_star.py", line 3
*a
^
SyntaxError: can't use starred expression here
A `SyntaxError` occurs when Python cannot understand your code.
Python could not understand the code in the file
'TESTS:\syntax\cannot_use_star.py'
beyond the location indicated by ^.
1: """Should raise SyntaxError: can't use starred expression here"""
2:
-->3: *a
^
The star operator `*` is interpreted to mean that
iterable unpacking is to be used to assign a name
to each item of an iterable, which does not make sense here.
---
## (33) Cannot use double star operator
Traceback (most recent call last):
File "TESTS:\trb_syntax_common.py", line 49, in create_tracebacks
__import__(name)
File "TESTS:\