summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_unparse.py
diff options
context:
space:
mode:
authorJelle Zijlstra <jelle.zijlstra@gmail.com>2023-06-16 16:31:23 (GMT)
committerGitHub <noreply@github.com>2023-06-16 16:31:23 (GMT)
commit957a974d4fc1575787e4a29a399a47520d6df6d3 (patch)
tree5bfca946786c529e843f95f18c71e83ff16a603c /Lib/test/test_unparse.py
parent70c075c194d3739ae10ce76265f05fa82ed46487 (diff)
downloadcpython-957a974d4fc1575787e4a29a399a47520d6df6d3.zip
cpython-957a974d4fc1575787e4a29a399a47520d6df6d3.tar.gz
cpython-957a974d4fc1575787e4a29a399a47520d6df6d3.tar.bz2
gh-104799: PEP 695 backward compatibility for ast.unparse (#105846)
Diffstat (limited to 'Lib/test/test_unparse.py')
-rw-r--r--Lib/test/test_unparse.py74
1 files changed, 73 insertions, 1 deletions
diff --git a/Lib/test/test_unparse.py b/Lib/test/test_unparse.py
index 88c7c3a..41a6318 100644
--- a/Lib/test/test_unparse.py
+++ b/Lib/test/test_unparse.py
@@ -1,4 +1,4 @@
-"""Tests for the unparse.py script in the Tools/parser directory."""
+"""Tests for ast.unparse."""
import unittest
import test.support
@@ -625,6 +625,78 @@ class CosmeticTestCase(ASTTestCase):
self.check_src_roundtrip("a, b = [c, d] = e, f = g")
+class ManualASTCreationTestCase(unittest.TestCase):
+ """Test that AST nodes created without a type_params field unparse correctly."""
+
+ def test_class(self):
+ node = ast.ClassDef(name="X", bases=[], keywords=[], body=[ast.Pass()], decorator_list=[])
+ ast.fix_missing_locations(node)
+ self.assertEqual(ast.unparse(node), "class X:\n pass")
+
+ def test_class_with_type_params(self):
+ node = ast.ClassDef(name="X", bases=[], keywords=[], body=[ast.Pass()], decorator_list=[],
+ type_params=[ast.TypeVar("T")])
+ ast.fix_missing_locations(node)
+ self.assertEqual(ast.unparse(node), "class X[T]:\n pass")
+
+ def test_function(self):
+ node = ast.FunctionDef(
+ name="f",
+ args=ast.arguments(posonlyargs=[], args=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]),
+ body=[ast.Pass()],
+ decorator_list=[],
+ returns=None,
+ )
+ ast.fix_missing_locations(node)
+ self.assertEqual(ast.unparse(node), "def f():\n pass")
+
+ def test_function_with_type_params(self):
+ node = ast.FunctionDef(
+ name="f",
+ args=ast.arguments(posonlyargs=[], args=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]),
+ body=[ast.Pass()],
+ decorator_list=[],
+ returns=None,
+ type_params=[ast.TypeVar("T")],
+ )
+ ast.fix_missing_locations(node)
+ self.assertEqual(ast.unparse(node), "def f[T]():\n pass")
+
+ def test_function_with_type_params_and_bound(self):
+ node = ast.FunctionDef(
+ name="f",
+ args=ast.arguments(posonlyargs=[], args=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]),
+ body=[ast.Pass()],
+ decorator_list=[],
+ returns=None,
+ type_params=[ast.TypeVar("T", bound=ast.Name("int"))],
+ )
+ ast.fix_missing_locations(node)
+ self.assertEqual(ast.unparse(node), "def f[T: int]():\n pass")
+
+ def test_async_function(self):
+ node = ast.AsyncFunctionDef(
+ name="f",
+ args=ast.arguments(posonlyargs=[], args=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]),
+ body=[ast.Pass()],
+ decorator_list=[],
+ returns=None,
+ )
+ ast.fix_missing_locations(node)
+ self.assertEqual(ast.unparse(node), "async def f():\n pass")
+
+ def test_async_function_with_type_params(self):
+ node = ast.AsyncFunctionDef(
+ name="f",
+ args=ast.arguments(posonlyargs=[], args=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]),
+ body=[ast.Pass()],
+ decorator_list=[],
+ returns=None,
+ type_params=[ast.TypeVar("T")],
+ )
+ ast.fix_missing_locations(node)
+ self.assertEqual(ast.unparse(node), "async def f[T]():\n pass")
+
class DirectoryTestCase(ASTTestCase):
"""Test roundtrip behaviour on all files in Lib and Lib/test."""