Debian Codegraph @

Lib/test/test_type_comments.py

raw
1 import ast
2 import sys
3 import unittest
4 from test.support import import_helper
5
6
7 funcdef = """\
8 def foo():
9 # type: () -> int
10 pass
11
12 def bar(): # type: () -> None
13 pass
14 """
15
16 asyncdef = """\
17 async def foo():
18 # type: () -> int
19 return await bar()
20
21 async def bar(): # type: () -> int
22 return await bar()
23 """
24
25 asyncvar = """\
26 async = 12
27 await = 13
28 """
29
30 asynccomp = """\
31 async def foo(xs):
32 [x async for x in xs]
33 """
34
35 matmul = """\
36 a = b @ c
37 """
38
39 fstring = """\
40 a = 42
41 f"{a}"
42 """
43
44 underscorednumber = """\
45 a = 42_42_42
46 """
47
48 redundantdef = """\
49 def foo(): # type: () -> int
50 # type: () -> str
51 return ''
52 """
53
54 nonasciidef = """\
55 def foo():
56 # type: () -> àçčéñt
57 pass
58 """
59
60 forstmt = """\
61 for a in []: # type: int
62 pass
63 """
64
65 withstmt = """\
66 with context() as a: # type: int
67 pass
68 """
69
70 parenthesized_withstmt = """\
71 with (a as b): # type: int
72 pass
73
74 with (a, b): # type: int
75 pass
76 """
77
78 vardecl = """\
79 a = 0 # type: int
80 """
81
82 ignores = """\
83 def foo():
84 pass # type: ignore
85
86 def bar():
87 x = 1 # type: ignore
88
89 def baz():
90 pass # type: ignore[excuse]
91 pass # type: ignore=excuse
92 pass # type: ignore [excuse]
93 x = 1 # type: ignore whatever
94 """
95
96 # Test for long-form type-comments in arguments. A test function
97 # named 'fabvk' would have two positional args, a and b, plus a
98 # var-arg *v, plus a kw-arg **k. It is verified in test_longargs()
99 # that it has exactly these arguments, no more, no fewer.
100 longargs = """\
101 def fa(
102 a = 1, # type: A
103 ):
104 pass
105
106 def fa(
107 a = 1 # type: A
108 ):
109 pass
110
111 def fa(
112 a = 1, # type: A
113 /
114 ):
115 pass
116
117 def fab(
118 a, # type: A
119 b, # type: B
120 ):
121 pass
122
123 def fab(
124 a, # type: A
125 /,
126 b, # type: B
127 ):
128 pass
129
130 def fab(
131 a, # type: A
132 b # type: B
133 ):
134 pass
135
136 def fv(
137 *v, # type: V
138 ):
139 pass
140
141 def fv(
142 *v # type: V
143 ):
144 pass
145
146 def fk(
147 **k, # type: K
148 ):
149 pass
150
151 def fk(
152 **k # type: K
153 ):
154 pass
155
156 def fvk(
157 *v, # type: V
158 **k, # type: K
159 ):
160 pass
161
162 def fvk(
163 *v, # type: V
164 **k # type: K
165 ):
166 pass
167
168 def fav(
169 a, # type: A
170 *v, # type: V
171 ):
172 pass
173
174 def fav(
175 a, # type: A
176 /,
177 *v, # type: V
178 ):
179 pass
180
181 def fav(
182 a, # type: A
183 *v # type: V
184 ):
185 pass
186
187 def fak(
188 a, # type: A
189 **k, # type: K
190 ):
191 pass
192
193 def fak(
194 a, # type: A
195 /,
196 **k, # type: K
197 ):
198 pass
199
200 def fak(
201 a, # type: A
202 **k # type: K
203 ):
204 pass
205
206 def favk(
207 a, # type: A
208 *v, # type: V
209 **k, # type: K
210 ):
211 pass
212
213 def favk(
214 a, # type: A
215 /,
216 *v, # type: V
217 **k, # type: K
218 ):
219 pass
220
221 def favk(
222 a, # type: A
223 *v, # type: V
224 **k # type: K
225 ):
226 pass
227 """
228
229
230 class TypeCommentTests(unittest.TestCase):
231
232 lowest = 4 # Lowest minor version supported
233 highest = sys.version_info[1] # Highest minor version
234
235 def parse(self, source, feature_version=highest):
236 return ast.parse(source, type_comments=True,
237 feature_version=feature_version)
238
239 def parse_all(self, source, minver=lowest, maxver=highest, expected_regex=""):
240 for version in range(self.lowest, self.highest + 1):
241 feature_version = (3, version)
242 if minver <= version <= maxver:
243 try:
244 yield self.parse(source, feature_version)
245 except SyntaxError as err:
246 raise SyntaxError(str(err) + f" feature_version={feature_version}")
247 else:
248 with self.assertRaisesRegex(SyntaxError, expected_regex,
249 msg=f"feature_version={feature_version}"):
250 self.parse(source, feature_version)
251
252 def classic_parse(self, source):
253 return ast.parse(source)
254
255 def test_funcdef(self):
256 for tree in self.parse_all(funcdef):
257 self.assertEqual(tree.body[0].type_comment, "() -> int")
258 self.assertEqual(tree.body[1].type_comment, "() -> None")
259 tree = self.classic_parse(funcdef)
260 self.assertEqual(tree.body[0].type_comment, None)
261 self.assertEqual(tree.body[1].type_comment, None)
262
263 def test_asyncdef(self):
264 for tree in self.parse_all(asyncdef, minver=5):
265 self.assertEqual(tree.body[0].type_comment, "() -> int")
266 self.assertEqual(tree.body[1].type_comment, "() -> int")
267 tree = self.classic_parse(asyncdef)
268 self.assertEqual(tree.body[0].type_comment, None)
269 self.assertEqual(tree.body[1].type_comment, None)
270
271 def test_asyncvar(self):
272 with self.assertRaises(SyntaxError):
273 self.classic_parse(asyncvar)
274
275 def test_asynccomp(self):
276 for tree in self.parse_all(asynccomp, minver=6):
277 pass
278
279 def test_matmul(self):
280 for tree in self.parse_all(matmul, minver=5):
281 pass
282
283 def test_fstring(self):
284 for tree in self.parse_all(fstring):
285 pass
286
287 def test_underscorednumber(self):
288 for tree in self.parse_all(underscorednumber, minver=6):
289 pass
290
291 def test_redundantdef(self):
292 for tree in self.parse_all(redundantdef, maxver=0,
293 expected_regex="^Cannot have two type comments on def"):
294 pass
295
296 def test_nonasciidef(self):
297 for tree in self.parse_all(nonasciidef):
298 self.assertEqual(tree.body[0].type_comment, "() -> àçčéñt")
299
300 def test_forstmt(self):
301 for tree in self.parse_all(forstmt):
302 self.assertEqual(tree.body[0].type_comment, "int")
303 tree = self.classic_parse(forstmt)
304 self.assertEqual(tree.body[0].type_comment, None)
305
306 def test_withstmt(self):
307 for tree in self.parse_all(withstmt):
308 self.assertEqual(tree.body[0].type_comment, "int")
309 tree = self.classic_parse(withstmt)
310 self.assertEqual(tree.body[0].type_comment, None)
311
312 def test_parenthesized_withstmt(self):
313 for tree in self.parse_all(parenthesized_withstmt):
314 self.assertEqual(tree.body[0].type_comment, "int")
315 self.assertEqual(tree.body[1].type_comment, "int")
316 tree = self.classic_parse(parenthesized_withstmt)
317 self.assertEqual(tree.body[0].type_comment, None)
318 self.assertEqual(tree.body[1].type_comment, None)
319
320 def test_vardecl(self):
321 for tree in self.parse_all(vardecl):
322 self.assertEqual(tree.body[0].type_comment, "int")
323 tree = self.classic_parse(vardecl)
324 self.assertEqual(tree.body[0].type_comment, None)
325
326 def test_ignores(self):
327 for tree in self.parse_all(ignores):
328 self.assertEqual(
329 [(ti.lineno, ti.tag) for ti in tree.type_ignores],
330 [
331 (2, ''),
332 (5, ''),
333 (8, '[excuse]'),
334 (9, '=excuse'),
335 (10, ' [excuse]'),
336 (11, ' whatever'),
337 ])
338 tree = self.classic_parse(ignores)
339 self.assertEqual(tree.type_ignores, [])
340
341 def test_longargs(self):
342 for tree in self.parse_all(longargs, minver=8):
343 for t in tree.body:
344 # The expected args are encoded in the function name
345 todo = set(t.name[1:])
346 self.assertEqual(len(t.args.args) + len(t.args.posonlyargs),
347 len(todo) - bool(t.args.vararg) - bool(t.args.kwarg))
348 self.assertStartsWith(t.name, 'f')
349 for index, c in enumerate(t.name[1:]):
350 todo.remove(c)
351 if c == 'v':
352 arg = t.args.vararg
353 elif c == 'k':
354 arg = t.args.kwarg
355 else:
356 assert 0 <= ord(c) - ord('a') < len(t.args.posonlyargs + t.args.args)
357 if index < len(t.args.posonlyargs):
358 arg = t.args.posonlyargs[ord(c) - ord('a')]
359 else:
360 arg = t.args.args[ord(c) - ord('a') - len(t.args.posonlyargs)]
361 self.assertEqual(arg.arg, c) # That's the argument name
362 self.assertEqual(arg.type_comment, arg.arg.upper())
363 assert not todo
364 tree = self.classic_parse(longargs)
365 for t in tree.body:
366 for arg in t.args.args + [t.args.vararg, t.args.kwarg]:
367 if arg is not None:
368 self.assertIsNone(arg.type_comment, "%s(%s:%r)" %
369 (t.name, arg.arg, arg.type_comment))
370
371 def test_inappropriate_type_comments(self):
372 """Tests for inappropriately-placed type comments.
373
374 These should be silently ignored with type comments off,
375 but raise SyntaxError with type comments on.
376
377 This is not meant to be exhaustive.
378 """
379
380 def check_both_ways(source):
381 ast.parse(source, type_comments=False)
382 for tree in self.parse_all(source, maxver=0):
383 pass
384
385 check_both_ways("pass # type: int\n")
386 check_both_ways("foo() # type: int\n")
387 check_both_ways("x += 1 # type: int\n")
388 check_both_ways("while True: # type: int\n continue\n")
389 check_both_ways("while True:\n continue # type: int\n")
390 check_both_ways("try: # type: int\n pass\nfinally:\n pass\n")
391 check_both_ways("try:\n pass\nfinally: # type: int\n pass\n")
392 check_both_ways("pass # type: ignorewhatever\n")
393 check_both_ways("pass # type: ignoreé\n")
394
395 def test_non_utf8_type_comment_with_ignore_cookie(self):
396 _testcapi = import_helper.import_module('_testcapi')
397 flags = 0x0800 | 0x1000 # PyCF_IGNORE_COOKIE | PyCF_TYPE_COMMENTS
398 with self.assertRaises(UnicodeDecodeError):
399 _testcapi.Py_CompileStringExFlags(
400 b"a=1 # type: \x80", "<test>", 256, flags)
401 with self.assertRaises(UnicodeDecodeError):
402 _testcapi.Py_CompileStringExFlags(
403 b"def a(f=8, #type: \x80\n\x80", "<test>", 256, flags)
404
405 def test_func_type_input(self):
406
407 def parse_func_type_input(source):
408 return ast.parse(source, "<unknown>", "func_type")
409
410 # Some checks below will crash if the returned structure is wrong
411 tree = parse_func_type_input("() -> int")
412 self.assertEqual(tree.argtypes, [])
413 self.assertEqual(tree.returns.id, "int")
414
415 tree = parse_func_type_input("(int) -> List[str]")
416 self.assertEqual(len(tree.argtypes), 1)
417 arg = tree.argtypes[0]
418 self.assertEqual(arg.id, "int")
419 self.assertEqual(tree.returns.value.id, "List")
420 self.assertEqual(tree.returns.slice.id, "str")
421
422 tree = parse_func_type_input("(int, *str, **Any) -> float")
423 self.assertEqual(tree.argtypes[0].id, "int")
424 self.assertEqual(tree.argtypes[1].id, "str")
425 self.assertEqual(tree.argtypes[2].id, "Any")
426 self.assertEqual(tree.returns.id, "float")
427
428 tree = parse_func_type_input("(*int) -> None")
429 self.assertEqual(tree.argtypes[0].id, "int")
430 tree = parse_func_type_input("(**int) -> None")
431 self.assertEqual(tree.argtypes[0].id, "int")
432 tree = parse_func_type_input("(*int, **str) -> None")
433 self.assertEqual(tree.argtypes[0].id, "int")
434 self.assertEqual(tree.argtypes[1].id, "str")
435
436 with self.assertRaises(SyntaxError):
437 tree = parse_func_type_input("(int, *str, *Any) -> float")
438
439 with self.assertRaises(SyntaxError):
440 tree = parse_func_type_input("(int, **str, Any) -> float")
441
442 with self.assertRaises(SyntaxError):
443 tree = parse_func_type_input("(**int, **str) -> float")
444
445
446 if __name__ == '__main__':
447 unittest.main()