Lines Matching refs:self

132     def assertASTEqual(self, ast1, ast2):
134 self.assertIsInstance(ast1, ast.AST)
135 self.assertIsInstance(ast2, ast.AST)
142 self.fail(f"{type(a)!r} is not {type(b)!r}")
166 raise self.failureException(msg) from None
168 self.fail(f"{a!r} != {b!r}")
171 def check_ast_roundtrip(self, code1, **kwargs):
172 with self.subTest(code1=code1, ast_parse_kwargs=kwargs):
176 self.assertASTEqual(ast1, ast2)
178 def check_invalid(self, node, raises=ValueError):
179 with self.subTest(node=node):
180 self.assertRaises(raises, ast.unparse, node)
182 def get_source(self, code1, code2=None):
187 def check_src_roundtrip(self, code1, code2=None):
188 code1, code2 = self.get_source(code1, code2)
189 with self.subTest(code1=code1, code2=code2):
190 self.assertEqual(code2, code1)
192 def check_src_dont_roundtrip(self, code1, code2=None):
193 code1, code2 = self.get_source(code1, code2)
194 with self.subTest(code1=code1, code2=code2):
195 self.assertNotEqual(code2, code1)
200 def test_fstrings(self):
201 self.check_ast_roundtrip("f'a'")
202 self.check_ast_roundtrip("f'{{}}'")
203 self.check_ast_roundtrip("f'{{5}}'")
204 self.check_ast_roundtrip("f'{{5}}5'")
205 self.check_ast_roundtrip("f'X{{}}X'")
206 self.check_ast_roundtrip("f'{a}'")
207 self.check_ast_roundtrip("f'{ {1:2}}'")
208 self.check_ast_roundtrip("f'a{a}a'")
209 self.check_ast_roundtrip("f'a{a}{a}a'")
210 self.check_ast_roundtrip("f'a{a}a{a}a'")
211 self.check_ast_roundtrip("f'{a!r}x{a!s}12{{}}{a!a}'")
212 self.check_ast_roundtrip("f'{a:10}'")
213 self.check_ast_roundtrip("f'{a:100_000{10}}'")
214 self.check_ast_roundtrip("f'{a!r:10}'")
215 self.check_ast_roundtrip("f'{a:a{b}10}'")
216 self.check_ast_roundtrip(
221 def test_fstrings_special_chars(self):
223 self.check_ast_roundtrip(r"""f'{f"{0}"*3}'""")
224 self.check_ast_roundtrip(r"""f'{f"{y}"*3}'""")
225 self.check_ast_roundtrip("""f''""")
226 self.check_ast_roundtrip('''f"""'end' "quote\\""""''')
228 def test_fstrings_complicated(self):
230 self.check_ast_roundtrip("""f'''{"'"}'''""")
231 self.check_ast_roundtrip('''f\'\'\'-{f"""*{f"+{f'.{x}.'}+"}*"""}-\'\'\'''')
232 self.check_ast_roundtrip('''f\'\'\'-{f"""*{f"+{f'.{x}.'}+"}*"""}-'single quote\\'\'\'\'''')
233 self.check_ast_roundtrip('f"""{\'\'\'\n\'\'\'}"""')
234 self.check_ast_roundtrip('f"""{g(\'\'\'\n\'\'\')}"""')
235 self.check_ast_roundtrip('''f"a\\r\\nb"''')
236 self.check_ast_roundtrip('''f"\\u2028{'x'}"''')
238 def test_strings(self):
239 self.check_ast_roundtrip("u'foo'")
240 self.check_ast_roundtrip("r'foo'")
241 self.check_ast_roundtrip("b'foo'")
243 def test_del_statement(self):
244 self.check_ast_roundtrip("del x, y, z")
246 def test_shifts(self):
247 self.check_ast_roundtrip("45 << 2")
248 self.check_ast_roundtrip("13 >> 7")
250 def test_for_else(self):
251 self.check_ast_roundtrip(for_else)
253 def test_while_else(self):
254 self.check_ast_roundtrip(while_else)
256 def test_unary_parens(self):
257 self.check_ast_roundtrip("(-1)**7")
258 self.check_ast_roundtrip("(-1.)**8")
259 self.check_ast_roundtrip("(-1j)**6")
260 self.check_ast_roundtrip("not True or False")
261 self.check_ast_roundtrip("True or not False")
263 def test_integer_parens(self):
264 self.check_ast_roundtrip("3 .__abs__()")
266 def test_huge_float(self):
267 self.check_ast_roundtrip("1e1000")
268 self.check_ast_roundtrip("-1e1000")
269 self.check_ast_roundtrip("1e1000j")
270 self.check_ast_roundtrip("-1e1000j")
272 def test_nan(self):
273 self.assertASTEqual(
278 def test_min_int(self):
279 self.check_ast_roundtrip(str(-(2 ** 31)))
280 self.check_ast_roundtrip(str(-(2 ** 63)))
282 def test_imaginary_literals(self):
283 self.check_ast_roundtrip("7j")
284 self.check_ast_roundtrip("-7j")
285 self.check_ast_roundtrip("0j")
286 self.check_ast_roundtrip("-0j")
288 def test_lambda_parentheses(self):
289 self.check_ast_roundtrip("(lambda: int)()")
291 def test_chained_comparisons(self):
292 self.check_ast_roundtrip("1 < 4 <= 5")
293 self.check_ast_roundtrip("a is b is c is not d")
295 def test_function_arguments(self):
296 self.check_ast_roundtrip("def f(): pass")
297 self.check_ast_roundtrip("def f(a): pass")
298 self.check_ast_roundtrip("def f(b = 2): pass")
299 self.check_ast_roundtrip("def f(a, b): pass")
300 self.check_ast_roundtrip("def f(a, b = 2): pass")
301 self.check_ast_roundtrip("def f(a = 5, b = 2): pass")
302 self.check_ast_roundtrip("def f(*, a = 1, b = 2): pass")
303 self.check_ast_roundtrip("def f(*, a = 1, b): pass")
304 self.check_ast_roundtrip("def f(*, a, b = 2): pass")
305 self.check_ast_roundtrip("def f(a, b = None, *, c, **kwds): pass")
306 self.check_ast_roundtrip("def f(a=2, *args, c=5, d, **kwds): pass")
307 self.check_ast_roundtrip("def f(*args, **kwargs): pass")
309 def test_relative_import(self):
310 self.check_ast_roundtrip(relative_import)
312 def test_nonlocal(self):
313 self.check_ast_roundtrip(nonlocal_ex)
315 def test_raise_from(self):
316 self.check_ast_roundtrip(raise_from)
318 def test_bytes(self):
319 self.check_ast_roundtrip("b'123'")
321 def test_annotations(self):
322 self.check_ast_roundtrip("def f(a : int): pass")
323 self.check_ast_roundtrip("def f(a: int = 5): pass")
324 self.check_ast_roundtrip("def f(*args: [int]): pass")
325 self.check_ast_roundtrip("def f(**kwargs: dict): pass")
326 self.check_ast_roundtrip("def f() -> None: pass")
328 def test_set_literal(self):
329 self.check_ast_roundtrip("{'a', 'b', 'c'}")
331 def test_empty_set(self):
332 self.assertASTEqual(
337 def test_set_comprehension(self):
338 self.check_ast_roundtrip("{x for x in range(5)}")
340 def test_dict_comprehension(self):
341 self.check_ast_roundtrip("{x: x*x for x in range(10)}")
343 def test_class_decorators(self):
344 self.check_ast_roundtrip(class_decorator)
346 def test_class_definition(self):
347 self.check_ast_roundtrip("class A(metaclass=type, *[], **{}): pass")
349 def test_elifs(self):
350 self.check_ast_roundtrip(elif1)
351 self.check_ast_roundtrip(elif2)
353 def test_try_except_finally(self):
354 self.check_ast_roundtrip(try_except_finally)
356 def test_try_except_star_finally(self):
357 self.check_ast_roundtrip(try_except_star_finally)
359 def test_starred_assignment(self):
360 self.check_ast_roundtrip("a, *b, c = seq")
361 self.check_ast_roundtrip("a, (*b, c) = seq")
362 self.check_ast_roundtrip("a, *b[0], c = seq")
363 self.check_ast_roundtrip("a, *(b, c) = seq")
365 def test_with_simple(self):
366 self.check_ast_roundtrip(with_simple)
368 def test_with_as(self):
369 self.check_ast_roundtrip(with_as)
371 def test_with_two_items(self):
372 self.check_ast_roundtrip(with_two_items)
374 def test_dict_unpacking_in_dict(self):
376 self.check_ast_roundtrip(r"""{**{'y': 2}, 'x': 1}""")
377 self.check_ast_roundtrip(r"""{**{'y': 2}, **{'x': 1}}""")
379 def test_slices(self):
380 self.check_ast_roundtrip("a[i]")
381 self.check_ast_roundtrip("a[i,]")
382 self.check_ast_roundtrip("a[i, j]")
384 self.check_ast_roundtrip("a[(*a,)]")
385 self.check_ast_roundtrip("a[*a]")
386 self.check_ast_roundtrip("a[b, *a]")
387 self.check_ast_roundtrip("a[*a, c]")
388 self.check_ast_roundtrip("a[b, *a, c]")
389 self.check_ast_roundtrip("a[*a, *a]")
390 self.check_ast_roundtrip("a[b, *a, *a]")
391 self.check_ast_roundtrip("a[*a, b, *a]")
392 self.check_ast_roundtrip("a[*a, *a, b]")
393 self.check_ast_roundtrip("a[b, *a, *a, c]")
394 self.check_ast_roundtrip("a[(a:=b)]")
395 self.check_ast_roundtrip("a[(a:=b,c)]")
396 self.check_ast_roundtrip("a[()]")
397 self.check_ast_roundtrip("a[i:j]")
398 self.check_ast_roundtrip("a[:j]")
399 self.check_ast_roundtrip("a[i:]")
400 self.check_ast_roundtrip("a[i:j:k]")
401 self.check_ast_roundtrip("a[:j:k]")
402 self.check_ast_roundtrip("a[i::k]")
403 self.check_ast_roundtrip("a[i:j,]")
404 self.check_ast_roundtrip("a[i:j, k]")
406 def test_invalid_raise(self):
407 self.check_invalid(ast.Raise(exc=None, cause=ast.Name(id="X")))
409 def test_invalid_fstring_value(self):
410 self.check_invalid(
419 def test_invalid_fstring_backslash(self):
420 self.check_invalid(ast.FormattedValue(value=ast.Constant(value="\\\\")))
422 def test_invalid_yield_from(self):
423 self.check_invalid(ast.YieldFrom(value=None))
425 def test_import_from_level_none(self):
427 self.assertEqual(ast.unparse(tree), "from mod import x")
429 self.assertEqual(ast.unparse(tree), "from mod import x")
431 def test_docstrings(self):
449 self.check_ast_roundtrip(f"'''{docstring}'''")
451 def test_constant_tuples(self):
452 self.check_src_roundtrip(ast.Constant(value=(1,), kind=None), "(1,)")
453 self.check_src_roundtrip(
457 def test_function_type(self):
463 self.check_ast_roundtrip(function_type, mode="func_type")
465 def test_type_comments(self):
479 self.check_ast_roundtrip(statement, type_comments=True)
481 def test_type_ignore(self):
494 self.check_ast_roundtrip(statement, type_comments=True)
500 def test_simple_expressions_parens(self):
501 self.check_src_roundtrip("(a := b)")
502 self.check_src_roundtrip("await x")
503 self.check_src_roundtrip("x if x else y")
504 self.check_src_roundtrip("lambda x: x")
505 self.check_src_roundtrip("1 + 1")
506 self.check_src_roundtrip("1 + 2 / 3")
507 self.check_src_roundtrip("(1 + 2) / 3")
508 self.check_src_roundtrip("(1 + 2) * 3 + 4 * (5 + 2)")
509 self.check_src_roundtrip("(1 + 2) * 3 + 4 * (5 + 2) ** 2")
510 self.check_src_roundtrip("~x")
511 self.check_src_roundtrip("x and y")
512 self.check_src_roundtrip("x and y and z")
513 self.check_src_roundtrip("x and (y and x)")
514 self.check_src_roundtrip("(x and y) and z")
515 self.check_src_roundtrip("(x ** y) ** z ** q")
516 self.check_src_roundtrip("x >> y")
517 self.check_src_roundtrip("x << y")
518 self.check_src_roundtrip("x >> y and x >> z")
519 self.check_src_roundtrip("x + y - z * q ^ t ** k")
520 self.check_src_roundtrip("P * V if P and V else n * R * T")
521 self.check_src_roundtrip("lambda P, V, n: P * V == n * R * T")
522 self.check_src_roundtrip("flag & (other | foo)")
523 self.check_src_roundtrip("not x == y")
524 self.check_src_roundtrip("x == (not y)")
525 self.check_src_roundtrip("yield x")
526 self.check_src_roundtrip("yield from x")
527 self.check_src_roundtrip("call((yield x))")
528 self.check_src_roundtrip("return x + (yield x)")
530 def test_class_bases_and_keywords(self):
531 self.check_src_roundtrip("class X:\n pass")
532 self.check_src_roundtrip("class X(A):\n pass")
533 self.check_src_roundtrip("class X(A, B, C, D):\n pass")
534 self.check_src_roundtrip("class X(x=y):\n pass")
535 self.check_src_roundtrip("class X(metaclass=z):\n pass")
536 self.check_src_roundtrip("class X(x=y, z=d):\n pass")
537 self.check_src_roundtrip("class X(A, x=y):\n pass")
538 self.check_src_roundtrip("class X(A, **kw):\n pass")
539 self.check_src_roundtrip("class X(*args):\n pass")
540 self.check_src_roundtrip("class X(*args, **kwargs):\n pass")
542 def test_fstrings(self):
543 self.check_src_roundtrip('''f\'\'\'-{f"""*{f"+{f'.{x}.'}+"}*"""}-\'\'\'''')
544 self.check_src_roundtrip('''f"\\u2028{'x'}"''')
545 self.check_src_roundtrip(r"f'{x}\n'")
546 self.check_src_roundtrip('''f''\'{"""\n"""}\\n''\'''')
547 self.check_src_roundtrip('''f''\'{f"""{x}\n"""}\\n''\'''')
549 def test_docstrings(self):
571 self.check_src_roundtrip(f"{prefix}{docstring}")
573 def test_docstrings_negative_cases(self):
588 self.check_ast_roundtrip(src)
589 self.check_src_dont_roundtrip(src)
591 def test_unary_op_factor(self):
593 self.check_src_roundtrip(f"{prefix}1")
595 self.check_src_roundtrip(f"{prefix} 1")
597 def test_slices(self):
598 self.check_src_roundtrip("a[()]")
599 self.check_src_roundtrip("a[1]")
600 self.check_src_roundtrip("a[1, 2]")
604 self.check_src_roundtrip("a[*a,]")
605 self.check_src_roundtrip("a[1, *a]")
606 self.check_src_roundtrip("a[*a, 2]")
607 self.check_src_roundtrip("a[1, *a, 2]")
608 self.check_src_roundtrip("a[*a, *a]")
609 self.check_src_roundtrip("a[1, *a, *a]")
610 self.check_src_roundtrip("a[*a, 1, *a]")
611 self.check_src_roundtrip("a[*a, *a, 1]")
612 self.check_src_roundtrip("a[1, *a, *a, 2]")
613 self.check_src_roundtrip("a[1:2, *a]")
614 self.check_src_roundtrip("a[*a, 1:2]")
616 def test_lambda_parameters(self):
617 self.check_src_roundtrip("lambda: something")
618 self.check_src_roundtrip("four = lambda: 2 + 2")
619 self.check_src_roundtrip("lambda x: x * 2")
620 self.check_src_roundtrip("square = lambda n: n ** 2")
621 self.check_src_roundtrip("lambda x, y: x + y")
622 self.check_src_roundtrip("add = lambda x, y: x + y")
623 self.check_src_roundtrip("lambda x, y, /, z, q, *, u: None")
624 self.check_src_roundtrip("lambda x, *y, **z: None")
626 def test_star_expr_assign_target(self):
653 with self.subTest(source_type=source_type, target=target):
654 self.check_src_roundtrip(source.format(target=target))
656 def test_star_expr_assign_target_multiple(self):
657 self.check_src_roundtrip("() = []")
658 self.check_src_roundtrip("[] = ()")
659 self.check_src_roundtrip("() = [a] = c, = [d] = e, f = () = g = h")
660 self.check_src_roundtrip("a = b = c = d")
661 self.check_src_roundtrip("a, b = c, d = e, f = g")
662 self.check_src_roundtrip("[a, b] = [c, d] = [e, f] = g")
663 self.check_src_roundtrip("a, b = [c, d] = e, f = g")
709 def test_files(self):
710 for item in self.files_to_test():
714 with self.subTest(filename=item):
716 self.check_ast_roundtrip(source)