8000 Optionally allow redefinition of variable with different type by JukkaL · Pull Request #6197 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Optionally allow redefinition of variable with different type #6197

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

Merged
merged 56 commits into from
Jan 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
8e49664
Support redefinition of local variables with a different type
JukkaL Sep 20, 2018
3b409fc
Add docstring
JukkaL Sep 20, 2018
71cb8ea
Allow redefinitions of form "x = f(x)"
JukkaL Sep 20, 2018
35f56ae
Fix special cases
JukkaL Sep 20, 2018
2524777
Add test cases
JukkaL Sep 20, 2018
ab70d0d
Fix redefinition with 'break' and 'continue'
JukkaL Sep 20, 2018
525d5f4
Fix issues
JukkaL Sep 20, 2018
2c614a2
Fix some edge cases
JukkaL Sep 20, 2018
bf2f869
Support top-level redefinition through renaming (WIP)
JukkaL Sep 28, 2018
9e2c6b8
Remove obsolete code
JukkaL Sep 28, 2018
2b885da
Cleanup
JukkaL Sep 28, 2018
712669c
Merge transform code into a single class
JukkaL Sep 28, 2018
e0b7df0
Code cleanup
JukkaL Sep 28, 2018
f153809
Fix issues
JukkaL Sep 28, 2018
856d401
Fix issues
JukkaL Sep 28, 2018
865f1dd
Work around type check error
JukkaL Sep 28, 2018
2deff14
Merge branch 'master' into redefine-var
JukkaL Oct 4, 2018
27a1e57
Fix merge issue
JukkaL Oct 4, 2018
f04e37a
Fix lint
JukkaL Oct 4, 2018
1f9f62e
Fix more tests
JukkaL Oct 4, 2018
9ba82a9
Rename semanal_redef -> renaming
JukkaL Oct 4, 2018
fbf052e
Only perform redefinition if variable has been read
JukkaL Oct 9, 2018
1116b47
A few fixes
JukkaL Oct 9, 2018
70a3085
Add hacky workaround for 'self'
JukkaL Oct 9, 2018
cfad560
Merge branch 'master' into redefine-var
JukkaL Oct 9, 2018
b57960b
Remove unused things
JukkaL Oct 10, 2018
580a774
Revert some unnecessary changes
JukkaL Oct 10, 2018
051e695
Merge commit '3bea26f89a66834f42caaa90cf8db44f99d39dcc' into redefine…
JukkaL Dec 9, 2018
77495fb
Merge branch 'master' into redefine-var
JukkaL Dec 9, 2018
dc03143
Fix test case
JukkaL Dec 9, 2018
4ceed87
Merge branch 'master' into redefine-var
JukkaL Dec 19, 2018
ac00e6d
Merge branch 'master' into redefine-var
JukkaL Dec 20, 2018
20fd0f9
Use `if int():` consistently
JukkaL Dec 20, 2018
3843cea
Fix some test cases
JukkaL Dec 20, 2018
5cfaf9c
Add flag for allowing redefinitions
JukkaL Dec 20, 2018
83ba85f
Use the flag
JukkaL Dec 20, 2018
43a0362
Fix tests
JukkaL Dec 20, 2018
59b3413
Fix test cases
JukkaL Dec 20, 2018
7e3eff9
Add test case for turning flag off explicitly
JukkaL Dec 20, 2018
afa1096
Rename flag to --[dis]allow-redefinition (no plural)
JukkaL Dec 20, 2018
916288c
Fix semantic analyzer tests
JukkaL Dec 21, 2018
459187e
Update test case
JukkaL Dec 21, 2018
e165e46
Merge branch 'master' into redefine-var
JukkaL Jan 15, 2019
0a119b3
Merge branch 'master' into redefine-var
JukkaL Jan 15, 2019
fadb322
Merge branch 'master' into redefine-var
JukkaL Jan 17, 2019
76ad071
Fix brokenness from merge
JukkaL Jan 17, 2019
979a17c
Respond to review
JukkaL Jan 18, 2019
ae0043f
Clarify code
JukkaL Jan 18, 2019
512c1ed
Test additional special case
JukkaL Jan 18, 2019
1f7e36d
Add test case
JukkaL Jan 18, 2019
72043ae
Fix with statements
JukkaL Jan 18, 2019
8c30c90
Remove unnecessary check
JukkaL Jan 18, 2019
e390f38
Add comment
JukkaL Jan 18, 2019
bc0ee19
Ensure renaming cannot happen across scopes in class
JukkaL Jan 18, 2019
54b86d8
Remove unnecessary call
JukkaL Jan 18, 2019
496d2dd
The final assignments takes precedence in class when renaming
JukkaL Jan 18, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge transform code into a single class
  • Loading branch information
