Skip to content

Commit

Permalink
Closes #1161
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Feb 19, 2020
1 parent 02841c8 commit ed7bff3
Show file tree
Hide file tree
Showing 39 changed files with 101 additions and 108 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ max-complexity = 6
max-line-length = 80

# Self settings:
max-imports = 15
max-imports = 16

# Excluding some directories:
exclude =
Expand Down
36 changes: 18 additions & 18 deletions wemake_python_styleguide/logic/arguments/function_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@
from wemake_python_styleguide.logic.arguments.call_args import get_starred_args


def get_args_without_special_argument(
def is_call_matched_by_arguments(
node: types.AnyFunctionDefAndLambda,
call: ast.Call,
) -> bool:
"""Tells whether ``call`` is matched by arguments of ``node``."""
same_vararg = _has_same_vararg(node, call)
same_kwarg = _has_same_kwarg(node, call)
same_args = _has_same_args(node, call)
same_kw_args = _has_same_kw_args(node, call)
return same_vararg and same_kwarg and same_args and same_kw_args


def _get_args_without_special_argument(
node: types.AnyFunctionDefAndLambda,
) -> List[ast.arg]:
"""Gets ``node`` arguments excluding ``self``, ``cls``, ``mcs``."""
Expand All @@ -20,7 +32,7 @@ def get_args_without_special_argument(
return node_args[1:]


def has_same_vararg(
def _has_same_vararg(
node: types.AnyFunctionDefAndLambda,
call: ast.Call,
) -> bool:
Expand All @@ -37,7 +49,7 @@ def has_same_vararg(
return node.args.vararg == vararg_name # type: ignore


def has_same_kwarg(
def _has_same_kwarg(
node: types.AnyFunctionDefAndLambda,
call: ast.Call,
) -> bool:
Expand All @@ -57,12 +69,12 @@ def has_same_kwarg(
return node.args.kwarg == kwarg_name # type: ignore


def has_same_args( # noqa: WPS231
def _has_same_args( # noqa: WPS231
node: types.AnyFunctionDefAndLambda,
call: ast.Call,
) -> bool:
"""Tells whether ``call`` has the same positional args as ``node``."""
node_args = get_args_without_special_argument(node)
node_args = _get_args_without_special_argument(node)
paired_arguments = zip_longest(call.args, node_args)
for call_arg, func_arg in paired_arguments:
if isinstance(call_arg, ast.Starred):
Expand Down Expand Up @@ -94,7 +106,7 @@ def _clean_call_keyword_args(
return prepared_kw_args, real_kw_args


def has_same_kw_args(
def _has_same_kw_args(
node: types.AnyFunctionDefAndLambda,
call: ast.Call,
) -> bool:
Expand All @@ -107,15 +119,3 @@ def has_same_kw_args(
if func_arg and not call_arg:
return False
return len(real_kw_args) == len(node.args.kwonlyargs)


def is_call_matched_by_arguments(
node: types.AnyFunctionDefAndLambda,
call: ast.Call,
) -> bool:
"""Tells whether ``call`` is matched by arguments of ``node``."""
same_vararg = has_same_vararg(node, call)
same_kwarg = has_same_kwarg(node, call)
same_args = has_same_args(node, call)
same_kw_args = has_same_kw_args(node, call)
return same_vararg and same_kwarg and same_args and same_kw_args
83 changes: 39 additions & 44 deletions wemake_python_styleguide/logic/arguments/super_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,37 @@
from typing import Dict, Optional


def get_keyword_args_by_names(
def is_ordinary_super_call(node: ast.AST, class_name: str) -> bool:
"""
Tells whether super ``call`` is ordinary.
By ordinary we mean:
- either call without arguments::
super().function()
- or call with our class and self arguments::
super(Class, self).function()
Any other combination of arguments is considered as expected by
this function.
"""
call = _get_super_call(node)
if call is None:
return False
args_number = len(call.args) + len(call.keywords)
if args_number == 0:
return True
return args_number == 2 and _is_super_called_with(
call,
type_=class_name,
object_='self',
)


def _get_keyword_args_by_names(
call: ast.Call,
*names: str,
) -> Dict[str, ast.expr]:
Expand All @@ -16,62 +46,27 @@ def get_keyword_args_by_names(
return keyword_args


def is_super_called_with(call: ast.Call, type_: str, object_: str) -> bool:
def _is_super_called_with(call: ast.Call, type_: str, object_: str) -> bool:
"""Tells whether super ``call`` was done with ``type_`` and ``object_``."""
arg1: Optional[ast.expr]
arg2: Optional[ast.expr]
if len(call.args) == 2:
# branch for super(Test, self)
arg1 = call.args[0]
arg2 = call.args[1]
elif len(call.keywords) == 2:
# branch for super(t=Test, obj=self)
keyword_args = get_keyword_args_by_names(call, 't', 'obj')
if len(call.args) == 2: # branch for super(Test, self)
arg1: Optional[ast.expr] = call.args[0]
arg2: Optional[ast.expr] = call.args[1]
elif len(call.keywords) == 2: # branch for super(t=Test, obj=self)
keyword_args = _get_keyword_args_by_names(call, 't', 'obj')
arg1 = keyword_args.get('t')
arg2 = keyword_args.get('obj')
else:
# branch for super(Test, obj=self)
else: # branch for super(Test, obj=self)
arg1 = call.args[0]
arg2 = call.keywords[0].value
is_expected_type = isinstance(arg1, ast.Name) and arg1.id == type_
is_expected_object = isinstance(arg2, ast.Name) and arg2.id == object_
return is_expected_type and is_expected_object


def get_super_call(node: ast.AST) -> Optional[ast.Call]:
def _get_super_call(node: ast.AST) -> Optional[ast.Call]:
"""Returns given ``node`` if it represents ``super`` ``ast.Call``."""
if not isinstance(node, ast.Call):
return None
if not isinstance(node.func, ast.Name) or node.func.id != 'super':
return None
return node


def is_ordinary_super_call(node: ast.AST, class_name: str) -> bool:
"""
Tells whether super ``call`` is ordinary.
By ordinary we mean:
- either call without arguments::
super().function()
- or call with our class and self arguments::
super(Class, self).function()
Any other combination of arguments is considered as unordinary by
this function.
"""
call = get_super_call(node)
if call is None:
return False
args_number = len(call.args) + len(call.keywords)
if args_number == 0:
return True
return args_number == 2 and is_super_called_with(
call,
type_=class_name,
object_='self',
)
2 changes: 1 addition & 1 deletion wemake_python_styleguide/logic/complexity/cognitive.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import ast
from typing import Callable, Tuple

from wemake_python_styleguide.logic import bools, recursion
from wemake_python_styleguide.logic.tree import bools, recursion
from wemake_python_styleguide.types import AnyFunctionDef, AnyNodes

#: Control flow nodes that increment and can be nested.
Expand Down
1 change: 1 addition & 0 deletions wemake_python_styleguide/logic/tree/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# -*- coding: utf-8 -*-
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from ast import AST, Attribute, Call, ClassDef, walk

from wemake_python_styleguide.logic.functions import given_function_called
from wemake_python_styleguide.logic.nodes import get_context
from wemake_python_styleguide.logic.tree.functions import given_function_called
from wemake_python_styleguide.types import AnyFunctionDef


Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion wemake_python_styleguide/visitors/ast/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from wemake_python_styleguide.compat.aliases import FunctionNodes
from wemake_python_styleguide.logic import safe_eval, source
from wemake_python_styleguide.logic.naming.name_nodes import extract_name
from wemake_python_styleguide.logic.operators import (
from wemake_python_styleguide.logic.tree.operators import (
get_parent_ignoring_unary,
unwrap_starred_node,
unwrap_unary_node,
Expand Down
10 changes: 4 additions & 6 deletions wemake_python_styleguide/visitors/ast/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@
from wemake_python_styleguide import constants, types
from wemake_python_styleguide.compat.aliases import AssignNodes, FunctionNodes
from wemake_python_styleguide.compat.functions import get_assign_targets
from wemake_python_styleguide.logic import (
from wemake_python_styleguide.logic import nodes, source, walk
from wemake_python_styleguide.logic.arguments import function_args, super_args
from wemake_python_styleguide.logic.naming import access, name_nodes
from wemake_python_styleguide.logic.tree import (
attributes,
classes,
functions,
nodes,
source,
strings,
walk,
)
from wemake_python_styleguide.logic.arguments import function_args, super_args
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
7 changes: 3 additions & 4 deletions wemake_python_styleguide/visitors/ast/compares.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@

from wemake_python_styleguide.compat.aliases import AssignNodes
from wemake_python_styleguide.compat.functions import get_assign_targets
from wemake_python_styleguide.logic import (
from wemake_python_styleguide.logic import nodes, source
from wemake_python_styleguide.logic.naming.name_nodes import is_same_variable
from wemake_python_styleguide.logic.tree import (
compares,
functions,
ifs,
nodes,
operators,
source,
)
from wemake_python_styleguide.logic.naming.name_nodes import is_same_variable
from wemake_python_styleguide.types import AnyIf, AnyNodes
from wemake_python_styleguide.violations.best_practices import (
HeterogenousCompareViolation,
Expand Down
2 changes: 1 addition & 1 deletion wemake_python_styleguide/visitors/ast/complexity/access.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from typing_extensions import final

from wemake_python_styleguide.logic import attributes
from wemake_python_styleguide.logic.tree import attributes
from wemake_python_styleguide.types import AnyAccess
from wemake_python_styleguide.violations.complexity import (
TooDeepAccessViolation,
Expand Down
2 changes: 1 addition & 1 deletion wemake_python_styleguide/visitors/ast/complexity/calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from typing_extensions import final

from wemake_python_styleguide.logic.calls import parts
from wemake_python_styleguide.logic.tree.calls import parts
from wemake_python_styleguide.violations.complexity import (
TooLongCallChainViolation,
)
Expand Down
2 changes: 1 addition & 1 deletion wemake_python_styleguide/visitors/ast/complexity/counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from typing_extensions import final

from wemake_python_styleguide.constants import MAX_LEN_YIELD_TUPLE
from wemake_python_styleguide.logic.functions import is_method
from wemake_python_styleguide.logic.nodes import get_parent
from wemake_python_styleguide.logic.tree.functions import is_method
from wemake_python_styleguide.types import AnyFunctionDef, AnyImport
from wemake_python_styleguide.violations.complexity import (
TooLongCompareViolation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

from typing_extensions import final

from wemake_python_styleguide.logic import functions
from wemake_python_styleguide.logic.complexity import cognitive
from wemake_python_styleguide.logic.naming import access
from wemake_python_styleguide.logic.nodes import get_parent
from wemake_python_styleguide.logic.tree import functions
from wemake_python_styleguide.types import (
AnyFunctionDef,
AnyFunctionDefAndLambda,
Expand Down
7 changes: 4 additions & 3 deletions wemake_python_styleguide/visitors/ast/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

from typing_extensions import final

from wemake_python_styleguide.logic import ifs, operators, source
from wemake_python_styleguide.logic.compares import CompareBounds
from wemake_python_styleguide.logic.functions import given_function_called
from wemake_python_styleguide.logic import source
from wemake_python_styleguide.logic.tree import ifs, operators
from wemake_python_styleguide.logic.tree.compares import CompareBounds
from wemake_python_styleguide.logic.tree.functions import given_function_called
from wemake_python_styleguide.types import AnyIf, AnyNodes
from wemake_python_styleguide.violations.best_practices import (
SameElementsInConditionViolation,
Expand Down
2 changes: 1 addition & 1 deletion wemake_python_styleguide/visitors/ast/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from typing_extensions import final

from wemake_python_styleguide.logic import exceptions
from wemake_python_styleguide.logic.tree import exceptions
from wemake_python_styleguide.logic.walk import is_contained
from wemake_python_styleguide.types import AnyNodes
from wemake_python_styleguide.violations.best_practices import (
Expand Down
9 changes: 4 additions & 5 deletions wemake_python_styleguide/visitors/ast/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@
FUNCTIONS_BLACKLIST,
LITERALS_BLACKLIST,
)
from wemake_python_styleguide.logic import (
from wemake_python_styleguide.logic import nodes, walk
from wemake_python_styleguide.logic.arguments import function_args
from wemake_python_styleguide.logic.naming import access
from wemake_python_styleguide.logic.tree import (
attributes,
exceptions,
functions,
nodes,
operators,
walk,
)
from wemake_python_styleguide.logic.arguments import function_args
from wemake_python_styleguide.logic.naming import access
from wemake_python_styleguide.types import AnyFunctionDef, AnyNodes
from wemake_python_styleguide.violations import consistency, naming
from wemake_python_styleguide.violations.best_practices import (
Expand Down
3 changes: 2 additions & 1 deletion wemake_python_styleguide/visitors/ast/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
from typing_extensions import final

from wemake_python_styleguide.constants import FUTURE_IMPORTS_WHITELIST
from wemake_python_styleguide.logic import imports, nodes
from wemake_python_styleguide.logic import nodes
from wemake_python_styleguide.logic.naming import access
from wemake_python_styleguide.logic.tree import imports
from wemake_python_styleguide.types import AnyImport, ConfigurationOptions
from wemake_python_styleguide.violations.base import BaseViolation
from wemake_python_styleguide.violations.best_practices import (
Expand Down
7 changes: 4 additions & 3 deletions wemake_python_styleguide/visitors/ast/keywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
from typing_extensions import final

from wemake_python_styleguide.compat.aliases import FunctionNodes
from wemake_python_styleguide.logic import keywords, operators, walk
from wemake_python_styleguide.logic.exceptions import get_exception_name
from wemake_python_styleguide.logic import walk
from wemake_python_styleguide.logic.nodes import get_context, get_parent
from wemake_python_styleguide.logic.variables import (
from wemake_python_styleguide.logic.tree import keywords, operators
from wemake_python_styleguide.logic.tree.exceptions import get_exception_name
from wemake_python_styleguide.logic.tree.variables import (
is_valid_block_variable_definition,
)
from wemake_python_styleguide.types import AnyFunctionDef, AnyNodes, AnyWith
Expand Down
11 changes: 3 additions & 8 deletions wemake_python_styleguide/visitors/ast/loops.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@

from wemake_python_styleguide.compat.aliases import AssignNodes, ForNodes
from wemake_python_styleguide.compat.functions import get_assign_targets
from wemake_python_styleguide.logic import (
nodes,
operators,
slices,
source,
walk,
)
from wemake_python_styleguide.logic.variables import (
from wemake_python_styleguide.logic import nodes, source, walk
from wemake_python_styleguide.logic.tree import operators, slices
from wemake_python_styleguide.logic.tree.variables import (
is_valid_block_variable_definition,
)
from wemake_python_styleguide.types import AnyFor, AnyNodes
Expand Down
Loading

0 comments on commit ed7bff3

Please sign in to comment.