@@ -129,41 +129,120 @@ def foo(object: object):
129
129
assert foo .__annotations__ ['object' ] == object
130
130
131
131
132
+ import sys
133
+
134
+ def assert_raise_syntax_error (source , msg ):
135
+ try :
136
+ compile (source , "" , "single" )
137
+ except SyntaxError as e :
138
+ assert msg in str (e ), "\n Code:\n ----\n %s\n ----\n Expected message: %s\n Actual message: %s" % (source , msg , str (e ))
139
+ else :
140
+ assert False , "Syntax Error was not raised.\n Code:\n ----\n %s\n ----\n has to raise Syntax Error: %s" % (source ,msg )
141
+
132
142
def test_cannot_assign ():
133
- import sys
134
143
if sys .implementation .version .minor >= 8 :
135
- def assert_raise (s , msg ):
136
- try :
137
- compile ("%s = 1" % s , "" , "single" )
138
- except SyntaxError as e :
139
- assert msg in str (e ), str (e )
140
- else :
141
- assert False
142
- try :
143
- compile ("with foo as %s:\n pass" % s , "" , "single" )
144
- except SyntaxError as e :
145
- assert msg in str (e ), str (e )
146
- else :
147
- assert False
148
- assert_raise ("None" , "cannot assign to None" )
149
- assert_raise ("1" , "cannot assign to literal" )
150
- assert_raise ("1.1" , "cannot assign to literal" )
151
- assert_raise ("{1}" , "cannot assign to set display" )
152
- assert_raise ("{1: 2}" , "cannot assign to dict display" )
153
- assert_raise ("1.2j" , "cannot assign to literal" )
154
- assert_raise ("..." , "cannot assign to Ellipsis" )
155
- assert_raise ("True" , "cannot assign to True" )
156
- assert_raise ("False" , "cannot assign to False" )
157
- assert_raise ("b''" , "cannot assign to literal" )
158
- assert_raise ("''" , "cannot assign to literal" )
159
- assert_raise ("f''" , "cannot assign to f-string expression" )
160
- assert_raise ("(None,)" , "cannot assign to None" )
161
-
144
+ def check (s , msg ):
145
+ # test simple assignment
146
+ assert_raise_syntax_error ("%s = 1" % s , msg )
147
+ # testing aug assignment
148
+ assert_raise_syntax_error ("%s += 1" % s , msg )
149
+ # test with statement
150
+ assert_raise_syntax_error ("with foo as %s:\n pass" % s , msg )
151
+ # test for statement
152
+ assert_raise_syntax_error ("for %s in range(1,10):\n pass" % s , msg )
153
+ # test for comprehension statement
154
+ assert_raise_syntax_error ("[1 for %s in range(1,10)]" % s , msg )
155
+ check ("1" , "cannot assign to literal" )
156
+ check ("1.1" , "cannot assign to literal" )
157
+ check ("{1}" , "cannot assign to set display" )
158
+ check ("{}" , "cannot assign to dict display" )
159
+ check ("{1: 2}" , "cannot assign to dict display" )
160
+ check ("[1,2, 3]" , "cannot assign to literal" )
161
+ check ("(1,2, 3)" , "cannot assign to literal" )
162
+ check ("1.2j" , "cannot assign to literal" )
163
+ check ("None" , "cannot assign to None" )
164
+ check ("..." , "cannot assign to Ellipsis" )
165
+ check ("True" , "cannot assign to True" )
166
+ check ("False" , "cannot assign to False" )
167
+ check ("b''" , "cannot assign to literal" )
168
+ check ("''" , "cannot assign to literal" )
169
+ check ("f''" , "cannot assign to f-string expression" )
170
+ check ("(a,None, b)" , "cannot assign to None" )
171
+ check ("(a,True, b)" , "cannot assign to True" )
172
+ check ("(a,False, b)" , "cannot assign to False" )
173
+ check ("a+b" , "cannot assign to operator" )
174
+ check ("fn()" , "cannot assign to function call" )
175
+ check ("{letter for letter in 'ahoj'}" , "cannot assign to set comprehension" )
176
+ check ("[letter for letter in 'ahoj']" , "cannot assign to list comprehension" )
177
+ check ("(letter for letter in 'ahoj')" , "cannot assign to generator expression" )
178
+ check ("obj.True" , "invalid syntax" )
179
+ check ("(a, *True, b)" , "cannot assign to True" )
180
+ check ("(a, *False, b)" , "cannot assign to False" )
181
+ check ("(a, *None, b)" , "cannot assign to None" )
182
+ check ("(a, *..., b)" , "cannot assign to Ellipsis" )
183
+ check ("__debug__" , "cannot assign to __debug__" )
184
+ check ("a.__debug__" , "cannot assign to __debug__" )
185
+ check ("a.b.__debug__" , "cannot assign to __debug__" )
186
+
187
+ def test_cannot_assign_without_with ():
188
+ if sys .implementation .version .minor >= 8 :
189
+ def check (s , msg ):
190
+ # test simple assignment
191
+ assert_raise_syntax_error ("%s = 1" % s , msg )
192
+ # testing aug assignment
193
+ assert_raise_syntax_error ("%s += 1" % s , msg )
194
+ # test for statement
195
+ assert_raise_syntax_error ("for %s in range(1,10):\n pass" % s , msg )
196
+ # test for comprehension statement
197
+ assert_raise_syntax_error ("[1 for %s in range(1,10)]" % s , msg )
198
+ check ("*True" , "cannot assign to True" )
199
+ check ("*False" , "cannot assign to False" )
200
+ check ("*None" , "cannot assign to None" )
201
+ check ("*..." , "cannot assign to Ellipsis" )
202
+ check ("[a, b, c + 1]," , "cannot assign to operator" )
203
+
204
+ def test_cannot_assign_other ():
205
+ if sys .implementation .version .minor >= 8 :
206
+ assert_raise_syntax_error ("a if 1 else b = 1" , "cannot assign to conditional expression" )
207
+ assert_raise_syntax_error ("a if 1 else b -= 1" , "cannot assign to conditional expression" )
208
+ assert_raise_syntax_error ("f(True=2)" , "cannot assign to True" )
209
+ assert_raise_syntax_error ("f(__debug__=2)" , "cannot assign to __debug__" )
210
+ assert_raise_syntax_error ("def f(__debug__): pass\n " , "cannot assign to __debug__" )
211
+ assert_raise_syntax_error ("def f(*, x=lambda __debug__:0): pass\n " , "cannot assign to __debug__" )
212
+ assert_raise_syntax_error ("def f(*args:(lambda __debug__:0)): pass\n " , "cannot assign to __debug__" )
213
+ assert_raise_syntax_error ("def f(**kwargs:(lambda __debug__:0)): pass\n " , "cannot assign to __debug__" )
214
+ assert_raise_syntax_error ("def f(**__debug__): pass\n " , "cannot assign to __debug__" )
215
+ assert_raise_syntax_error ("def f(*xx, __debug__): pass\n " , "cannot assign to __debug__" )
216
+
217
+ def test_invalid_assignmetn_to_yield_expression ():
218
+ if sys .implementation .version .minor >= 8 :
219
+ assert_raise_syntax_error ("def fn():\n (yield 10) = 1" , "cannot assign to yield expression" )
220
+ assert_raise_syntax_error ("def fn():\n (yield 10) += 1" , "cannot assign to yield expression" )
221
+ assert_raise_syntax_error ("def fn():\n with foo as (yield 10) : pass" , "cannot assign to yield expression" )
222
+ assert_raise_syntax_error ("def fn():\n for (yield 10) in range(1,10): pass" , "cannot assign to yield expression" )
162
223
163
224
def test_invalid_ann_assignment ():
164
- try :
165
- exec ("lambda: x:x" )
166
- except SyntaxError as e :
167
- assert e .msg == 'illegal target for annotation'
168
- return
169
- assert False , "SyntaxError not raised"
225
+ if sys .implementation .version .minor >= 8 :
226
+ assert_raise_syntax_error ("lambda: x:x" , 'illegal target for annotation' )
227
+ assert_raise_syntax_error ("list(): int" , "illegal target for annotation" )
228
+ assert_raise_syntax_error ("{}: int" , "illegal target for annotation" )
229
+ assert_raise_syntax_error ("{1,2}: int" , "illegal target for annotation" )
230
+ assert_raise_syntax_error ("{'1':'2'}: int" , "illegal target for annotation" )
231
+ assert_raise_syntax_error ("[1,2]: int" , "only single target (not list) can be annotated" )
232
+ assert_raise_syntax_error ("(1,2): int" , "only single target (not tuple) can be annotated" )
233
+
234
+ def test_invalid_aug_assignment ():
235
+ if sys .implementation .version .minor >= 8 :
236
+ assert_raise_syntax_error ("[] += 1" , 'illegal expression for augmented assignment' )
237
+ assert_raise_syntax_error ("() += 1" , 'illegal expression for augmented assignment' )
238
+ assert_raise_syntax_error ("x, y += (1, 2)" , 'illegal expression for augmented assignment' )
239
+ assert_raise_syntax_error ("x, y += 1" , 'illegal expression for augmented assignment' )
240
+
241
+ def test_invalid_star_import ():
242
+ assert_raise_syntax_error ("def f(): from bla import *\n " , 'import * only allowed at module level' )
243
+
244
+ def test_invalid_return_statement ():
245
+ assert_raise_syntax_error ("return 10" , "'return' outside function" )
246
+ assert_raise_syntax_error ("class A: return 10\n " , "'return' outside function" )
247
+
248
+
0 commit comments