mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
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>
This commit is contained in:
parent
852263e108
commit
ca269e58c2
28 changed files with 1924 additions and 623 deletions
|
@ -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."""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue