[Python-checkins] (no subject)
Batuhan Taşkaya
webhook-mailer at python.org
Sun Mar 15 15:57:01 EDT 2020
To: python-checkins at python.org
Subject:
bpo-38870: Implement support for ast.FunctionType in ast.unparse (GH-19016)
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0
https://github.com/python/cpython/commit/5b66ec166b81c8a77286da2c0d17be3579c3=
069a
commit: 5b66ec166b81c8a77286da2c0d17be3579c3069a
branch: master
author: Batuhan Ta=C5=9Fkaya <47358913+isidentical at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-03-15T19:56:57Z
summary:
bpo-38870: Implement support for ast.FunctionType in ast.unparse (GH-19016)
files:
M Lib/ast.py
M Lib/test/test_unparse.py
diff --git a/Lib/ast.py b/Lib/ast.py
index 077eb92abb402..e347d8b9dab1d 100644
--- a/Lib/ast.py
+++ b/Lib/ast.py
@@ -741,6 +741,15 @@ def _write_docstring_and_traverse_body(self, node):
def visit_Module(self, node):
self._write_docstring_and_traverse_body(node)
=20
+ def visit_FunctionType(self, node):
+ with self.delimit("(", ")"):
+ self.interleave(
+ lambda: self.write(", "), self.traverse, node.argtypes
+ )
+
+ self.write(" -> ")
+ self.traverse(node.returns)
+
def visit_Expr(self, node):
self.fill()
self.set_precedence(_Precedence.YIELD, node.value)
diff --git a/Lib/test/test_unparse.py b/Lib/test/test_unparse.py
index 3d87cfb6daeef..23292640086b9 100644
--- a/Lib/test/test_unparse.py
+++ b/Lib/test/test_unparse.py
@@ -122,10 +122,10 @@ class ASTTestCase(unittest.TestCase):
def assertASTEqual(self, ast1, ast2):
self.assertEqual(ast.dump(ast1), ast.dump(ast2))
=20
- def check_ast_roundtrip(self, code1):
- ast1 =3D ast.parse(code1)
+ def check_ast_roundtrip(self, code1, **kwargs):
+ ast1 =3D ast.parse(code1, **kwargs)
code2 =3D ast.unparse(ast1)
- ast2 =3D ast.parse(code2)
+ ast2 =3D ast.parse(code2, **kwargs)
self.assertASTEqual(ast1, ast2)
=20
def check_invalid(self, node, raises=3DValueError):
@@ -330,6 +330,14 @@ def test_constant_tuples(self):
ast.Constant(value=3D(1, 2, 3), kind=3DNone), "(1, 2, 3)"
)
=20
+ def test_function_type(self):
+ for function_type in (
+ "() -> int",
+ "(int, int) -> int",
+ "(Callable[complex], More[Complex(call.to_typevar())]) -> None"
+ ):
+ self.check_ast_roundtrip(function_type, mode=3D"func_type")
+
=20
class CosmeticTestCase(ASTTestCase):
"""Test if there are cosmetic issues caused by unnecesary additions"""
More information about the Python-checkins
mailing list