Skip to content

Commit

Permalink
Refactors out duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Jul 27, 2019
1 parent 71ec637 commit b29c2fd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
15 changes: 14 additions & 1 deletion wemake_python_styleguide/logic/naming/name_nodes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# -*- coding: utf-8 -*-

import ast
from typing import List, Optional
import itertools
from typing import Iterable, List, Optional

from wemake_python_styleguide.compat.functions import get_assign_targets
from wemake_python_styleguide.types import AnyAssign


def is_same_variable(left: ast.AST, right: ast.AST) -> bool:
Expand Down Expand Up @@ -29,6 +33,15 @@ def get_assigned_name(node: ast.AST) -> Optional[str]:
return None


def flat_variable_names(nodes: Iterable[AnyAssign]) -> Iterable[str]:
"""Returns flat variable names from several nodes."""
return itertools.chain.from_iterable((
get_variables_from_node(target)
for node in nodes
for target in get_assign_targets(node)
))


def get_variables_from_node(node: ast.AST) -> List[str]:
"""Gets the assigned names from the list of nodes."""
names: List[str] = []
Expand Down
8 changes: 2 additions & 6 deletions wemake_python_styleguide/visitors/ast/blocks.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# -*- coding: utf-8 -*-

import ast
import itertools
from collections import defaultdict
from typing import ClassVar, DefaultDict, Set, Union, cast

from typing_extensions import final

from wemake_python_styleguide.compat.functions import get_assign_targets
from wemake_python_styleguide.logic.naming.name_nodes import (
flat_variable_names,
get_variables_from_node,
)
from wemake_python_styleguide.logic.nodes import get_context, get_parent
Expand Down Expand Up @@ -127,10 +126,7 @@ def visit_locals(self, node: Union[AnyAssign, ast.arg]) -> None:
if isinstance(node, ast.arg):
names = {node.arg}
else:
names = set(itertools.chain.from_iterable((
_extract_names(target)
for target in get_assign_targets(node)
)))
names = set(flat_variable_names([node]))

self._scope(node, names, is_local=True)
self.generic_visit(node)
Expand Down
22 changes: 5 additions & 17 deletions wemake_python_styleguide/visitors/ast/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import ast
from collections import Counter
from typing import ClassVar, Container, FrozenSet, List, Optional, Tuple
from typing import ClassVar, FrozenSet, List, Optional, Tuple

import astor
from typing_extensions import final
Expand All @@ -17,7 +17,7 @@
strings,
walk,
)
from wemake_python_styleguide.logic.naming import access
from wemake_python_styleguide.logic.naming import access, name_nodes
from wemake_python_styleguide.violations import best_practices as bp
from wemake_python_styleguide.violations import consistency, oop
from wemake_python_styleguide.visitors import base, decorators
Expand Down Expand Up @@ -254,20 +254,6 @@ def visit_ClassDef(self, node: ast.ClassDef) -> None:
self._check_attributes_shadowing(node)
self.generic_visit(node)

# TODO: can be moved to logic, if is used anywhere else
def _flat_assign_names(
self,
nodes: List[types.AnyAssign],
) -> Container[str]:
flat_assigns = []
for attribute in nodes:
targets = get_assign_targets(attribute)
flat_assigns.extend([
at.id for at in targets
if isinstance(at, ast.Name)
])
return set(flat_assigns)

def _get_attributes(
self,
node: ast.ClassDef,
Expand All @@ -288,7 +274,9 @@ def _get_attributes(

def _check_attributes_shadowing(self, node: ast.ClassDef) -> None:
class_attributes, instance_attributes = self._get_attributes(node)
class_attribute_names = self._flat_assign_names(class_attributes)
class_attribute_names = set(
name_nodes.flat_variable_names(class_attributes),
)

for instance_attr in instance_attributes:
if instance_attr.attr in class_attribute_names:
Expand Down

0 comments on commit b29c2fd

Please sign in to comment.