Skip to content

Commit

Permalink
FIX #3: get local names from desctructive assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
Илья Лебедев committed Nov 15, 2019
1 parent d7d0f2d commit 38bbbbe
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
check:
make -j2 types style
make -j3 test types style

test:
python -m pytest

types:
mypy --strict --implicit-optional .
Expand Down
5 changes: 3 additions & 2 deletions mr_proper/utils/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def is_imported_from_stdlib(name: str, file_ast_tree: ast.Module) -> Optional[bo
def get_local_var_names_from_funcdef(funcdef_node: AnyFuncdef) -> List[str]:
local_vars_names: List[str] = []
for assign_node in get_nodes_from_funcdef_body(funcdef_node, [ast.Assign]):
local_vars_names += [t.id for t in assign_node.targets if isinstance(t, ast.Name)]
for target in assign_node.targets:
local_vars_names += [n.id for n in ast.walk(target) if isinstance(n, ast.Name)]
for annassign_node in get_nodes_from_funcdef_body(funcdef_node, [ast.AnnAssign]):
if isinstance(annassign_node.target, ast.Name):
local_vars_names.append(annassign_node.target.id)
Expand All @@ -74,7 +75,7 @@ def get_local_var_names_from_funcdef(funcdef_node: AnyFuncdef) -> List[str]:
for n in ast.walk(funcdef_node)
if isinstance(n, ast.ExceptHandler) and n.name
}
return local_vars_names
return list(set(local_vars_names))


def get_local_var_names_from_loop(loop_node: Union[ast.comprehension, ast.For]) -> List[str]:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_pure_checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import ast

from mr_proper.public_api import is_function_pure


def test_ok_for_destructive_assignment():
funcdef = ast.parse("""
def foo(a):
b, c = a
return b * c
""".strip()).body[0]
assert is_function_pure(funcdef)

0 comments on commit 38bbbbe

Please sign in to comment.