Skip to content
Merged
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
The final assignments takes precedence in class when renaming
  • Loading branch information
JukkaL committed Jan 18, 2019
commit 496d2ddb1bc7a21c1bfb299e0e7f241d80368e9c
24 changes: 19 additions & 5 deletions mypy/renaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
)
from mypy.traverser import TraverserVisitor

MYPY = False
if MYPY:
from typing_extensions import Final


# Scope kinds
FILE = 0 # type: Final
FUNCTION = 1 # type: Final
CLASS = 2 # type: Final


class VariableRenameVisitor(TraverserVisitor):
"""Rename variables to allow redefinition of variables.
Expand Down Expand Up @@ -59,14 +69,16 @@ def __init__(self) -> None:
self.refs = [] # type: List[Dict[str, List[List[NameExpr]]]]
# Number of reads of the most recent definition of a variable (per scope)
self.num_reads = [] # type: List[Dict[str, int]]
# Kinds of nested scopes (FILE, FUNCTION or CLASS)
self.scope_kinds = [] # type: List[int]

def visit_mypy_file(self, file_node: MypyFile) -> None:
"""Rename variables within a file.

This is the main entry point to this class.
"""
self.clear()
self.enter_scope()
self.enter_scope(FILE)
self.enter_block()

for d in file_node.defs:
Expand All @@ -80,7 +92,7 @@ def visit_func_def(self, fdef: FuncDef) -> None:
# be redefined later, since function could refer to either definition.
self.reject_redefinition_of_vars_in_scope()

self.enter_scope()
self.enter_scope(FUNCTION)
self.enter_block()

for arg in fdef.arguments:
Expand All @@ -99,7 +111,7 @@ def visit_func_def(self, fdef: FuncDef) -> None:

def visit_class_def(self, cdef: ClassDef) -> None:
self.reject_redefinition_of_vars_in_scope()
self.enter_scope()
self.enter_scope(CLASS)
super().visit_class_def(cdef)
self.leave_scope()

Expand Down Expand Up @@ -234,7 +246,7 @@ def flush_refs(self) -> None:

This will be called at the end of a scope.
"""
is_func = self.is_nested()
is_func = self.scope_kinds[-1] == FUNCTION
for name, refs in self.refs[-1].items():
if len(refs) == 1:
# Only one definition -- no renaming neeed.
Expand Down Expand Up @@ -286,15 +298,17 @@ def leave_loop(self) -> None:
def current_block(self) -> int:
return self.blocks[-1]

def enter_scope(self) -> None:
def enter_scope(self, kind: int) -> None:
self.var_blocks.append({})
self.refs.append({})
self.num_reads.append({})
self.scope_kinds.append(kind)

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

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