gh-116022: Improve repr() of AST nodes (#117046)

Co-authored-by: AN Long <aisk@users.noreply.github.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This commit is contained in:
Tomas R 2024-09-18 19:28:22 +02:00 committed by GitHub
parent f9fa6ba4f8
commit 21d2a9ab2f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 682 additions and 2 deletions

View file

@ -10,6 +10,7 @@ import textwrap
import types
import unittest
import weakref
from pathlib import Path
from textwrap import dedent
try:
import _testinternalcapi
@ -29,6 +30,16 @@ STDLIB = os.path.dirname(ast.__file__)
STDLIB_FILES = [fn for fn in os.listdir(STDLIB) if fn.endswith(".py")]
STDLIB_FILES.extend(["test/test_grammar.py", "test/test_unpack_ex.py"])
AST_REPR_DATA_FILE = Path(__file__).parent / "data" / "ast_repr.txt"
def ast_repr_get_test_cases() -> list[str]:
return exec_tests + eval_tests
def ast_repr_update_snapshots() -> None:
data = [repr(ast.parse(test)) for test in ast_repr_get_test_cases()]
AST_REPR_DATA_FILE.write_text("\n".join(data))
class AST_Tests(unittest.TestCase):
maxDiff = None
@ -408,7 +419,7 @@ class AST_Tests(unittest.TestCase):
m = ast.Module([ast.Expr(ast.expr(**pos), **pos)], [])
with self.assertRaises(TypeError) as cm:
compile(m, "<test>", "exec")
self.assertIn("but got <ast.expr", str(cm.exception))
self.assertIn("but got expr()", str(cm.exception))
def test_invalid_identifier(self):
m = ast.Module([ast.Expr(ast.Name(42, ast.Load()))], [])
@ -772,6 +783,12 @@ class AST_Tests(unittest.TestCase):
for node, attr, source in tests:
self.assert_none_check(node, attr, source)
def test_repr(self) -> None:
snapshots = AST_REPR_DATA_FILE.read_text().split("\n")
for test, snapshot in zip(ast_repr_get_test_cases(), snapshots, strict=True):
with self.subTest(test_input=test):
self.assertEqual(repr(ast.parse(test)), snapshot)
class CopyTests(unittest.TestCase):
"""Test copying and pickling AST nodes."""
@ -3332,5 +3349,8 @@ class ASTOptimiziationTests(unittest.TestCase):
self.assert_ast(result_code, non_optimized_target, optimized_target)
if __name__ == "__main__":
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == '--snapshot-update':
ast_repr_update_snapshots()
sys.exit(0)
unittest.main()