summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_unparse.py
diff options
context:
space:
mode:
authorJelle Zijlstra <jelle.zijlstra@gmail.com>2024-05-03 13:17:32 (GMT)
committerGitHub <noreply@github.com>2024-05-03 13:17:32 (GMT)
commitca269e58c290be8ca11bb728004ea842d9f85e3a (patch)
tree7af6ddffd5195536343780ef7aeb338ef460501e /Lib/test/test_unparse.py
parent852263e1086748492602a90347ecc0a3925e1dda (diff)
downloadcpython-ca269e58c290be8ca11bb728004ea842d9f85e3a.zip
cpython-ca269e58c290be8ca11bb728004ea842d9f85e3a.tar.gz
cpython-ca269e58c290be8ca11bb728004ea842d9f85e3a.tar.bz2
gh-116126: Implement PEP 696 (#116129)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
Diffstat (limited to 'Lib/test/test_unparse.py')
-rw-r--r--Lib/test/test_unparse.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/Lib/test/test_unparse.py b/Lib/test/test_unparse.py
index bb15f64..35394f2 100644
--- a/Lib/test/test_unparse.py
+++ b/Lib/test/test_unparse.py
@@ -673,6 +673,20 @@ class CosmeticTestCase(ASTTestCase):
self.check_ast_roundtrip("""f'\\'{x:\\"}' """)
self.check_ast_roundtrip("""f'\\'{x:\\\\"}' """)
+ def test_type_params(self):
+ self.check_ast_roundtrip("type A = int")
+ self.check_ast_roundtrip("type A[T] = int")
+ self.check_ast_roundtrip("type A[T: int] = int")
+ self.check_ast_roundtrip("type A[T = int] = int")
+ self.check_ast_roundtrip("type A[T: int = int] = int")
+ self.check_ast_roundtrip("type A[**P] = int")
+ self.check_ast_roundtrip("type A[**P = int] = int")
+ self.check_ast_roundtrip("type A[*Ts] = int")
+ self.check_ast_roundtrip("type A[*Ts = int] = int")
+ self.check_ast_roundtrip("type A[*Ts = *int] = int")
+ self.check_ast_roundtrip("def f[T: int = int, **P = int, *Ts = *int]():\n pass")
+ self.check_ast_roundtrip("class C[T: int = int, **P = int, *Ts = *int]():\n pass")
+
class ManualASTCreationTestCase(unittest.TestCase):
"""Test that AST nodes created without a type_params field unparse correctly."""
@@ -723,6 +737,20 @@ class ManualASTCreationTestCase(unittest.TestCase):
ast.fix_missing_locations(node)
self.assertEqual(ast.unparse(node), "def f[T: int]():\n pass")
+ def test_function_with_type_params_and_default(self):
+ node = ast.FunctionDef(
+ name="f",
+ args=ast.arguments(),
+ body=[ast.Pass()],
+ type_params=[
+ ast.TypeVar("T", default_value=ast.Constant(value=1)),
+ ast.TypeVarTuple("Ts", default_value=ast.Starred(value=ast.Constant(value=1), ctx=ast.Load())),
+ ast.ParamSpec("P", default_value=ast.Constant(value=1)),
+ ],
+ )
+ ast.fix_missing_locations(node)
+ self.assertEqual(ast.unparse(node), "def f[T = 1, *Ts = *1, **P = 1]():\n pass")
+
def test_async_function(self):
node = ast.AsyncFunctionDef(
name="f",
@@ -746,6 +774,20 @@ class ManualASTCreationTestCase(unittest.TestCase):
ast.fix_missing_locations(node)
self.assertEqual(ast.unparse(node), "async def f[T]():\n pass")
+ def test_async_function_with_type_params_and_default(self):
+ node = ast.AsyncFunctionDef(
+ name="f",
+ args=ast.arguments(),
+ body=[ast.Pass()],
+ type_params=[
+ ast.TypeVar("T", default_value=ast.Constant(value=1)),
+ ast.TypeVarTuple("Ts", default_value=ast.Starred(value=ast.Constant(value=1), ctx=ast.Load())),
+ ast.ParamSpec("P", default_value=ast.Constant(value=1)),
+ ],
+ )
+ ast.fix_missing_locations(node)
+ self.assertEqual(ast.unparse(node), "async def f[T = 1, *Ts = *1, **P = 1]():\n pass")
+
class DirectoryTestCase(ASTTestCase):
"""Test roundtrip behaviour on all files in Lib and Lib/test."""