JukkaL committed Oct 4, 2018
commit 712669c5debfdcd622c1ce7d3fbfe367736dc287
2 changes: 1 addition & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
from mypy import experiments
from mypy.plugin import Plugin, ClassDefContext, SemanticAnalyzerPluginInterface 10000
from mypy.util import get_prefix, correct_relative_import, unmangle
from mypy.semanal_shared import SemanticAnalyzerInterface, set_callable_name, VarDefAnalyzer
from mypy.semanal_shared import SemanticAnalyzerInterface, set_callable_name
from mypy.scope import Scope
from mypy.semanal_namedtuple import NamedTupleAnalyzer, NAMEDTUPLE_PROHIBITED_NAMES
from mypy.semanal_typeddict import TypedDictAnalyzer
Expand Down
170 changes: 138 additions & 32 deletions mypy/semanal_redef.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

from mypy.nodes import (
Block, AssignmentStmt, NameExpr, MypyFile, FuncDef, Lvalue, ListExpr, TupleExpr, TempNode,
WhileStmt, ForStmt, BreakStmt, ContinueStmt, TryStmt, WithStmt, StarExpr, ImportFrom, MemberExpr,
IndexExpr
WhileStmt, ForStmt, BreakStmt, ContinueStmt, TryStmt, WithStmt, StarExpr, ImportFrom,
MemberExpr, IndexExpr
)
from mypy.traverser import TraverserVisitor
from mypy.semanal_shared import VarDefAnalyzer


class VariableRenameVisitor(TraverserVisitor):
Expand All @@ -16,15 +15,22 @@ class VariableRenameVisitor(TraverserVisitor):

x = 0
f(x)

x = ''
g(x)

It can be renamed to this:
It would be transformed to this:

