Debian Codegraph @

Lib/test/test_unparse.py

raw
1 """Tests for ast.unparse."""
2
3 import unittest
4 import test.support
5 import pathlib
6 import random
7 import tokenize
8 import warnings
9 import ast
10 from test.support.ast_helper import ASTTestMixin
11
12
13 def read_pyfile(filename):
14 """Read and return the contents of a Python source file (as a
15 string), taking into account the file encoding."""
16 with tokenize.open(filename) as stream:
17 return stream.read()
18
19
20 for_else = """\
21 def f():
22 for x in range(10):
23 break
24 else:
25 y = 2
26 z = 3
27 """
28
29 while_else = """\
30 def g():
31 while True:
32 break
33 else:
34 y = 2
35 z = 3
36 """
37
38 relative_import = """\
39 from . import fred
40 from .. import barney
41 from .australia import shrimp as prawns
42 """
43
44 nonlocal_ex = """\
45 def f():
46 x = 1
47 def g():
48 nonlocal x
49 x = 2
50 y = 7
51 def h():
52 nonlocal x, y
53 """
54
55 # also acts as test for 'except ... as ...'
56 raise_from = """\
57 try:
58 1 / 0
59 except ZeroDivisionError as e:
60 raise ArithmeticError from e
61 """
62
63 class_decorator = """\
64 @f1(arg)
65 @f2
66 class Foo: pass
67 """
68
69 elif1 = """\
70 if cond1:
71 suite1
72 elif cond2:
73 suite2
74 else:
75 suite3
76 """
77
78 elif2 = """\
79 if cond1:
80 suite1
81 elif cond2:
82 suite2
83 """
84
85 try_except_finally = """\
86 try:
87 suite1
88 except ex1:
89 suite2
90 except ex2:
91 suite3
92 else:
93 suite4
94 finally:
95 suite5
96 """
97
98 try_except_star_finally = """\
99 try:
100 suite1
101 except* ex1:
102 suite2
103 except* ex2:
104 suite3
105 else:
106 suite4
107 finally:
108 suite5
109 """
110
111 with_simple = """\
112 with f():
113 suite1
114 """
115
116 with_as = """\
117 with f() as x:
118 suite1
119 """
120
121 with_two_items = """\
122 with f() as x, g() as y:
123 suite1
124 """
125
126 docstring_prefixes = (
127 "",
128 "class foo:\n ",
129 "def foo():\n ",
130 "async def foo():\n ",
131 )
132
133 class ASTTestCase(ASTTestMixin, unittest.TestCase):
134 def check_ast_roundtrip(self, code1, **kwargs):
135 with self.subTest(code1=code1, ast_parse_kwargs=kwargs):
136 ast1 = ast.parse(code1, **kwargs)
137 code2 = ast.unparse(ast1)
138 ast2 = ast.parse(code2, **kwargs)
139 self.assertASTEqual(ast1, ast2)
140
141 def check_invalid(self, node, raises=ValueError):
142 with self.subTest(node=node):
143 self.assertRaises(raises, ast.unparse, node)
144
145 def get_source(self, code1, code2=None, **kwargs):
146 code2 = code2 or code1
147 code1 = ast.unparse(ast.parse(code1, **kwargs))
148 return code1, code2
149
150 def check_src_roundtrip(self, code1, code2=None, **kwargs):
151 code1, code2 = self.get_source(code1, code2, **kwargs)
152 with self.subTest(code1=code1, code2=code2):
153 self.assertEqual(code2, code1)
154
155 def check_src_dont_roundtrip(self, code1, code2=None):
156 code1, code2 = self.get_source(code1, code2)
157 with self.subTest(code1=code1, code2=code2):
158 self.assertNotEqual(code2, code1)
159
160 class UnparseTestCase(ASTTestCase):
161 # Tests for specific bugs found in earlier versions of unparse
162
163 def test_fstrings(self):
164 self.check_ast_roundtrip("f'a'")
165 self.check_ast_roundtrip("f'{{}}'")
166 self.check_ast_roundtrip("f'{{5}}'")
167 self.check_ast_roundtrip("f'{{5}}5'")
168 self.check_ast_roundtrip("f'X{{}}X'")
169 self.check_ast_roundtrip("f'{a}'")
170 self.check_ast_roundtrip("f'{ {1:2}}'")
171 self.check_ast_roundtrip("f'a{a}a'")
172 self.check_ast_roundtrip("f'a{a}{a}a'")
173 self.check_ast_roundtrip("f'a{a}a{a}a'")
174 self.check_ast_roundtrip("f'{a!r}x{a!s}12{{}}{a!a}'")
175 self.check_ast_roundtrip("f'{a:10}'")
176 self.check_ast_roundtrip("f'{a:100_000{10}}'")
177 self.check_ast_roundtrip("f'{a!r:10}'")
178 self.check_ast_roundtrip("f'{a:a{b}10}'")
179 self.check_ast_roundtrip(
180 "f'a{b}{c!s}{d!r}{e!a}{f:a}{g:a{b}}{h!s:a}"
181 "{j!s:{a}b}{k!s:a{b}c}{l!a:{b}c{d}}{x+y=}'"
182 )
183
184 def test_fstrings_special_chars(self):
185 # See issue 25180
186 self.check_ast_roundtrip(r"""f'{f"{0}"*3}'""")
187 self.check_ast_roundtrip(r"""f'{f"{y}"*3}'""")
188 self.check_ast_roundtrip("""f''""")
189 self.check_ast_roundtrip('''f"""'end' "quote\\""""''')
190
191 def test_fstrings_complicated(self):
192 # See issue 28002
193 self.check_ast_roundtrip("""f'''{"'"}'''""")
194 self.check_ast_roundtrip('''f\'\'\'-{f"""*{f"+{f'.{x}.'}+"}*"""}-\'\'\'''')
195 self.check_ast_roundtrip('''f\'\'\'-{f"""*{f"+{f'.{x}.'}+"}*"""}-'single quote\\'\'\'\'''')
196 self.check_ast_roundtrip('f"""{\'\'\'\n\'\'\'}"""')
197 self.check_ast_roundtrip('f"""{g(\'\'\'\n\'\'\')}"""')
198 self.check_ast_roundtrip('''f"a\\r\\nb"''')
199 self.check_ast_roundtrip('''f"\\u2028{'x'}"''')
200
201 def test_fstrings_pep701(self):
202 self.check_ast_roundtrip('f" something { my_dict["key"] } something else "')
203 self.check_ast_roundtrip('f"{f"{f"{f"{f"{f"{1+1}"}"}"}"}"}"')
204
205 def test_tstrings(self):
206 self.check_ast_roundtrip("t'foo'")
207 self.check_ast_roundtrip("t'foo {bar}'")
208 self.check_ast_roundtrip("t'foo {bar!s:.2f}'")
209 self.check_ast_roundtrip("t'{a + b}'")
210 self.check_ast_roundtrip("t'{a + b:x}'")
211 self.check_ast_roundtrip("t'{a + b!s}'")
212 self.check_ast_roundtrip("t'{ {a}}'")
213 self.check_ast_roundtrip("t'{ {a}=}'")
214 self.check_ast_roundtrip("t'{{a}}'")
215 self.check_ast_roundtrip("t''")
216 self.check_ast_roundtrip('t""')
217 self.check_ast_roundtrip("t'{(lambda x: x)}'")
218 self.check_ast_roundtrip("t'{t'{x}'}'")
219
220 def test_tstring_with_nonsensical_str_field(self):
221 # `value` suggests that the original code is `t'{test1}`, but `str` suggests otherwise
222 self.assertEqual(
223 ast.unparse(
224 ast.TemplateStr(
225 values=[
226 ast.Interpolation(
227 value=ast.Name(id="test1", ctx=ast.Load()), str="test2", conversion=-1
228 )
229 ]
230 )
231 ),
232 "t'{test2}'",
233 )
234
235 def test_tstring_with_none_str_field(self):
236 self.assertEqual(
237 ast.unparse(
238 ast.TemplateStr(
239 [ast.Interpolation(value=ast.Name(id="test1"), str=None, conversion=-1)]
240 )
241 ),
242 "t'{test1}'",
243 )
244 self.assertEqual(
245 ast.unparse(
246 ast.TemplateStr(
247 [
248 ast.Interpolation(
249 value=ast.Lambda(
250 args=ast.arguments(args=[ast.arg(arg="x")]),
251 body=ast.Name(id="x"),
252 ),
253 str=None,
254 conversion=-1,
255 )
256 ]
257 )
258 ),
259 "t'{(lambda x: x)}'",
260 )
261 self.assertEqual(
262 ast.unparse(
263 ast.TemplateStr(
264 values=[
265 ast.Interpolation(
266 value=ast.TemplateStr(
267 # `str` field kept here
268 [ast.Interpolation(value=ast.Name(id="x"), str="y", conversion=-1)]
269 ),
270 str=None,
271 conversion=-1,
272 )
273 ]
274 )
275 ),
276 '''t"{t'{y}'}"''',
277 )
278 self.assertEqual(
279 ast.unparse(
280 ast.TemplateStr(
281 values=[
282 ast.Interpolation(
283 value=ast.TemplateStr(
284 [ast.Interpolation(value=ast.Name(id="x"), str=None, conversion=-1)]
285 ),
286 str=None,
287 conversion=-1,
288 )
289 ]
290 )
291 ),
292 '''t"{t'{x}'}"''',
293 )
294 self.assertEqual(
295 ast.unparse(ast.TemplateStr(
296 [ast.Interpolation(value=ast.Constant(value="foo"), str=None, conversion=114)]
297 )),
298 '''t"{'foo'!r}"''',
299 )
300
301 def test_strings(self):
302 self.check_ast_roundtrip("u'foo'")
303 self.check_ast_roundtrip("r'foo'")
304 self.check_ast_roundtrip("b'foo'")
305
306 def test_del_statement(self):
307 self.check_ast_roundtrip("del x, y, z")
308
309 def test_shifts(self):
310 self.check_ast_roundtrip("45 << 2")
311 self.check_ast_roundtrip("13 >> 7")
312
313 def test_for_else(self):
314 self.check_ast_roundtrip(for_else)
315
316 def test_while_else(self):
317 self.check_ast_roundtrip(while_else)
318
319 def test_unary_parens(self):
320 self.check_ast_roundtrip("(-1)**7")
321 self.check_ast_roundtrip("(-1.)**8")
322 self.check_ast_roundtrip("(-1j)**6")
323 self.check_ast_roundtrip("not True or False")
324 self.check_ast_roundtrip("True or not False")
325
326 def test_integer_parens(self):
327 self.check_ast_roundtrip("3 .__abs__()")
328
329 def test_huge_float(self):
330 self.check_ast_roundtrip("1e1000")
331 self.check_ast_roundtrip("-1e1000")
332 self.check_ast_roundtrip("1e1000j")
333 self.check_ast_roundtrip("-1e1000j")
334
335 def test_nan(self):
336 self.assertASTEqual(
337 ast.parse(ast.unparse(ast.Constant(value=float('nan')))),
338 ast.parse('1e1000 - 1e1000')
339 )
340
341 def test_min_int(self):
342 self.check_ast_roundtrip(str(-(2 ** 31)))
343 self.check_ast_roundtrip(str(-(2 ** 63)))
344
345 def test_imaginary_literals(self):
346 self.check_ast_roundtrip("7j")
347 self.check_ast_roundtrip("-7j")
348 self.check_ast_roundtrip("0j")
349 self.check_ast_roundtrip("-0j")
350
351 def test_lambda_parentheses(self):
352 self.check_ast_roundtrip("(lambda: int)()")
353
354 def test_chained_comparisons(self):
355 self.check_ast_roundtrip("1 < 4 <= 5")
356 self.check_ast_roundtrip("a is b is c is not d")
357
358 def test_function_arguments(self):
359 self.check_ast_roundtrip("def f(): pass")
360 self.check_ast_roundtrip("def f(a): pass")
361 self.check_ast_roundtrip("def f(b = 2): pass")
362 self.check_ast_roundtrip("def f(a, b): pass")
363 self.check_ast_roundtrip("def f(a, b = 2): pass")
364 self.check_ast_roundtrip("def f(a = 5, b = 2): pass")
365 self.check_ast_roundtrip("def f(*, a = 1, b = 2): pass")
366 self.check_ast_roundtrip("def f(*, a = 1, b): pass")
367 self.check_ast_roundtrip("def f(*, a, b = 2): pass")
368 self.check_ast_roundtrip("def f(a, b = None, *, c, **kwds): pass")
369 self.check_ast_roundtrip("def f(a=2, *args, c=5, d, **kwds): pass")
370 self.check_ast_roundtrip("def f(*args, **kwargs): pass")
371
372 def test_relative_import(self):
373 self.check_ast_roundtrip(relative_import)
374
375 def test_nonlocal(self):
376 self.check_ast_roundtrip(nonlocal_ex)
377
378 def test_raise_from(self):
379 self.check_ast_roundtrip(raise_from)
380
381 def test_bytes(self):
382 self.check_ast_roundtrip("b'123'")
383
384 def test_annotations(self):
385 self.check_ast_roundtrip("def f(a : int): pass")
386 self.check_ast_roundtrip("def f(a: int = 5): pass")
387 self.check_ast_roundtrip("def f(*args: [int]): pass")
388 self.check_ast_roundtrip("def f(**kwargs: dict): pass")
389 self.check_ast_roundtrip("def f() -> None: pass")
390
391 def test_set_literal(self):
392 self.check_ast_roundtrip("{'a', 'b', 'c'}")
393
394 def test_empty_set(self):
395 self.assertASTEqual(
396 ast.parse(ast.unparse(ast.Set(elts=[]))),
397 ast.parse('{*()}')
398 )
399
400 def test_set_comprehension(self):
401 self.check_ast_roundtrip("{x for x in range(5)}")
402
403 def test_dict_comprehension(self):
404 self.check_ast_roundtrip("{x: x*x for x in range(10)}")
405
406 def test_class_decorators(self):
407 self.check_ast_roundtrip(class_decorator)
408
409 def test_class_definition(self):
410 self.check_ast_roundtrip("class A(metaclass=type, *[], **{}): pass")
411
412 def test_elifs(self):
413 self.check_ast_roundtrip(elif1)
414 self.check_ast_roundtrip(elif2)
415
416 def test_try_except_finally(self):
417 self.check_ast_roundtrip(try_except_finally)
418
419 def test_try_except_star_finally(self):
420 self.check_ast_roundtrip(try_except_star_finally)
421
422 def test_starred_assignment(self):
423 self.check_ast_roundtrip("a, *b, c = seq")
424 self.check_ast_roundtrip("a, (*b, c) = seq")
425 self.check_ast_roundtrip("a, *b[0], c = seq")
426 self.check_ast_roundtrip("a, *(b, c) = seq")
427
428 def test_with_simple(self):
429 self.check_ast_roundtrip(with_simple)
430
431 def test_with_as(self):
432 self.check_ast_roundtrip(with_as)
433
434 def test_with_two_items(self):
435 self.check_ast_roundtrip(with_two_items)
436
437 def test_dict_unpacking_in_dict(self):
438 # See issue 26489
439 self.check_ast_roundtrip(r"""{**{'y': 2}, 'x': 1}""")
440 self.check_ast_roundtrip(r"""{**{'y': 2}, **{'x': 1}}""")
441
442 def test_slices(self):
443 self.check_ast_roundtrip("a[i]")
444 self.check_ast_roundtrip("a[i,]")
445 self.check_ast_roundtrip("a[i, j]")
446 # The AST for these next two both look like `a[(*a,)]`
447 self.check_ast_roundtrip("a[(*a,)]")
448 self.check_ast_roundtrip("a[*a]")
449 self.check_ast_roundtrip("a[b, *a]")
450 self.check_ast_roundtrip("a[*a, c]")
451 self.check_ast_roundtrip("a[b, *a, c]")
452 self.check_ast_roundtrip("a[*a, *a]")
453 self.check_ast_roundtrip("a[b, *a, *a]")
454 self.check_ast_roundtrip("a[*a, b, *a]")
455 self.check_ast_roundtrip("a[*a, *a, b]")
456 self.check_ast_roundtrip("a[b, *a, *a, c]")
457 self.check_ast_roundtrip("a[(a:=b)]")
458 self.check_ast_roundtrip("a[(a:=b,c)]")
459 self.check_ast_roundtrip("a[()]")
460 self.check_ast_roundtrip("a[i:j]")
461 self.check_ast_roundtrip("a[:j]")
462 self.check_ast_roundtrip("a[i:]")
463 self.check_ast_roundtrip("a[i:j:k]")
464 self.check_ast_roundtrip("a[:j:k]")
465 self.check_ast_roundtrip("a[i::k]")
466 self.check_ast_roundtrip("a[i:j,]")
467 self.check_ast_roundtrip("a[i:j, k]")
468
469 def test_invalid_raise(self):
470 self.check_invalid(ast.Raise(exc=None, cause=ast.Name(id="X", ctx=ast.Load())))
471
472 def test_invalid_fstring_value(self):
473 self.check_invalid(
474 ast.JoinedStr(
475 values=[
476 ast.Name(id="test", ctx=ast.Load()),
477 ast.Constant(value="test")
478 ]
479 )
480 )
481
482 def test_fstring_backslash(self):
483 # valid since Python 3.12
484 self.assertEqual(ast.unparse(
485 ast.FormattedValue(
486 value=ast.Constant(value="\\\\"),
487 conversion=-1,
488 format_spec=None,
489 )
490 ), "{'\\\\\\\\'}")
491
492 def test_invalid_yield_from(self):
493 self.check_invalid(ast.YieldFrom(value=None))
494
495 def test_import_from_level_none(self):
496 tree = ast.ImportFrom(module='mod', names=[ast.alias(name='x')])
497 self.assertEqual(ast.unparse(tree), "from mod import x")
498 tree = ast.ImportFrom(module='mod', names=[ast.alias(name='x')], level=None)
499 self.assertEqual(ast.unparse(tree), "from mod import x")
500
501 def test_docstrings(self):
502 docstrings = (
503 'this ends with double quote"',
504 'this includes a """triple quote"""',
505 '\r',
506 '\\r',
507 '\t',
508 '\\t',
509 '\n',
510 '\\n',
511 '\r\\r\t\\t\n\\n',
512 '""">>> content = \"\"\"blabla\"\"\" <<<"""',
513 r'foo\n\x00',
514 "' \\'\\'\\'\"\"\" \"\"\\'\\' \\'",
515 '🐍⛎𩸽üéş^\\\\X\\\\BB\N{LONG RIGHTWARDS SQUIGGLE ARROW}'
516 )
517 for docstring in docstrings:
518 # check as Module docstrings for easy testing
519 self.check_ast_roundtrip(f"'''{docstring}'''")
520
521 def test_constant_tuples(self):
522 locs = ast.fix_missing_locations
523 self.check_src_roundtrip(
524 locs(ast.Module([ast.Expr(ast.Constant(value=(1,)))])), "(1,)")
525 self.check_src_roundtrip(
526 locs(ast.Module([ast.Expr(ast.Constant(value=(1, 2, 3)))])), "(1, 2, 3)"
527 )
528
529 def test_function_type(self):
530 for function_type in (
531 "() -> int",
532 "(int, int) -> int",
533 "(Callable[complex], More[Complex(call.to_typevar())]) -> None"
534 ):
535 self.check_ast_roundtrip(function_type, mode="func_type")
536
537 def test_type_comments(self):
538 for statement in (
539 "a = 5 # type:",
540 "a = 5 # type: int",
541 "a = 5 # type: int and more",
542 "def x(): # type: () -> None\n\tpass",
543 "def x(y): # type: (int) -> None and more\n\tpass",
544 "async def x(): # type: () -> None\n\tpass",
545 "async def x(y): # type: (int) -> None and more\n\tpass",
546 "for x in y: # type: int\n\tpass",
547 "async for x in y: # type: int\n\tpass",
548 "with x(): # type: int\n\tpass",
549 "async with x(): # type: int\n\tpass"
550 ):
551 self.check_ast_roundtrip(statement, type_comments=True)
552
553 def test_type_ignore(self):
554 for statement in (
555 "a = 5 # type: ignore",
556 "a = 5 # type: ignore and more",
557 "def x(): # type: ignore\n\tpass",
558 "def x(y): # type: ignore and more\n\tpass",
559 "async def x(): # type: ignore\n\tpass",
560 "async def x(y): # type: ignore and more\n\tpass",
561 "for x in y: # type: ignore\n\tpass",
562 "async for x in y: # type: ignore\n\tpass",
563 "with x(): # type: ignore\n\tpass",
564 "async with x(): # type: ignore\n\tpass"
565 ):
566 self.check_ast_roundtrip(statement, type_comments=True)
567
568 def test_unparse_interactive_semicolons(self):
569 # gh-129598: Fix ast.unparse() when ast.Interactive contains multiple statements
570 self.check_src_roundtrip("i = 1; 'expr'; raise Exception", mode='single')
571 self.check_src_roundtrip("i: int = 1; j: float = 0; k += l", mode='single')
572 combinable = (
573 "'expr'",
574 "(i := 1)",
575 "import foo",
576 "from foo import bar",
577 "i = 1",
578 "i += 1",
579 "i: int = 1",
580 "return i",
581 "pass",
582 "break",
583 "continue",
584 "del i",
585 "assert i",
586 "global i",
587 "nonlocal j",
588 "await i",
589 "yield i",
590 "yield from i",
591 "raise i",
592 "type t[T] = ...",
593 "i",
594 )
595 for a in combinable:
596 for b in combinable:
597 self.check_src_roundtrip(f"{a}; {b}", mode='single')
598
599 def test_unparse_interactive_integrity_1(self):
600 # rest of unparse_interactive_integrity tests just make sure mode='single' parse and unparse didn't break
601 self.check_src_roundtrip(
602 "if i:\n 'expr'\nelse:\n raise Exception",
603 "if i:\n 'expr'\nelse:\n raise Exception",
604 mode='single'
605 )
606 self.check_src_roundtrip(
607 "@decorator1\n@decorator2\ndef func():\n 'docstring'\n i = 1; 'expr'; raise Exception",
608 '''@decorator1\n@decorator2\ndef func():\n """docstring"""\n i = 1\n 'expr'\n raise Exception''',
609 mode='single'
610 )
611 self.check_src_roundtrip(
612 "@decorator1\n@decorator2\nclass cls:\n 'docstring'\n i = 1; 'expr'; raise Exception",
613 '''@decorator1\n@decorator2\nclass cls:\n """docstring"""\n i = 1\n 'expr'\n raise Exception''',
614 mode='single'
615 )
616
617 def test_unparse_interactive_integrity_2(self):
618 for statement in (
619 "def x():\n pass",
620 "def x(y):\n pass",
621 "async def x():\n pass",
622 "async def x(y):\n pass",
623 "for x in y:\n pass",
624 "async for x in y:\n pass",
625 "with x():\n pass",
626 "async with x():\n pass",
627 "def f():\n pass",
628 "def f(a):\n pass",
629 "def f(b=2):\n pass",
630 "def f(a, b):\n pass",
631 "def f(a, b=2):\n pass",
632 "def f(a=5, b=2):\n pass",
633 "def f(*, a=1, b=2):\n pass",
634 "def f(*, a=1, b):\n pass",
635 "def f(*, a, b=2):\n pass",
636 "def f(a, b=None, *, c, **kwds):\n pass",
637 "def f(a=2, *args, c=5, d, **kwds):\n pass",
638 "def f(*args, **kwargs):\n pass",
639 "class cls:\n\n def f(self):\n pass",
640 "class cls:\n\n def f(self, a):\n pass",
641 "class cls:\n\n def f(self, b=2):\n pass",
642 "class cls:\n\n def f(self, a, b):\n pass",
643 "class cls:\n\n def f(self, a, b=2):\n pass",
644 "class cls:\n\n def f(self, a=5, b=2):\n pass",
645 "class cls:\n\n def f(self, *, a=1, b=2):\n pass",
646 "class cls:\n\n def f(self, *, a=1, b):\n pass",
647 "class cls:\n\n def f(self, *, a, b=2):\n pass",
648 "class cls:\n\n def f(self, a, b=None, *, c, **kwds):\n pass",
649 "class cls:\n\n def f(self, a=2, *args, c=5, d, **kwds):\n pass",
650 "class cls:\n\n def f(self, *args, **kwargs):\n pass",
651 ):
652 self.check_src_roundtrip(statement, mode='single')
653
654 def test_unparse_interactive_integrity_3(self):
655 for statement in (
656 "def x():",
657 "def x(y):",
658 "async def x():",
659 "async def x(y):",
660 "for x in y:",
661 "async for x in y:",
662 "with x():",
663 "async with x():",
664 "def f():",
665 "def f(a):",
666 "def f(b=2):",
667 "def f(a, b):",
668 "def f(a, b=2):",
669 "def f(a=5, b=2):",
670 "def f(*, a=1, b=2):",
671 "def f(*, a=1, b):",
672 "def f(*, a, b=2):",
673 "def f(a, b=None, *, c, **kwds):",
674 "def f(a=2, *args, c=5, d, **kwds):",
675 "def f(*args, **kwargs):",
676 ):
677 src = statement + '\n i=1;j=2'
678 out = statement + '\n i = 1\n j = 2'
679
680 self.check_src_roundtrip(src, out, mode='single')
681
682
683 class CosmeticTestCase(ASTTestCase):
684 """Test if there are cosmetic issues caused by unnecessary additions"""
685
686 def test_simple_expressions_parens(self):
687 self.check_src_roundtrip("(a := b)")
688 self.check_src_roundtrip("await x")
689 self.check_src_roundtrip("x if x else y")
690 self.check_src_roundtrip("lambda x: x")
691 self.check_src_roundtrip("1 + 1")
692 self.check_src_roundtrip("1 + 2 / 3")
693 self.check_src_roundtrip("(1 + 2) / 3")
694 self.check_src_roundtrip("(1 + 2) * 3 + 4 * (5 + 2)")
695 self.check_src_roundtrip("(1 + 2) * 3 + 4 * (5 + 2) ** 2")
696 self.check_src_roundtrip("~x")
697 self.check_src_roundtrip("x and y")
698 self.check_src_roundtrip("x and y and z")
699 self.check_src_roundtrip("x and (y and x)")
700 self.check_src_roundtrip("(x and y) and z")
701 self.check_src_roundtrip("(x ** y) ** z ** q")
702 self.check_src_roundtrip("x >> y")
703 self.check_src_roundtrip("x << y")
704 self.check_src_roundtrip("x >> y and x >> z")
705 self.check_src_roundtrip("x + y - z * q ^ t ** k")
706 self.check_src_roundtrip("P * V if P and V else n * R * T")
707 self.check_src_roundtrip("lambda P, V, n: P * V == n * R * T")
708 self.check_src_roundtrip("flag & (other | foo)")
709 self.check_src_roundtrip("not x == y")
710 self.check_src_roundtrip("x == (not y)")
711 self.check_src_roundtrip("yield x")
712 self.check_src_roundtrip("yield from x")
713 self.check_src_roundtrip("call((yield x))")
714 self.check_src_roundtrip("return x + (yield x)")
715
716 def test_class_bases_and_keywords(self):
717 self.check_src_roundtrip("class X:\n pass")
718 self.check_src_roundtrip("class X(A):\n pass")
719 self.check_src_roundtrip("class X(A, B, C, D):\n pass")
720 self.check_src_roundtrip("class X(x=y):\n pass")
721 self.check_src_roundtrip("class X(metaclass=z):\n pass")
722 self.check_src_roundtrip("class X(x=y, z=d):\n pass")
723 self.check_src_roundtrip("class X(A, x=y):\n pass")
724 self.check_src_roundtrip("class X(A, **kw):\n pass")
725 self.check_src_roundtrip("class X(*args):\n pass")
726 self.check_src_roundtrip("class X(*args, **kwargs):\n pass")
727
728 def test_fstrings(self):
729 self.check_src_roundtrip('''f\'\'\'-{f"""*{f"+{f'.{x}.'}+"}*"""}-\'\'\'''')
730 self.check_src_roundtrip('''f\'-{f\'\'\'*{f"""+{f".{f'{x}'}."}+"""}*\'\'\'}-\'''')
731 self.check_src_roundtrip('''f\'-{f\'*{f\'\'\'+{f""".{f"{f'{x}'}"}."""}+\'\'\'}*\'}-\'''')
732 self.check_src_roundtrip('''f"\\u2028{'x'}"''')
733 self.check_src_roundtrip(r"f'{x}\n'")
734 self.check_src_roundtrip('''f"{'\\n'}\\n"''')
735 self.check_src_roundtrip('''f"{f'{x}\\n'}\\n"''')
736
737 def test_docstrings(self):
738 docstrings = (
739 '"""simple doc string"""',
740 '''"""A more complex one
741 with some newlines"""''',
742 '''"""Foo bar baz
743
744 empty newline"""''',
745 '"""With some \t"""',
746 '"""Foo "bar" baz """',
747 '"""\\r"""',
748 '""""""',
749 '"""\'\'\'"""',
750 '"""\'\'\'\'\'\'"""',
751 '"""🐍⛎𩸽üéş^\\\\X\\\\BB⟿"""',
752 '"""end in single \'quote\'"""',
753 "'''end in double \"quote\"'''",
754 '"""almost end in double "quote"."""',
755 )
756
757 for prefix in docstring_prefixes:
758 for docstring in docstrings:
759 self.check_src_roundtrip(f"{prefix}{docstring}")
760
761 def test_docstrings_negative_cases(self):
762 # Test some cases that involve strings in the children of the
763 # first node but aren't docstrings to make sure we don't have
764 # False positives.
765 docstrings_negative = (
766 'a = """false"""',
767 '"""false""" + """unless its optimized"""',
768 '1 + 1\n"""false"""',
769 'f"""no, top level but f-fstring"""'
770 )
771 for prefix in docstring_prefixes:
772 for negative in docstrings_negative:
773 # this cases should be result with single quote
774 # rather then triple quoted docstring
775 src = f"{prefix}{negative}"
776 self.check_ast_roundtrip(src)
777 self.check_src_dont_roundtrip(src)
778
779 def test_unary_op_factor(self):
780 for prefix in ("+", "-", "~"):
781 self.check_src_roundtrip(f"{prefix}1")
782 for prefix in ("not",):
783 self.check_src_roundtrip(f"{prefix} 1")
784
785 def test_slices(self):
786 self.check_src_roundtrip("a[()]")
787 self.check_src_roundtrip("a[1]")
788 self.check_src_roundtrip("a[1, 2]")
789 # Note that `a[*a]`, `a[*a,]`, and `a[(*a,)]` all evaluate to the same
790 # thing at runtime and have the same AST, but only `a[*a,]` passes
791 # this test, because that's what `ast.unparse` produces.
792 self.check_src_roundtrip("a[*a,]")
793 self.check_src_roundtrip("a[1, *a]")
794 self.check_src_roundtrip("a[*a, 2]")
795 self.check_src_roundtrip("a[1, *a, 2]")
796 self.check_src_roundtrip("a[*a, *a]")
797 self.check_src_roundtrip("a[1, *a, *a]")
798 self.check_src_roundtrip("a[*a, 1, *a]")
799 self.check_src_roundtrip("a[*a, *a, 1]")
800 self.check_src_roundtrip("a[1, *a, *a, 2]")
801 self.check_src_roundtrip("a[1:2, *a]")
802 self.check_src_roundtrip("a[*a, 1:2]")
803
804 def test_lambda_parameters(self):
805 self.check_src_roundtrip("lambda: something")
806 self.check_src_roundtrip("four = lambda: 2 + 2")
807 self.check_src_roundtrip("lambda x: x * 2")
808 self.check_src_roundtrip("square = lambda n: n ** 2")
809 self.check_src_roundtrip("lambda x, y: x + y")
810 self.check_src_roundtrip("add = lambda x, y: x + y")
811 self.check_src_roundtrip("lambda x, y, /, z, q, *, u: None")
812 self.check_src_roundtrip("lambda x, *y, **z: None")
813
814 def test_star_expr_assign_target(self):
815 for source_type, source in [
816 ("single assignment", "{target} = foo"),
817 ("multiple assignment", "{target} = {target} = bar"),
818 ("for loop", "for {target} in foo:\n pass"),
819 ("async for loop", "async for {target} in foo:\n pass")
820 ]:
821 for target in [
822 "a",
823 "a,",
824 "a, b",
825 "a, *b, c",
826 "a, (b, c), d",
827 "a, (b, c, d), *e",
828 "a, (b, *c, d), e",
829 "a, (b, *c, (d, e), f), g",
830 "[a]",
831 "[a, b]",
832 "[a, *b, c]",
833 "[a, [b, c], d]",
834 "[a, [b, c, d], *e]",
835 "[a, [b, *c, d], e]",
836 "[a, [b, *c, [d, e], f], g]",
837 "a, [b, c], d",
838 "[a, b, (c, d), (e, f)]",
839 "a, b, [*c], d, e"
840 ]:
841 with self.subTest(source_type=source_type, target=target):
842 self.check_src_roundtrip(source.format(target=target))
843
844 def test_star_expr_assign_target_multiple(self):
845 self.check_src_roundtrip("() = []")
846 self.check_src_roundtrip("[] = ()")
847 self.check_src_roundtrip("() = [a] = c, = [d] = e, f = () = g = h")
848 self.check_src_roundtrip("a = b = c = d")
849 self.check_src_roundtrip("a, b = c, d = e, f = g")
850 self.check_src_roundtrip("[a, b] = [c, d] = [e, f] = g")
851 self.check_src_roundtrip("a, b = [c, d] = e, f = g")
852
853 def test_multiquote_joined_string(self):
854 self.check_ast_roundtrip("f\"'''{1}\\\"\\\"\\\"\" ")
855 self.check_ast_roundtrip("""f"'''{1}""\\"" """)
856 self.check_ast_roundtrip("""f'""\"{1}''' """)
857 self.check_ast_roundtrip("""f'""\"{1}""\\"' """)
858
859 self.check_ast_roundtrip("""f"'''{"\\n"}""\\"" """)
860 self.check_ast_roundtrip("""f'""\"{"\\n"}''' """)
861 self.check_ast_roundtrip("""f'""\"{"\\n"}""\\"' """)
862
863 self.check_ast_roundtrip("""f'''""\"''\\'{"\\n"}''' """)
864 self.check_ast_roundtrip("""f'''""\"''\\'{"\\n\\"'"}''' """)
865 self.check_ast_roundtrip("""f'''""\"''\\'{""\"\\n\\"'''""\" '''\\n'''}''' """)
866
867 def test_backslash_in_format_spec(self):
868 import re
869 msg = re.escape('"\\ " is an invalid escape sequence. '
870 'Such sequences will not work in the future. '
871 'Did you mean "\\\\ "? A raw string is also an option.')
872 with self.assertWarnsRegex(SyntaxWarning, msg):
873 self.check_ast_roundtrip("""f"{x:\\ }" """)
874 self.check_ast_roundtrip("""f"{x:\\n}" """)
875
876 self.check_ast_roundtrip("""f"{x:\\\\ }" """)
877
878 with self.assertWarnsRegex(SyntaxWarning, msg):
879 self.check_ast_roundtrip("""f"{x:\\\\\\ }" """)
880 self.check_ast_roundtrip("""f"{x:\\\\\\n}" """)
881
882 self.check_ast_roundtrip("""f"{x:\\\\\\\\ }" """)
883
884 def test_quote_in_format_spec(self):
885 self.check_ast_roundtrip("""f"{x:'}" """)
886 self.check_ast_roundtrip("""f"{x:\\'}" """)
887 self.check_ast_roundtrip("""f"{x:\\\\'}" """)
888
889 self.check_ast_roundtrip("""f'\\'{x:"}' """)
890 self.check_ast_roundtrip("""f'\\'{x:\\"}' """)
891 self.check_ast_roundtrip("""f'\\'{x:\\\\"}' """)
892
893 def test_type_params(self):
894 self.check_ast_roundtrip("type A = int")
895 self.check_ast_roundtrip("type A[T] = int")
896 self.check_ast_roundtrip("type A[T: int] = int")
897 self.check_ast_roundtrip("type A[T = int] = int")
898 self.check_ast_roundtrip("type A[T: int = int] = int")
899 self.check_ast_roundtrip("type A[**P] = int")
900 self.check_ast_roundtrip("type A[**P = int] = int")
901 self.check_ast_roundtrip("type A[*Ts] = int")
902 self.check_ast_roundtrip("type A[*Ts = int] = int")
903 self.check_ast_roundtrip("type A[*Ts = *int] = int")
904 self.check_ast_roundtrip("def f[T: int = int, **P = int, *Ts = *int]():\n pass")
905 self.check_ast_roundtrip("class C[T: int = int, **P = int, *Ts = *int]():\n pass")
906
907
908 class ManualASTCreationTestCase(unittest.TestCase):
909 """Test that AST nodes created without a type_params field unparse correctly."""
910
911 def test_class(self):
912 node = ast.ClassDef(name="X", bases=[], keywords=[], body=[ast.Pass()], decorator_list=[])
913 ast.fix_missing_locations(node)
914 self.assertEqual(ast.unparse(node), "class X:\n pass")
915
916 def test_class_with_type_params(self):
917 node = ast.ClassDef(name="X", bases=[], keywords=[], body=[ast.Pass()], decorator_list=[],
918 type_params=[ast.TypeVar("T")])
919 ast.fix_missing_locations(node)
920 self.assertEqual(ast.unparse(node), "class X[T]:\n pass")
921
922 def test_function(self):
923 node = ast.FunctionDef(
924 name="f",
925 args=ast.arguments(posonlyargs=[], args=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]),
926 body=[ast.Pass()],
927 decorator_list=[],
928 returns=None,
929 )
930 ast.fix_missing_locations(node)
931 self.assertEqual(ast.unparse(node), "def f():\n pass")
932
933 def test_function_with_type_params(self):
934 node = ast.FunctionDef(
935 name="f",
936 args=ast.arguments(posonlyargs=[], args=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]),
937 body=[ast.Pass()],
938 decorator_list=[],
939 returns=None,
940 type_params=[ast.TypeVar("T")],
941 )
942 ast.fix_missing_locations(node)
943 self.assertEqual(ast.unparse(node), "def f[T]():\n pass")
944
945 def test_function_with_type_params_and_bound(self):
946 node = ast.FunctionDef(
947 name="f",
948 args=ast.arguments(posonlyargs=[], args=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]),
949 body=[ast.Pass()],
950 decorator_list=[],
951 returns=None,
952 type_params=[ast.TypeVar("T", bound=ast.Name("int", ctx=ast.Load()))],
953 )
954 ast.fix_missing_locations(node)
955 self.assertEqual(ast.unparse(node), "def f[T: int]():\n pass")
956
957 def test_function_with_type_params_and_default(self):
958 node = ast.FunctionDef(
959 name="f",
960 args=ast.arguments(),
961 body=[ast.Pass()],
962 type_params=[
963 ast.TypeVar("T", default_value=ast.Constant(value=1)),
964 ast.TypeVarTuple("Ts", default_value=ast.Starred(value=ast.Constant(value=1), ctx=ast.Load())),
965 ast.ParamSpec("P", default_value=ast.Constant(value=1)),
966 ],
967 )
968 ast.fix_missing_locations(node)
969 self.assertEqual(ast.unparse(node), "def f[T = 1, *Ts = *1, **P = 1]():\n pass")
970
971 def test_async_function(self):
972 node = ast.AsyncFunctionDef(
973 name="f",
974 args=ast.arguments(posonlyargs=[], args=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]),
975 body=[ast.Pass()],
976 decorator_list=[],
977 returns=None,
978 )
979 ast.fix_missing_locations(node)
980 self.assertEqual(ast.unparse(node), "async def f():\n pass")
981
982 def test_async_function_with_type_params(self):
983 node = ast.AsyncFunctionDef(
984 name="f",
985 args=ast.arguments(posonlyargs=[], args=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]),
986 body=[ast.Pass()],
987 decorator_list=[],
988 returns=None,
989 type_params=[ast.TypeVar("T")],
990 )
991 ast.fix_missing_locations(node)
992 self.assertEqual(ast.unparse(node), "async def f[T]():\n pass")
993
994 def test_async_function_with_type_params_and_default(self):
995 node = ast.AsyncFunctionDef(
996 name="f",
997 args=ast.arguments(),
998 body=[ast.Pass()],
999 type_params=[
1000 ast.TypeVar("T", default_value=ast.Constant(value=1)),
1001 ast.TypeVarTuple("Ts", default_value=ast.Starred(value=ast.Constant(value=1), ctx=ast.Load())),
1002 ast.ParamSpec("P", default_value=ast.Constant(value=1)),
1003 ],
1004 )
1005 ast.fix_missing_locations(node)
1006 self.assertEqual(ast.unparse(node), "async def f[T = 1, *Ts = *1, **P = 1]():\n pass")
1007
1008
1009 class DirectoryTestCase(ASTTestCase):
1010 """Test roundtrip behaviour on all files in Lib and Lib/test."""
1011
1012 lib_dir = pathlib.Path(__file__).parent / ".."
1013 test_directories = (lib_dir, lib_dir / "test")
1014 run_always_files = {"test_grammar.py", "test_syntax.py", "test_compile.py",
1015 "test_ast.py", "test_asdl_parser.py", "test_fstring.py",
1016 "test_patma.py", "test_type_alias.py", "test_type_params.py",
1017 "test_tokenize.py", "test_tstring.py"}
1018
1019 _files_to_test = None
1020
1021 @classmethod
1022 def files_to_test(cls):
1023
1024 if cls._files_to_test is not None:
1025 return cls._files_to_test
1026
1027 items = [
1028 item.resolve()
1029 for directory in cls.test_directories
1030 for item in directory.glob("*.py")
1031 if not item.name.startswith("bad")
1032 ]
1033
1034 # Test limited subset of files unless the 'cpu' resource is specified.
1035 if not test.support.is_resource_enabled("cpu"):
1036
1037 tests_to_run_always = {item for item in items if
1038 item.name in cls.run_always_files}
1039
1040 items = set(random.sample(items, 10))
1041
1042 # Make sure that at least tests that heavily use grammar features are
1043 # always considered in order to reduce the chance of missing something.
1044 items = list(items | tests_to_run_always)
1045
1046 # bpo-31174: Store the names sample to always test the same files.
1047 # It prevents false alarms when hunting reference leaks.
1048 cls._files_to_test = items
1049
1050 return items
1051
1052 def test_files(self):
1053 with warnings.catch_warnings():
1054 warnings.simplefilter('ignore', SyntaxWarning)
1055
1056 for item in self.files_to_test():
1057 if test.support.verbose:
1058 print(f"Testing {item.absolute()}")
1059
1060 with self.subTest(filename=item):
1061 source = read_pyfile(item)
1062 self.check_ast_roundtrip(source)
1063
1064
1065 if __name__ == "__main__":
1066 unittest.main()