Skip to content

Commit

Permalink
Major error.py refactoring to match flake8 guidelines
Browse files Browse the repository at this point in the history
Changes:
1. Fixes missing parents with `pip` installation, closes #30
2. Now respects `noqa` comments, closes #10
  • Loading branch information
sobolevn committed Jul 13, 2018
1 parent 764940d commit 1f80474
Show file tree
Hide file tree
Showing 17 changed files with 195 additions and 192 deletions.
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ classifiers = [
]

[tool.poetry.plugins."flake8.extension"]
WPS = "wemake_python_styleguide.checker:Checker"
Z = "wemake_python_styleguide.checker:Checker"

[tool.poetry.dependencies]
python = "^3.6"
Expand All @@ -51,7 +51,6 @@ pytest = "^3.6"
flake8-builtins = "^1.4"
flake8-commas = "^2.0"
flake8-quotes = "^1.0"
flake8-pep3101 = "^1.2"
flake8-comprehensions = "^1.4"
flake8-blind-except = "^0.1.1"
flake8-docstrings = "^1.3"
Expand Down
20 changes: 20 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
# -*- coding: utf-8 -*-

import inspect
import os
from operator import itemgetter

import pytest

from wemake_python_styleguide import errors


def _is_error_class(cls) -> bool:
return (
inspect.isclass(cls) and
issubclass(cls, errors.BaseStyleViolation) and
cls is not errors.BaseStyleViolation
)


@pytest.fixture(scope='module')
def all_errors():
"""Loads all errors from the package."""
return list(
map(itemgetter(1), inspect.getmembers(errors, _is_error_class)),
)


@pytest.fixture(scope='session')
def absolute_path():
Expand Down
20 changes: 20 additions & 0 deletions tests/fixtures/noqa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-

"""
This file contains all possible errors.
"""

from .version import get_version # noqa: Z100
def some():
from my_module import some_function # noqa: Z101


del {'a': 1}['a'] # noqa: Z102
raise # noqa: Z103
raise NotImplemented # noqa: Z104
hasattr(object, 'some') # noqa: Z105
value = 1 # noqa: Z106
x = 2 # noqa: Z107
__private = 3 # noqa: Z108
__author__ = 'Nikita Sobolev' # noqa: Z109

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_max_variables_cli_option(absolute_path):
stderr=subprocess.PIPE,
)
stdout, _ = process.communicate()
assert stdout.count(b'WPS150') == 0
assert stdout.count(b'Z150') == 0


def test_max_arguments_cli_option(absolute_path):
Expand All @@ -28,7 +28,7 @@ def test_max_arguments_cli_option(absolute_path):
stderr=subprocess.PIPE,
)
stdout, _ = process.communicate()
assert stdout.count(b'WPS151') == 0
assert stdout.count(b'Z151') == 0


def test_max_returns_cli_option(absolute_path):
Expand All @@ -42,7 +42,7 @@ def test_max_returns_cli_option(absolute_path):
stderr=subprocess.PIPE,
)
stdout, _ = process.communicate()
assert stdout.count(b'WPS153') == 0
assert stdout.count(b'Z153') == 0


