Skip to content

Commit

Permalink
BANG-225: add more corner cases
Browse files Browse the repository at this point in the history
  • Loading branch information
pshishmarev-bestdoctor committed Jun 13, 2023
1 parent 2598217 commit a2025ba
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
39 changes: 20 additions & 19 deletions flake8_variables_names/ast_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,30 @@
from flake8_variables_names.list_helpers import flat


def extract_name_nodes_from_assignment(assignment_node: ast.Assign) -> List[ast.Name]:
names = []
for target in assignment_node.targets:
if isinstance(target, ast.Name):
names.append(target)
elif isinstance(target, ast.Tuple):
names.extend([dim for dim in target.dims if isinstance(dim, ast.Name)])
return names
def extract_names_from_node(node: Union[ast.expr, ast.stmt]) -> List[ast.Name]:
if isinstance(node, ast.Name):
return [node]
if isinstance(node, ast.Assign):
nodes = []
for target in node.targets:
nodes.extend(extract_names_from_node(target))
return nodes
if isinstance(node, ast.AnnAssign):
return extract_names_from_node(node.target)
if isinstance(node, ast.Starred):
return extract_names_from_node(node.value)
if isinstance(node, ast.Tuple):
nodes = []
for elt in node.elts:
nodes.extend(extract_names_from_node(elt))
return nodes
return []


def get_var_names_from_assignment(
assignment_node: Union[ast.Assign, ast.AnnAssign],
) -> List[Tuple[str, ast.AST]]:
if (
isinstance(assignment_node, ast.AnnAssign)
and isinstance(assignment_node.target, ast.Name)
):
return [(assignment_node.target.id, assignment_node.target)]
elif isinstance(assignment_node, ast.Assign):
return [(n.id, n) for n in extract_name_nodes_from_assignment(assignment_node)]
return []
return [(n.id, n) for n in extract_names_from_node(assignment_node)]


def get_var_names_from_funcdef(funcdef_node: ast.FunctionDef) -> List[Tuple[str, ast.arg]]:
Expand All @@ -46,10 +49,8 @@ def get_var_names_from_for(for_node: ast.For) -> List[Tuple[str, ast.AST]]:

def extract_all_variable_names(ast_tree: ast.AST) -> List[Tuple[str, ast.AST]]:
var_info: List[Tuple[str, ast.AST]] = []
assignments = [n for n in ast.walk(ast_tree) if isinstance(n, ast.Assign)]
assignments = [n for n in ast.walk(ast_tree) if isinstance(n, (ast.Assign, ast.AnnAssign))]
var_info += flat([get_var_names_from_assignment(a) for a in assignments])
ann_assignments = [n for n in ast.walk(ast_tree) if isinstance(n, ast.AnnAssign)]
var_info += flat([get_var_names_from_assignment(a) for a in ann_assignments])
funcdefs = [n for n in ast.walk(ast_tree) if isinstance(n, ast.FunctionDef)]
var_info += flat([get_var_names_from_funcdef(f) for f in funcdefs])
fors = [n for n in ast.walk(ast_tree) if isinstance(n, ast.For)]
Expand Down
3 changes: 3 additions & 0 deletions tests/test_files/ok_names.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
user_id = 23
some_email = 'test@gmail.com'
some_normal_name, _ = -1, -2
some_other_name, *_ = -1, -2, -3
some_different_name, *other_different_name = -1, -2, -3
1 change: 1 addition & 0 deletions tests/test_files/short_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
c = 3
i = 4
x, y, z = 5, 6, 7
w, *q = 1, 2, 3
4 changes: 2 additions & 2 deletions tests/test_variables_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ def test_ok_good_names():

def test_ok_for_short_names_file():
errors = run_validator_for_test_file('short_names.py', use_strict_mode=True)
assert len(errors) == 7
assert len(errors) == 9
errors = run_validator_for_test_file('short_names.py', use_strict_mode=False)
assert len(errors) == 6
assert len(errors) == 8
assert (
get_error_message(errors[0])
== "VNE001 single letter variable names like 'a' are not allowed"
Expand Down

0 comments on commit a2025ba

Please sign in to comment.