-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
gh-104799: PEP 695 backward compatibility for ast.unparse #105846
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 @@ def test_star_expr_assign_target_multiple(self): | |
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=[]), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #105858 would make this so much easier; manual creation like this feels ridiculously tedious at the moment |
||
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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth also adding a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feels a bit repetitive, it's the same at the AST level. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough! |
||
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.""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Enable :func:`ast.unparse` to unparse function and class definitions created | ||
without the new ``type_params`` field from :pep:`695`. Patch by Jelle | ||
Zijlstra. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume it would be harder to just make sure that these nodes always have a
type_params
attribute, even if the relevant argument wasn't passed to the constructor?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that might be something to pursue for 3.13 though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#105858