Skip to content

Commit 97f1efb

Browse files
bpo-35169: Improve error messages for forbidden assignments. (GH-10342)
1 parent 6c48bf2 commit 97f1efb

File tree

6 files changed

+140
-96
lines changed

6 files changed

+140
-96
lines changed

Lib/test/test_dictcomps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ def test_local_visibility(self):
7373
self.assertEqual(v, "Local variable")
7474

7575
def test_illegal_assignment(self):
76-
with self.assertRaisesRegex(SyntaxError, "can't assign"):
76+
with self.assertRaisesRegex(SyntaxError, "cannot assign"):
7777
compile("{x: y for y, x in ((1, 2), (3, 4))} = 5", "<test>",
7878
"exec")
7979

80-
with self.assertRaisesRegex(SyntaxError, "can't assign"):
80+
with self.assertRaisesRegex(SyntaxError, "cannot assign"):
8181
compile("{x: y for y, x in ((1, 2), (3, 4))} += 5", "<test>",
8282
"exec")
8383

Lib/test/test_generators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,12 +1865,12 @@ def printsolution(self, x):
18651865
>>> def f(): (yield bar) = y
18661866
Traceback (most recent call last):
18671867
...
1868-
SyntaxError: can't assign to yield expression
1868+
SyntaxError: cannot assign to yield expression
18691869
18701870
>>> def f(): (yield bar) += y
18711871
Traceback (most recent call last):
18721872
...
1873-
SyntaxError: can't assign to yield expression
1873+
SyntaxError: cannot assign to yield expression
18741874
18751875
18761876
Now check some throw() conditions:

Lib/test/test_genexps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@
137137
>>> (y for y in (1,2)) = 10
138138
Traceback (most recent call last):
139139
...
140-
SyntaxError: can't assign to generator expression
140+
SyntaxError: cannot assign to generator expression
141141
142142
>>> (y for y in (1,2)) += 10
143143
Traceback (most recent call last):
144144
...
145-
SyntaxError: can't assign to generator expression
145+
SyntaxError: cannot assign to generator expression
146146
147147
148148
########### Tests borrowed from or inspired by test_generators.py ############

Lib/test/test_syntax.py

Lines changed: 87 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,35 +33,55 @@
3333
3434
>>> None = 1
3535
Traceback (most recent call last):
36-
SyntaxError: can't assign to keyword
36+
SyntaxError: cannot assign to None
37+
38+
>>> obj.True = 1
39+
Traceback (most recent call last):
40+
SyntaxError: invalid syntax
41+
42+
>>> True = 1
43+
Traceback (most recent call last):
44+
SyntaxError: cannot assign to True
45+
46+
>>> obj.__debug__ = 1
47+
Traceback (most recent call last):
48+
SyntaxError: cannot assign to __debug__
49+
50+
>>> __debug__ = 1
51+
Traceback (most recent call last):
52+
SyntaxError: cannot assign to __debug__
3753
3854
>>> f() = 1
3955
Traceback (most recent call last):
40-
SyntaxError: can't assign to function call
56+
SyntaxError: cannot assign to function call
4157
4258
>>> del f()
4359
Traceback (most recent call last):
44-
SyntaxError: can't delete function call
60+
SyntaxError: cannot delete function call
4561
4662
>>> a + 1 = 2
4763
Traceback (most recent call last):
48-
SyntaxError: can't assign to operator
64+
SyntaxError: cannot assign to operator
4965
5066
>>> (x for x in x) = 1
5167
Traceback (most recent call last):
52-
SyntaxError: can't assign to generator expression
68+
SyntaxError: cannot assign to generator expression
5369
5470
>>> 1 = 1
5571
Traceback (most recent call last):
56-
SyntaxError: can't assign to literal
72+
SyntaxError: cannot assign to literal
5773
5874
>>> "abc" = 1
5975
Traceback (most recent call last):
60-
SyntaxError: can't assign to literal
76+
SyntaxError: cannot assign to literal
6177
6278
>>> b"" = 1
6379
Traceback (most recent call last):
64-
SyntaxError: can't assign to literal
80+
SyntaxError: cannot assign to literal
81+
82+
>>> ... = 1
83+
Traceback (most recent call last):
84+
SyntaxError: cannot assign to Ellipsis
6585
6686
>>> `1` = 1
6787
Traceback (most recent call last):
@@ -74,15 +94,31 @@
7494
7595
>>> (a, "b", c) = (1, 2, 3)
7696
Traceback (most recent call last):
77-
SyntaxError: can't assign to literal
97+
SyntaxError: cannot assign to literal
98+
99+
>>> (a, True, c) = (1, 2, 3)
100+
Traceback (most recent call last):
101+
SyntaxError: cannot assign to True
102+
103+
>>> (a, __debug__, c) = (1, 2, 3)
104+
Traceback (most recent call last):
105+
SyntaxError: cannot assign to __debug__
106+
107+
>>> (a, *True, c) = (1, 2, 3)
108+
Traceback (most recent call last):
109+
SyntaxError: cannot assign to True
110+
111+
>>> (a, *__debug__, c) = (1, 2, 3)
112+
Traceback (most recent call last):
113+
SyntaxError: cannot assign to __debug__
78114
79115
>>> [a, b, c + 1] = [1, 2, 3]
80116
Traceback (most recent call last):
81-
SyntaxError: can't assign to operator
117+
SyntaxError: cannot assign to operator
82118
83119
>>> a if 1 else b = 1
84120
Traceback (most recent call last):
85-
SyntaxError: can't assign to conditional expression
121+
SyntaxError: cannot assign to conditional expression
86122
87123
From compiler_complex_args():
88124
@@ -255,36 +291,45 @@
255291
256292
>>> f(lambda x: x[0] = 3)
257293
Traceback (most recent call last):
258-
SyntaxError: lambda cannot contain assignment
294+
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
259295
260296
The grammar accepts any test (basically, any expression) in the
261297
keyword slot of a call site. Test a few different options.
262298
263299
>>> f(x()=2)
264300
Traceback (most recent call last):
265-
SyntaxError: keyword can't be an expression
301+
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
266302
>>> f(a or b=1)
267303
Traceback (most recent call last):
268-
SyntaxError: keyword can't be an expression
304+
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
269305
>>> f(x.y=1)
270306
Traceback (most recent call last):
271-
SyntaxError: keyword can't be an expression
307+
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
272308
>>> f((x)=2)
273309
Traceback (most recent call last):
274-
SyntaxError: keyword can't be an expression
310+
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
311+
>>> f(True=2)
312+
Traceback (most recent call last):
313+
SyntaxError: cannot assign to True
314+
>>> f(__debug__=1)
315+
Traceback (most recent call last):
316+
SyntaxError: cannot assign to __debug__
275317
276318
277319
More set_context():
278320
279321
>>> (x for x in x) += 1
280322
Traceback (most recent call last):
281-
SyntaxError: can't assign to generator expression
323+
SyntaxError: cannot assign to generator expression
282324
>>> None += 1
283325
Traceback (most recent call last):
284-
SyntaxError: can't assign to keyword
326+
SyntaxError: cannot assign to None
327+
>>> __debug__ += 1
328+
Traceback (most recent call last):
329+
SyntaxError: cannot assign to __debug__
285330
>>> f() += 1
286331
Traceback (most recent call last):
287-
SyntaxError: can't assign to function call
332+
SyntaxError: cannot assign to function call
288333
289334
290335
Test continue in finally in weird combinations.
@@ -481,15 +526,15 @@
481526
... pass
482527
Traceback (most recent call last):
483528
...
484-
SyntaxError: can't assign to function call
529+
SyntaxError: cannot assign to function call
485530
486531
>>> if 1:
487532
... pass
488533
... elif 1:
489534
... x() = 1
490535
Traceback (most recent call last):
491536
...
492-
SyntaxError: can't assign to function call
537+
SyntaxError: cannot assign to function call
493538
494539
>>> if 1:
495540
... x() = 1
@@ -499,7 +544,7 @@
499544
... pass
500545
Traceback (most recent call last):
501546
...
502-
SyntaxError: can't assign to function call
547+
SyntaxError: cannot assign to function call
503548
504549
>>> if 1:
505550
... pass
@@ -509,7 +554,7 @@
509554
... pass
510555
Traceback (most recent call last):
511556
...
512-
SyntaxError: can't assign to function call
557+
SyntaxError: cannot assign to function call
513558
514559
>>> if 1:
515560
... pass
@@ -519,7 +564,7 @@
519564
... x() = 1
520565
Traceback (most recent call last):
521566
...
522-
SyntaxError: can't assign to function call
567+
SyntaxError: cannot assign to function call
523568
524569
Make sure that the old "raise X, Y[, Z]" form is gone:
525570
>>> raise X, Y
@@ -539,21 +584,33 @@
539584
540585
>>> {1, 2, 3} = 42
541586
Traceback (most recent call last):
542-
SyntaxError: can't assign to literal
587+
SyntaxError: cannot assign to set display
588+
589+
>>> {1: 2, 3: 4} = 42
590+
Traceback (most recent call last):
591+
SyntaxError: cannot assign to dict display
592+
593+
>>> f'{x}' = 42
594+
Traceback (most recent call last):
595+
SyntaxError: cannot assign to f-string expression
596+
597+
>>> f'{x}-{y}' = 42
598+
Traceback (most recent call last):
599+
SyntaxError: cannot assign to f-string expression
543600
544601
Corner-cases that used to fail to raise the correct error:
545602
546603
>>> def f(*, x=lambda __debug__:0): pass
547604
Traceback (most recent call last):
548-
SyntaxError: assignment to keyword
605+
SyntaxError: cannot assign to __debug__
549606
550607
>>> def f(*args:(lambda __debug__:0)): pass
551608
Traceback (most recent call last):
552-
SyntaxError: assignment to keyword
609+
SyntaxError: cannot assign to __debug__
553610
554611
>>> def f(**kwargs:(lambda __debug__:0)): pass
555612
Traceback (most recent call last):
556-
SyntaxError: assignment to keyword
613+
SyntaxError: cannot assign to __debug__
557614
558615
>>> with (lambda *:0): pass
559616
Traceback (most recent call last):
@@ -563,11 +620,11 @@
563620
564621
>>> def f(**__debug__): pass
565622
Traceback (most recent call last):
566-
SyntaxError: assignment to keyword
623+
SyntaxError: cannot assign to __debug__
567624
568625
>>> def f(*xx, __debug__): pass
569626
Traceback (most recent call last):
570-
SyntaxError: assignment to keyword
627+
SyntaxError: cannot assign to __debug__
571628
572629
"""
573630

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improved error messages for forbidden assignments.

0 commit comments

Comments
 (0)