Skip to content

Commit 65aae98

Browse files
committed
Implement patchedast for TypeVar syntax in function definition
1 parent 6ef8edd commit 65aae98

3 files changed

Lines changed: 126 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
- #850 Update and pin black version in pre-commit and Github Actions
44
- #851 Bump supported python version to up to Python 3.14
5-
- #852 Implement patchedast for TypeAlias
5+
- #852 Implement patchedast handlers for TypeAlias
6+
- #853 Implement patchedast handlers TypeVar
67

78
# Release 1.14.0
89

rope/refactor/patchedast.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,9 @@ def _handle_function_def_node(self, node, is_async):
491491
children.extend(("@", decorator))
492492
children.extend(["async", "def"] if is_async else ["def"])
493493
children.append(node.name)
494+
type_params = getattr(node, "type_params")
495+
if type_params:
496+
children.extend(["[", *self._child_nodes(type_params, ","), "]"])
494497
children.extend(["(", node.args, ")"])
495498
children.append(":")
496499
children.extend(node.body)
@@ -829,6 +832,14 @@ def _TypeAlias(self, node):
829832
children = ["type", node.name, node.value]
830833
self._handle(node, children)
831834

835+
def _TypeVar(self, node):
836+
children = [node.name]
837+
if node.bound:
838+
children.extend([":", node.bound])
839+
if node.default_value:
840+
children.extend(["=", node.default_value])
841+
self._handle(node, children)
842+
832843

833844
class _Source:
834845
def __init__(self, source):

ropetest/refactor/patchedasttest.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ def assert_single_case_match_block(self, checker, match_type):
3737
"Expr",
3838
])
3939

40+
def assert_function_def_has_one_type_var(self, checker):
41+
checker.check_children(
42+
"FunctionDef",
43+
[
44+
"def",
45+
" ",
46+
"foo",
47+
"",
48+
"[",
49+
"",
50+
"TypeVar",
51+
"",
52+
"]",
53+
"",
54+
"(", "", "arguments", "", ")",
55+
"",
56+
":",
57+
"\n ",
58+
"Pass",
59+
],
60+
)
61+
4062
def test_operator_support_completeness(self):
4163
ast_ops = {
4264
n.__name__
@@ -1524,6 +1546,97 @@ def test_type_alias(self):
15241546
"Subscript",
15251547
])
15261548

1549+
def test_type_var_simple(self):
1550+
source = dedent("""\
1551+
def foo[S, T](x):
1552+
pass
1553+
""")
1554+
ast_frag = patchedast.get_patched_ast(source, True)
1555+
checker = _ResultChecker(self, ast_frag)
1556+
1557+
checker.check_children(
1558+
"FunctionDef",
1559+
[
1560+
"def",
1561+
" ",
1562+
"foo",
1563+
"",
1564+
"[",
1565+
"",
1566+
"TypeVar",
1567+
"",
1568+
",",
1569+
" ",
1570+
"TypeVar",
1571+
"",
1572+
"]",
1573+
"",
1574+
"(", "", "arguments", "", ")",
1575+
"",
1576+
":",
1577+
"\n ",
1578+
"Pass",
1579+
],
1580+
)
1581+
1582+
def test_type_var_with_constraint(self):
1583+
source = dedent("""\
1584+
def foo[T = D](x):
1585+
pass
1586+
""")
1587+
ast_frag = patchedast.get_patched_ast(source, True)
1588+
checker = _ResultChecker(self, ast_frag)
1589+
1590+
self.assert_function_def_has_one_type_var(checker)
1591+
1592+
checker.check_children("TypeVar", [
1593+
"T",
1594+
" ",
1595+
"=",
1596+
" ",
1597+
"Name",
1598+
])
1599+
1600+
def test_type_var_with_default_value(self):
1601+
source = dedent("""\
1602+
def foo[T: (A, B)](x):
1603+
pass
1604+
""")
1605+
ast_frag = patchedast.get_patched_ast(source, True)
1606+
checker = _ResultChecker(self, ast_frag)
1607+
1608+
self.assert_function_def_has_one_type_var(checker)
1609+
1610+
checker.check_children("TypeVar", [
1611+
"T",
1612+
"",
1613+
":",
1614+
" ",
1615+
"Tuple",
1616+
])
1617+
1618+
def test_type_var_with_constraint_and_default_value(self):
1619+
source = dedent("""\
1620+
def foo[T: (A, B) = D](x):
1621+
pass
1622+
""")
1623+
ast_frag = patchedast.get_patched_ast(source, True)
1624+
checker = _ResultChecker(self, ast_frag)
1625+
1626+
self.assert_function_def_has_one_type_var(checker)
1627+
1628+
checker.check_children("TypeVar", [
1629+
"T",
1630+
"",
1631+
":",
1632+
" ",
1633+
"Tuple",
1634+
" ",
1635+
"=",
1636+
" ",
1637+
"Name",
1638+
])
1639+
15271640

15281641
class _ResultChecker:
15291642
def __init__(self, test_case, ast):

0 commit comments

Comments
 (0)