summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2018-11-20 17:27:16 (GMT)
committerGitHub <noreply@github.com>2018-11-20 17:27:16 (GMT)
commit97f1efb6062188645a470daaa91e3669d739c75f (patch)
tree52b5013f014bcd50af40156270fe12e3fc7a5b83 /Lib
parent6c48bf2d9e1e18dfbfa35f7582ddd32f11f75129 (diff)
downloadcpython-97f1efb6062188645a470daaa91e3669d739c75f.zip
cpython-97f1efb6062188645a470daaa91e3669d739c75f.tar.gz
cpython-97f1efb6062188645a470daaa91e3669d739c75f.tar.bz2
bpo-35169: Improve error messages for forbidden assignments. (GH-10342)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_dictcomps.py4
-rw-r--r--Lib/test/test_generators.py4
-rw-r--r--Lib/test/test_genexps.py4
-rw-r--r--Lib/test/test_syntax.py117
4 files changed, 93 insertions, 36 deletions
diff --git a/Lib/test/test_dictcomps.py b/Lib/test/test_dictcomps.py
index 0873071..afe68a8 100644
--- a/Lib/test/test_dictcomps.py
+++ b/Lib/test/test_dictcomps.py
@@ -73,11 +73,11 @@ class DictComprehensionTest(unittest.TestCase):
self.assertEqual(v, "Local variable")
def test_illegal_assignment(self):
- with self.assertRaisesRegex(SyntaxError, "can't assign"):
+ with self.assertRaisesRegex(SyntaxError, "cannot assign"):
compile("{x: y for y, x in ((1, 2), (3, 4))} = 5", "<test>",
"exec")
- with self.assertRaisesRegex(SyntaxError, "can't assign"):
+ with self.assertRaisesRegex(SyntaxError, "cannot assign"):
compile("{x: y for y, x in ((1, 2), (3, 4))} += 5", "<test>",
"exec")
diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py
index 7a21cb7..320793c 100644
--- a/Lib/test/test_generators.py
+++ b/Lib/test/test_generators.py
@@ -1865,12 +1865,12 @@ SyntaxError: assignment to yield expression not possible
>>> def f(): (yield bar) = y
Traceback (most recent call last):
...
-SyntaxError: can't assign to yield expression
+SyntaxError: cannot assign to yield expression
>>> def f(): (yield bar) += y
Traceback (most recent call last):
...
-SyntaxError: can't assign to yield expression
+SyntaxError: cannot assign to yield expression
Now check some throw() conditions:
diff --git a/Lib/test/test_genexps.py b/Lib/test/test_genexps.py
index fb531d6..fd712bb 100644
--- a/Lib/test/test_genexps.py
+++ b/Lib/test/test_genexps.py
@@ -137,12 +137,12 @@ Verify that syntax error's are raised for genexps used as lvalues
>>> (y for y in (1,2)) = 10
Traceback (most recent call last):
...
- SyntaxError: can't assign to generator expression
+ SyntaxError: cannot assign to generator expression
>>> (y for y in (1,2)) += 10
Traceback (most recent call last):
...
- SyntaxError: can't assign to generator expression
+ SyntaxError: cannot assign to generator expression
########### Tests borrowed from or inspired by test_generators.py ############
diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py
index c5b2496..ce1de4b 100644
--- a/Lib/test/test_syntax.py
+++ b/Lib/test/test_syntax.py
@@ -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__
"""