mirror of
https://github.com/python/cpython.git
synced 2025-08-28 04:35:02 +00:00
bpo-35169: Improve error messages for forbidden assignments. (GH-10342)
This commit is contained in:
parent
6c48bf2d9e
commit
97f1efb606
6 changed files with 140 additions and 96 deletions
|
@ -33,35 +33,55 @@ SyntaxError: invalid syntax
|
|||
|
||||
>>> None = 1
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: can't assign to keyword
|
||||
SyntaxError: cannot assign to None
|
||||
|
||||
>>> obj.True = 1
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: invalid syntax
|
||||
|
||||
>>> True = 1
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: cannot assign to True
|
||||
|
||||
>>> obj.__debug__ = 1
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: cannot assign to __debug__
|
||||
|
||||
>>> __debug__ = 1
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: cannot assign to __debug__
|
||||
|
||||
>>> f() = 1
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: can't assign to function call
|
||||
SyntaxError: cannot assign to function call
|
||||
|
||||
>>> del f()
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: can't delete function call
|
||||
SyntaxError: cannot delete function call
|
||||
|
||||
>>> a + 1 = 2
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: can't assign to operator
|
||||
SyntaxError: cannot assign to operator
|
||||
|
||||
>>> (x for x in x) = 1
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: can't assign to generator expression
|
||||
SyntaxError: cannot assign to generator expression
|
||||
|
||||
>>> 1 = 1
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: can't assign to literal
|
||||
SyntaxError: cannot assign to literal
|
||||
|
||||
>>> "abc" = 1
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: can't assign to literal
|
||||
SyntaxError: cannot assign to literal
|
||||
|
||||
>>> b"" = 1
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: can't assign to literal
|
||||
SyntaxError: cannot assign to literal
|
||||
|
||||
>>> ... = 1
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: cannot assign to Ellipsis
|
||||
|
||||
>>> `1` = 1
|
||||
Traceback (most recent call last):
|
||||
|
@ -74,15 +94,31 @@ them.
|
|||
|
||||
>>> (a, "b", c) = (1, 2, 3)
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: can't assign to literal
|
||||
SyntaxError: cannot assign to literal
|
||||
|
||||
>>> (a, True, c) = (1, 2, 3)
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: cannot assign to True
|
||||
|
||||
>>> (a, __debug__, c) = (1, 2, 3)
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: cannot assign to __debug__
|
||||
|
||||
>>> (a, *True, c) = (1, 2, 3)
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: cannot assign to True
|
||||
|
||||
>>> (a, *__debug__, c) = (1, 2, 3)
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: cannot assign to __debug__
|
||||
|
||||
>>> [a, b, c + 1] = [1, 2, 3]
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: can't assign to operator
|
||||
SyntaxError: cannot assign to operator
|
||||
|
||||
>>> a if 1 else b = 1
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: can't assign to conditional expression
|
||||
SyntaxError: cannot assign to conditional expression
|
||||
|
||||
From compiler_complex_args():
|
||||
|
||||
|
@ -255,36 +291,45 @@ SyntaxError: invalid syntax
|
|||
|
||||
>>> f(lambda x: x[0] = 3)
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: lambda cannot contain assignment
|
||||
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
|
||||
|
||||
The grammar accepts any test (basically, any expression) in the
|
||||
keyword slot of a call site. Test a few different options.
|
||||
|
||||
>>> f(x()=2)
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: keyword can't be an expression
|
||||
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
|
||||
>>> f(a or b=1)
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: keyword can't be an expression
|
||||
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
|
||||
>>> f(x.y=1)
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: keyword can't be an expression
|
||||
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
|
||||
>>> f((x)=2)
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: keyword can't be an expression
|
||||
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
|
||||
>>> f(True=2)
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: cannot assign to True
|
||||
>>> f(__debug__=1)
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: cannot assign to __debug__
|
||||
|
||||
|
||||
More set_context():
|
||||
|
||||
>>> (x for x in x) += 1
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: can't assign to generator expression
|
||||
SyntaxError: cannot assign to generator expression
|
||||
>>> None += 1
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: can't assign to keyword
|
||||
SyntaxError: cannot assign to None
|
||||
>>> __debug__ += 1
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: cannot assign to __debug__
|
||||
>>> f() += 1
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: can't assign to function call
|
||||
SyntaxError: cannot assign to function call
|
||||
|
||||
|
||||
Test continue in finally in weird combinations.
|
||||
|
@ -481,7 +526,7 @@ leading to spurious errors.
|
|||
... pass
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
SyntaxError: can't assign to function call
|
||||
SyntaxError: cannot assign to function call
|
||||
|
||||
>>> if 1:
|
||||
... pass
|
||||
|
@ -489,7 +534,7 @@ leading to spurious errors.
|
|||
... x() = 1
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
SyntaxError: can't assign to function call
|
||||
SyntaxError: cannot assign to function call
|
||||
|
||||
>>> if 1:
|
||||
... x() = 1
|
||||
|
@ -499,7 +544,7 @@ leading to spurious errors.
|
|||
... pass
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
SyntaxError: can't assign to function call
|
||||
SyntaxError: cannot assign to function call
|
||||
|
||||
>>> if 1:
|
||||
... pass
|
||||
|
@ -509,7 +554,7 @@ leading to spurious errors.
|
|||
... pass
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
SyntaxError: can't assign to function call
|
||||
SyntaxError: cannot assign to function call
|
||||
|
||||
>>> if 1:
|
||||
... pass
|
||||
|
@ -519,7 +564,7 @@ leading to spurious errors.
|
|||
... x() = 1
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
SyntaxError: can't assign to function call
|
||||
SyntaxError: cannot assign to function call
|
||||
|
||||
Make sure that the old "raise X, Y[, Z]" form is gone:
|
||||
>>> raise X, Y
|
||||
|
@ -539,21 +584,33 @@ SyntaxError: keyword argument repeated
|
|||
|
||||
>>> {1, 2, 3} = 42
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: can't assign to literal
|
||||
SyntaxError: cannot assign to set display
|
||||
|
||||
>>> {1: 2, 3: 4} = 42
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: cannot assign to dict display
|
||||
|
||||
>>> f'{x}' = 42
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: cannot assign to f-string expression
|
||||
|
||||
>>> f'{x}-{y}' = 42
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: cannot assign to f-string expression
|
||||
|
||||
Corner-cases that used to fail to raise the correct error:
|
||||
|
||||
>>> def f(*, x=lambda __debug__:0): pass
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: assignment to keyword
|
||||
SyntaxError: cannot assign to __debug__
|
||||
|
||||
>>> def f(*args:(lambda __debug__:0)): pass
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: assignment to keyword
|
||||
SyntaxError: cannot assign to __debug__
|
||||
|
||||
>>> def f(**kwargs:(lambda __debug__:0)): pass
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: assignment to keyword
|
||||
SyntaxError: cannot assign to __debug__
|
||||
|
||||
>>> with (lambda *:0): pass
|
||||
Traceback (most recent call last):
|
||||
|
@ -563,11 +620,11 @@ Corner-cases that used to crash:
|
|||
|
||||
>>> def f(**__debug__): pass
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: assignment to keyword
|
||||
SyntaxError: cannot assign to __debug__
|
||||
|
||||
>>> def f(*xx, __debug__): pass
|
||||
Traceback (most recent call last):
|
||||
SyntaxError: assignment to keyword
|
||||
SyntaxError: cannot assign to __debug__
|
||||
|
||||
"""
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue