Skip to content

Commit c474fb4

Browse files
committed
WIP: Rewrite references to inner functions in treetransform
1 parent 49e3864 commit c474fb4

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

mypy/treetransform.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def __init__(self) -> None:
4848
# There may be multiple references to a Var node. Keep track of
4949
# Var translations using a dictionary.
5050
self.var_map = {} # type: Dict[Var, Var]
51+
self.func_map = {} # type: Dict[FuncDef, FuncDef]
5152

5253
def visit_mypy_file(self, node: MypyFile) -> Node:
5354
# NOTE: The 'names' and 'imports' instance variables will be empty!
@@ -98,6 +99,12 @@ def copy_argument(self, argument: Argument) -> Argument:
9899

99100
def visit_func_def(self, node: FuncDef) -> FuncDef:
100101
# Note that a FuncDef must be transformed to a FuncDef.
102+
103+
# These contortions are needed to handle the case of recursive
104+
# references inside the function being transformed.
105+
result = FuncDef(node.name(), node.arguments, node.body, None)
106+
self.func_map[node] = result
107+
101108
new = FuncDef(node.name(),
102109
[self.copy_argument(arg) for arg in node.arguments],
103110
self.block(node.body),
@@ -113,7 +120,8 @@ def visit_func_def(self, node: FuncDef) -> FuncDef:
113120
new.is_class = node.is_class
114121
new.is_property = node.is_property
115122
new.original_def = node.original_def
116-
return new
123+
result.__dict__ = new.__dict__
124+
return result
117125

118126
def visit_func_expr(self, node: FuncExpr) -> Node:
119127
new = FuncExpr([self.copy_argument(arg) for arg in node.arguments],
@@ -332,6 +340,9 @@ def copy_ref(self, new: RefExpr, original: RefExpr) -> None:
332340
target = original.node
333341
if isinstance(target, Var):
334342
target = self.visit_var(target)
343+
elif isinstance(target, FuncDef):
344+
if target in self.func_map:
345+
target = self.func_map[target]
335346
new.node = target
336347
new.is_def = original.is_def
337348

0 commit comments

Comments
 (0)