def test_max_expressions_cli_options(absolute_path):
Expand All @@ -56,4 +56,4 @@ def test_max_expressions_cli_options(absolute_path):
stderr=subprocess.PIPE,
)
stdout, _ = process.communicate()
assert stdout.count(b'WPS154') == 0
assert stdout.count(b'Z154') == 0
16 changes: 8 additions & 8 deletions tests/test_checkers/test_high_complexity.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,49 @@ def test_too_many_variables_in_fixture(absolute_path):
"""End-to-End test to check variables count."""
filename = absolute_path('fixtures', 'complexity', 'wrong_variables.py')
process = subprocess.Popen(
['flake8', filename],
['flake8', '--select', 'Z', filename],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
stdout, _ = process.communicate()

assert stdout.count(b'WPS150') == 2
assert stdout.count(b'Z202') == 2


def test_too_many_arguments_in_fixture(absolute_path):
"""End-to-End test to check arguments count."""
filename = absolute_path('fixtures', 'complexity', 'wrong_arguments.py')
process = subprocess.Popen(
['flake8', filename],
['flake8', '--select', 'Z', filename],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
stdout, _ = process.communicate()

assert stdout.count(b'WPS151') == 4
assert stdout.count(b'Z203') == 4


def test_too_many_returns_in_fixture(absolute_path):
"""End-to-End test to check returns count."""
filename = absolute_path('fixtures', 'complexity', 'wrong_returns.py')
process = subprocess.Popen(
['flake8', filename],
['flake8', '--select', 'Z', filename],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
stdout, _ = process.communicate()

assert stdout.count(b'WPS153') == 1
assert stdout.count(b'Z205') == 1


def test_too_many_expressions_in_fixture(absolute_path):
"""End-to-End test to check expressions count."""
filename = absolute_path('fixtures', 'complexity', 'wrong_expressions.py')
process = subprocess.Popen(
['flake8', filename],
['flake8', '--select', 'Z', filename],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
stdout, _ = process.communicate()

assert stdout.count(b'WPS154') == 1
assert stdout.count(b'Z206') == 1
29 changes: 29 additions & 0 deletions tests/test_checkers/test_noqa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-

import subprocess


def test_noqa_fixture_disabled(absolute_path):
"""End-to-End test to check that all errors are present."""
filename = absolute_path('fixtures', 'noqa.py')
process = subprocess.Popen(
['flake8', '--disable-noqa', '--select', 'Z', filename],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
stdout, _ = process.communicate()

assert stdout.count(b'Z') > 0


def test_noqa_fixture(absolute_path):
"""End-to-End test to check that `noqa` works."""
filename = absolute_path('fixtures', 'noqa.py')
process = subprocess.Popen(
['flake8', '--select', 'Z', filename],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
stdout, _ = process.communicate()

assert stdout.count(b'Z') == 0
10 changes: 5 additions & 5 deletions tests/test_checkers/test_wrong_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ def test_wrong_variables_in_fixture(absolute_path):
"""End-to-End test to check variable rules."""
filename = absolute_path('fixtures', 'wrong_variable.py')
process = subprocess.Popen(
['flake8', filename],
['flake8', '--select', 'Z', filename],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
stdout, _ = process.communicate()

assert stdout.count(b'WPS120') == 12
assert stdout.count(b'WPS121') == 6
assert stdout.count(b'WPS122') == 1
assert stdout.count(b'WPS123') == 1
assert stdout.count(b'Z106') == 12
assert stdout.count(b'Z107') == 6
assert stdout.count(b'Z108') == 1
assert stdout.count(b'Z109') == 1
23 changes: 0 additions & 23 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,5 @@
# -*- coding: utf-8 -*-

import inspect
from operator import itemgetter

import pytest

from wemake_python_styleguide import errors


def _is_error_class(cls) -> bool:
return (
inspect.isclass(cls) and
issubclass(cls, errors.BaseStyleViolation) and
cls is not errors.BaseStyleViolation
)


@pytest.fixture(scope='module')
def all_errors():
"""Loads all errors from the package."""
return list(
map(itemgetter(1), inspect.getmembers(errors, _is_error_class)),
)


def test_all_unique_error_codes(all_errors):
"""Ensures that all errors have unique error codes."""
Expand Down
9 changes: 2 additions & 7 deletions tests/test_visitors/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,15 @@

import pytest

from wemake_python_styleguide.compat import maybe_set_parent
from wemake_python_styleguide.visitors.base.visitor import BaseNodeVisitor


@pytest.fixture(scope='session')
def parse_ast_tree():
"""Helper function to convert code to ast."""
def factory(code: str) -> ast.AST:
tree = ast.parse(dedent(code))

for statement in ast.walk(tree):
for child in ast.iter_child_nodes(statement):
child.parent = statement

return tree
return maybe_set_parent(ast.parse(dedent(code)))

return factory

Expand Down
29 changes: 0 additions & 29 deletions tests/test_visitors/test_wrong_import/test_dynamic_imports.py

This file was deleted.

3 changes: 2 additions & 1 deletion wemake_python_styleguide/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from ast import Module
from typing import Generator, Tuple

from wemake_python_styleguide.compat import maybe_set_parent
from wemake_python_styleguide.options.config import Configuration
from wemake_python_styleguide.version import version
from wemake_python_styleguide.visitors.high_complexity import ComplexityVisitor
Expand Down Expand Up @@ -38,7 +39,7 @@ class Checker(object):

def __init__(self, tree: Module, filename: str = '-') -> None:
"""Creates new checker instance."""
self.tree = tree
self.tree = maybe_set_parent(tree)
self.filename = filename

self._visitors = (
Expand Down
19 changes: 19 additions & 0 deletions wemake_python_styleguide/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-

"""
This module contains ugly hacks and fixes for version compat issues.
Do not be over-exited to add anything here.
"""

import ast


def maybe_set_parent(tree: ast.AST) -> ast.AST:
"""Sets parents for all nodes that do not have this prop."""
for statement in ast.walk(tree):
for child in ast.iter_child_nodes(statement):
if not hasattr(child, 'parent'): # noqa: Z105
setattr(child, 'parent', statement) # noqa: Z105

return tree
3 changes: 3 additions & 0 deletions wemake_python_styleguide/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@

# Misc:
'copyright',

# Dynamic imports:
'__import__',
))

#: List of module metadata we forbid to use.
Expand Down
Loading

0 comments on commit 1f80474

Please sign in to comment.