class documentation

class VariableRenameVisitor(TraverserVisitor): (source)

View In Hierarchy

Rename variables to allow redefinition of variables.

For example, consider this code:

x = 0 f(x)

x = "a" g(x)

It will be transformed like this:

x' = 0 f(x')

x = "a" g(x)

There will be two independent variables (x' and x) that will have separate inferred types. The publicly exposed variant will get the non-suffixed name. This is the last definition at module top level and the first definition (argument) within a function.

Renaming only happens for assignments within the same block. Renaming is performed before semantic analysis, immediately after parsing.

The implementation performs a rudimentary static analysis. The analysis is overly conservative to keep things simple.

Method __init__ Undocumented
Method analyze​_lvalue Process assignment; in particular, keep track of (re)defined names.
Method clear Undocumented
Method current​_block Undocumented
Method enter​_block Undocumented
Method enter​_loop Undocumented
Method enter​_scope Undocumented
Method enter​_try Undocumented
Method flush​_refs Rename all references within the current scope.
Method handle​_arg Store function argument.
Method handle​_def Store new name definition.
Method handle​_ref Store reference to defined name.
Method handle​_refine Store assignment to an existing name (that replaces previous value, if any).
Method is​_nested Undocumented
Method record​_assignment Record assignment to given name and return True if it defines a new variable.
Method reject​_redefinition​_of​_vars​_in​_loop Reject redefinition of variables in the innermost loop.
Method reject​_redefinition​_of​_vars​_in​_scope Make it impossible to redefine defined variables in the current scope.
Method rename​_refs Undocumented
Method visit​_assignment​_stmt Undocumented
Method visit​_block Undocumented
Method visit​_break​_stmt Undocumented
Method visit​_class​_def Undocumented
Method visit​_continue​_stmt Undocumented
Method visit​_for​_stmt Undocumented
Method visit​_func​_def Undocumented
Method visit​_import Undocumented
Method visit​_import​_from Undocumented
Method visit​_mypy​_file Rename variables within a file.
Method visit​_name​_expr Undocumented
Method visit​_try​_stmt Undocumented
Method visit​_while​_stmt Undocumented
Method visit​_with​_stmt Undocumented
Instance Variable block​_id Undocumented
Instance Variable block​_loop​_depth Undocumented
Instance Variable blocks Undocumented
Instance Variable disallow​_redef​_depth Undocumented
Instance Variable loop​_depth Undocumented
Instance Variable num​_reads Undocumented
Instance Variable refs Undocumented
Instance Variable scope​_kinds Undocumented
Instance Variable var​_blocks Undocumented

Inherited from TraverserVisitor:

Method visit​_assert​_stmt Undocumented
Method visit​_assignment​_expr Undocumented
Method visit​_await​_expr Undocumented
Method visit​_backquote​_expr Undocumented
Method visit​_call​_expr Undocumented
Method visit​_cast​_expr Undocumented
Method visit​_comparison​_expr Undocumented
Method visit​_conditional​_expr Undocumented
Method visit​_decorator Undocumented
Method visit​_del​_stmt Undocumented
Method visit​_dict​_expr Undocumented
Method visit​_dictionary​_comprehension Undocumented
Method visit​_exec​_stmt Undocumented
Method visit​_expression​_stmt Undocumented
Method visit​_func Undocumented
Method visit​_generator​_expr Undocumented
Method visit​_if​_stmt Undocumented
Method visit​_index​_expr Undocumented
Method visit​_lambda​_expr Undocumented
Method visit​_list​_comprehension Undocumented
Method visit​_list​_expr Undocumented
Method visit​_member​_expr Undocumented
Method visit​_op​_expr Undocumented
Method visit​_operator​_assignment​_stmt Undocumented
Method visit​_overloaded​_func​_def Undocumented
Method visit​_print​_stmt Undocumented
Method visit​_raise​_stmt Undocumented
Method visit​_return​_stmt Undocumented
Method visit​_reveal​_expr Undocumented
Method visit​_set​_comprehension Undocumented
Method visit​_set​_expr Undocumented
Method visit​_slice​_expr Undocumented
Method visit​_star​_expr Undocumented
Method visit​_super​_expr Undocumented
Method visit​_tuple​_expr Undocumented
Method visit​_type​_application Undocumented
Method visit​_unary​_expr Undocumented
Method visit​_yield​_expr Undocumented
Method visit​_yield​_from​_expr Undocumented
def __init__(self): (source)
def analyze_lvalue(self, lvalue, is_nested=False): (source)

Process assignment; in particular, keep track of (re)defined names.

Args:
is_nested: True for non-outermost Lvalue in a multiple assignment such as
"x, y = ..."
Parameters
lvalue:LvalueUndocumented
is​_nested:boolUndocumented
def clear(self): (source)

Undocumented

def current_block(self): (source)

Undocumented

Returns
intUndocumented
@contextmanager
def enter_block(self): (source)

Undocumented

Returns
Iterator[None]Undocumented
@contextmanager
def enter_loop(self): (source)

Undocumented

Returns
Iterator[None]Undocumented
@contextmanager
def enter_scope(self, kind): (source)

Undocumented

Parameters
kind:intUndocumented
Returns
Iterator[None]Undocumented
@contextmanager
def enter_try(self): (source)

Undocumented

Returns
Iterator[None]Undocumented
def flush_refs(self): (source)

Rename all references within the current scope.

This will be called at the end of a scope.

def handle_arg(self, name): (source)
Store function argument.
Parameters
name:strUndocumented
def handle_def(self, expr): (source)
Store new name definition.
Parameters
expr:NameExprUndocumented
def handle_ref(self, expr): (source)
Store reference to defined name.
Parameters
expr:NameExprUndocumented
def handle_refine(self, expr): (source)
Store assignment to an existing name (that replaces previous value, if any).
Parameters
expr:NameExprUndocumented
def is_nested(self): (source)

Undocumented

Returns
intUndocumented
def record_assignment(self, name, can_be_redefined): (source)

Record assignment to given name and return True if it defines a new variable.

Args:
can_be_redefined: If True, allows assignment in the same block to redefine
this name (if this is a new definition)
Parameters
name:strUndocumented
can​_be​_redefined:boolUndocumented
Returns
boolUndocumented
def reject_redefinition_of_vars_in_loop(self): (source)

Reject redefinition of variables in the innermost loop.

If there is an early exit from a loop, there may be ambiguity about which value may escape the loop. Example where this matters:

while f():

x = 0 if g():

break

x = '' # Error -- not a redefinition

reveal_type(x) # int

This method ensures that the second assignment to 'x' doesn't introduce a new variable.

def reject_redefinition_of_vars_in_scope(self): (source)

Make it impossible to redefine defined variables in the current scope.

This is used if we encounter a function definition that can make it ambiguous which definition is live. Example:

x = 0

def f() -> int:
return x

x = '' # Error -- cannot redefine x across function definition

def rename_refs(self, names, index): (source)

Undocumented

Parameters
names:List[NameExpr]Undocumented
index:intUndocumented
def visit_assignment_stmt(self, s): (source)

Undocumented

Parameters
s:AssignmentStmtUndocumented
def visit_block(self, block): (source)

Undocumented

Parameters
block:BlockUndocumented
def visit_break_stmt(self, stmt): (source)

Undocumented

Parameters
stmt:BreakStmtUndocumented
def visit_class_def(self, cdef): (source)

Undocumented

Parameters
cdef:ClassDefUndocumented
def visit_continue_stmt(self, stmt): (source)

Undocumented

Parameters
stmt:ContinueStmtUndocumented
def visit_for_stmt(self, stmt): (source)

Undocumented

Parameters
stmt:ForStmtUndocumented
def visit_func_def(self, fdef): (source)

Undocumented

Parameters
fdef:FuncDefUndocumented
def visit_import(self, imp): (source)

Undocumented

Parameters
imp:ImportUndocumented
def visit_import_from(self, imp): (source)

Undocumented

Parameters
imp:ImportFromUndocumented
def visit_mypy_file(self, file_node): (source)

Rename variables within a file.

This is the main entry point to this class.

Parameters
file​_node:MypyFileUndocumented
def visit_name_expr(self, expr): (source)

Undocumented

Parameters
expr:NameExprUndocumented
def visit_try_stmt(self, stmt): (source)

Undocumented

Parameters
stmt:TryStmtUndocumented
def visit_while_stmt(self, stmt): (source)

Undocumented

Parameters
stmt:WhileStmtUndocumented
def visit_with_stmt(self, stmt): (source)

Undocumented

Parameters
stmt:WithStmtUndocumented
block_id: int = (source)

Undocumented

block_loop_depth: Dict[int, int] = (source)

Undocumented

blocks: list = (source)

Undocumented

disallow_redef_depth: int = (source)

Undocumented

loop_depth: int = (source)

Undocumented

num_reads: List[Dict[str, int]] = (source)

Undocumented

refs: List[Dict[str, List[List[NameExpr]]]] = (source)

Undocumented

scope_kinds: List[int] = (source)

Undocumented

var_blocks: list = (source)

Undocumented