x' = 0
f(x')

x~ = 0
f(x~)
x = ''
g(X)
g(x)

There will be two independent variables (x' and x) that will have separate
inferred types.

Renaming only happens for assignments within the same block.

TODO:
* renaming in functions (argument redef)
Expand Down Expand Up @@ -53,69 +59,82 @@ class VariableRenameVisitor(TraverserVisitor):
"""

def __init__(self) -> None:
self.var_def_analyzer = VarDefAnalyzer()
# Counter for labeling new blocks
self.block_id = 0
self.disallow_redef_depth = 0
self.loop_depth = 0
# Map block id to loop depth.
self.block_loop_depth = {} # type: Dict[int, int]
# Stack of block ids being processed.
self.blocks = [] # type: List[int]
# List of scopes; each scope maps short name to block id.
self.var_blocks = [{}] # type: List[Dict[str, int]]
# Variables which have no assigned value yet (e.g., "x: t" but no assigment).
# Assignment in any block is considered an initialization.
self.uninitialized = set() # type: Set[str]

# References to variables
self.refs = [] # type: List[Dict[str, List[List[NameExpr]]]]

def visit_mypy_file(self, file_node: MypyFile) -> None:
self.var_def_analyzer.clear()
self.var_def_analyzer.enter_block()
self.clear()
self.enter_bloc 10000 k()
self.refs.append({})
for d in file_node.defs:
d.accept(self)
self.flush_refs()
self.var_def_analyzer.leave_block()
self.leave_block()

def visit_func_def(self, fdef: FuncDef) -> None:
# Conservatively do not allow variable defined before a function to
# be redefined later, since function could refer to either definition.
self.var_def_analyzer.reject_redefinition_of_vars_in_scope()
self.var_def_analyzer.process_assignment(fdef.name(), can_be_redefined=False)
self.var_def_analyzer.enter_scope()
self.reject_redefinition_of_vars_in_scope()
self.process_assignment(fdef.name(), can_be_redefined=False)
self.enter_scope()
self.refs.append({})

for arg in fdef.arguments:
name = arg.variable.name()
self.var_def_analyzer.process_assignment(arg.variable.name(),
can_be_redefined=True)
self.process_assignment(arg.variable.name(), can_be_redefined=True)
self.handle_arg(name)

self.visit_block(fdef.body, enter=False)
self.flush_refs()
self.var_def_analyzer.leave_scope()
self.leave_scope()

def visit_block(self, block: Block, enter: bool = True) -> None:
if enter:
self.var_def_analyzer.enter_block()
self.enter_block()
super().visit_block(block)
if enter:
self.var_def_analyzer.leave_block()
self.leave_block()

def visit_while_stmt(self, stmt: WhileStmt) -> None:
self.var_def_analyzer.enter_loop()
self.enter_loop()
super().visit_while_stmt(stmt)
self.var_def_analyzer.leave_loop()
self.leave_loop()

def visit_for_stmt(self, stmt: ForStmt) -> None:
self.analyze_lvalue(stmt.index, True)
self.var_def_analyzer.enter_loop()
self.enter_loop()
super().visit_for_stmt(stmt)
self.var_def_analyzer.leave_loop()
self.leave_loop()

def visit_break_stmt(self, stmt: BreakStmt) -> None:
self.var_def_analyzer.reject_redefinition_of_vars_in_loop()
self.reject_redefinition_of_vars_in_loop()

def visit_continue_stmt(self, stmt: ContinueStmt) -> None:
self.var_def_analyzer.reject_redefinition_of_vars_in_loop()
self.reject_redefinition_of_vars_in_loop()

def visit_try_stmt(self, stmt: TryStmt) -> None:
self.var_def_analyzer.enter_with_or_try()
self.enter_with_or_try()
super().visit_try_stmt(stmt)
self.var_def_analyzer.leave_with_or_try()
self.leave_with_or_try()

def visit_with_stmt(self, stmt: WithStmt) -> None:
self.var_def_analyzer.enter_with_or_try()
self.enter_with_or_try()
super().visit_with_stmt(stmt)
self.var_def_analyzer.leave_with_or_try()
self.leave_with_or_try()

def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
has_initializer = not isinstance(s.rvalue, TempNode)
Expand All @@ -126,7 +145,7 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
def analyze_lvalue(self, lvalue: Lvalue, has_initializer: bool) -> None:
if isinstance(lvalue, NameExpr):
name = lvalue.name
is_new = self.var_def_analyzer.process_assignment(name, True, not has_initializer)
is_new = self.process_assignment(name, True, not has_initializer)
if is_new: # and name != '_': # Underscore gets special handling later
self.handle_def(lvalue)
else:
Expand All @@ -144,7 +163,7 @@ def analyze_lvalue(self, lvalue: Lvalue, has_initializer: bool) -> None:

def visit_import_from(self, imp: ImportFrom) -> None:
for id, as_id in imp.names:
self.var_def_analyzer.process_assignment(as_id or id, False, False)
self.process_assignment(as_id or id, False, False)

def visit_name_expr(self, expr: NameExpr) -> None:
self.handle_ref(expr)
Expand All @@ -166,7 +185,7 @@ def handle_ref(self, expr: NameExpr) -> None:
names[-1].append(expr)

def flush_refs(self) -> None:
is_func = self.var_def_analyzer.is_nested()
is_func = self.is_nested()
for name, refs in self.refs[-1].items():
if len(refs) == 1:
continue
Expand All @@ -183,3 +202,90 @@ def rename_refs(self, names: List[NameExpr], index: int) -> None:
new_name = name + "'" * (index + 1)
for expr in names:
expr.name = new_name

# ----

def clear(self) -> None:
self.blocks = []
self.var_blocks = [{}]

def enter_block(self) -> None:
self.block_id += 1
self.blocks.append(self.block_id)
self.block_loop_depth[self.block_id] = self.loop_depth

def leave_block(self) -> None:
self.blocks.pop()

def enter_with_or_try(self) -> None:
self.disallow_redef_depth += 1

def leave_with_or_try(self) -> None:
self.disallow_redef_depth -= 1

def enter_loop(self) -> None:
self.loop_depth += 1

def leave_loop(self) -> None:
self.loop_depth -= 1

def current_block(self) -> int:
return self.blocks[-1]

def enter_scope(self) -> None:
self.var_blocks.append({})

def leave_scope(self) -> None:
self.var_blocks.pop()

def is_nested(self) -> int:
return len(self.var_blocks) > 1

def reject_redefinition_of_vars_in_scope(self) -> None:
"""Make it impossible to redefine defined variables in the current scope.

This is used if we encounter a function definition or break/continue that
can make it ambiguous which definition is live.
"""
var_blocks = self.var_blocks[-1]
for key in var_blocks:
var_blocks[key] = -1

def reject_redefinition_of_vars_in_loop(self) -> None:
var_blocks = self.var_blocks[-1]
for key, block in var_blocks.items():
if self.block_loop_depth.get(block) == self.loop_depth:
var_blocks[key] = -1

def process_assignment(self, name: str, can_be_redefined: bool, no_value: bool = False) -> bool:
"""Record assignment to given name and return True if it defines a new name.

Args:
can_be_redefined: If True, allows assignment in the same block to redefine the name
no_value: If True, the first assignment we encounter will not be considered to redefine
this but to initilize it (in any block)
"""
if self.disallow_redef_depth > 0:
can_be_redefined = False
block = self.current_block()
var_blocks = self.var_blocks[-1]
uninitialized = self.uninitialized
existing_no_value = name in self.uninitialized
if no_value:
uninitialized.add(name)
else:
uninitialized.discard(name)
if name not in var_blocks:
# New definition
if can_be_redefined:
var_blocks[name] = block
else:
# This doesn't support arbitrary redefinition.
# TODO: Make this less restricted.
var_blocks[name] = -1
return True
elif var_blocks[name] == block and not existing_no_value:
# Redefinition
return True
else:
return False
112 changes: 0 additions & 112 deletions mypy/semanal_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,115 +150,3 @@ def set_callable_name(sig: Type, fdef: FuncDef) -> Type:
return sig.with_name(fdef.name())
else:
return sig


class VarDefAnalyzer:
"""Helper class that keeps track of variable redefinitions.

This decides whether an assignment to an existing variable should define a new variable.
This can happen if there are multiple assignments to a variable within the same block:

x = 0
x = str(x) # Defines a new 'x'

Since we now have two distinct 'x' variables, they can have independent inferred types.
"""

def __init__(self) -> None:
self.block_id = 0
self.disallow_redef_depth = 0
self.loop_depth = 0
# Map block id to loop depth.
self.block_loop_depth = {} # type: Dict[int, int]
# Stack of block ids being processed.
self.blocks = [] # type: List[int]
# List of scopes; each scope maps short name to block id.
self.var_blocks = [{}] # type: List[Dict[str, int]]
# Variables which have no assigned value yet (e.g., "x: t" but no assigment).
# Assignment in any block is considered an initialization.
self.uninitialized = set() # type: Set[str]

def clear(self) -> None:
self.blocks = []
self.var_blocks = [{}]

def enter_block(self) -> None:
self.block_id += 1
self.blocks.append(self.block_id)
self.block_loop_depth[self.block_id] = self.loop_depth

def leave_block(self) -> None:
self.blocks.pop()

def enter_with_or_try(self) -> None:
self.disallow_redef_depth += 1

def leave_with_or_try(self) -> None:
self.disallow_redef_depth -= 1

def enter_loop(self) -> None:
self.loop_depth += 1

def leave_loop(self) -> None:
self.loop_depth -= 1

def current_block(self) -> int:
return self.blocks[-1]

def enter_scope(self) -> None:
self.var_blocks.append({})

def leave_scope(self) -> None:
self.var_blocks.pop()

def is_nested(self) -> int:
return len(self.var_blocks) > 1

def reject_redefinition_of_vars_in_scope(self) -> None:
"""Make it impossible to redefine defined variables in the current scope.

This is used if we encounter a function definition or break/continue that
can make it ambiguous which definition is live.
"""
var_blocks = self.var_blocks[-1]
for key in var_blocks:
var_blocks[key] = -1

def reject_redefinition_of_vars_in_loop(self) -> None:
var_blocks = self.var_blocks[-1]
for key, block in var_blocks.items():
if self.block_loop_depth.get(block) == self.loop_depth:
var_blocks[key] = -1

def process_assignment(self, name: str, can_be_redefined: bool, no_value: bool = False) -> bool:
"""Record assignment to given name and return True if it defines a new name.

Args:
can_be_redefined: If True, allows assignment in the same block to redefine the name
no_value: If True, the first assignment we encounter will not be considered to redefine
this but to initilize it (in any block)
"""
if self.disallow_redef_depth > 0:
can_be_redefined = False
block = self.current_block()
var_blocks = self.var_blocks[-1]
uninitialized = self.uninitialized
existing_no_value = name in self.uninitialized
if no_value:
uninitialized.add(name)
else:
uninitialized.discard(name)
if name not in var_blocks:
# New definition
if can_be_redefined:
var_blocks[name] = block
else:
# This doesn't support arbitrary redefinition.
# TODO: Make this less restricted.
var_blocks[name] = -1
return True
elif var_blocks[name] == block and not existing_no_value:
# Redefinition
return True
else:
return False
0