Skip to content

Commit 1f80474

Browse files
committed
Major error.py refactoring to match flake8 guidelines
Changes: 1. Fixes missing parents with `pip` installation, closes #30 2. Now respects `noqa` comments, closes #10
1 parent 764940d commit 1f80474

File tree

17 files changed

+195
-192
lines changed

17 files changed

+195
-192
lines changed

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ classifiers = [
3737
]
3838

3939
[tool.poetry.plugins."flake8.extension"]
40-
WPS = "wemake_python_styleguide.checker:Checker"
40+
Z = "wemake_python_styleguide.checker:Checker"
4141

4242
[tool.poetry.dependencies]
4343
python = "^3.6"
@@ -51,7 +51,6 @@ pytest = "^3.6"
5151
flake8-builtins = "^1.4"
5252
flake8-commas = "^2.0"
5353
flake8-quotes = "^1.0"
54-
flake8-pep3101 = "^1.2"
5554
flake8-comprehensions = "^1.4"
5655
flake8-blind-except = "^0.1.1"
5756
flake8-docstrings = "^1.3"

tests/conftest.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
11
# -*- coding: utf-8 -*-
22

3+
import inspect
34
import os
5+
from operator import itemgetter
46

57
import pytest
68

9+
from wemake_python_styleguide import errors
10+
11+
12+
def _is_error_class(cls) -> bool:
13+
return (
14+
inspect.isclass(cls) and
15+
issubclass(cls, errors.BaseStyleViolation) and
16+
cls is not errors.BaseStyleViolation
17+
)
18+
19+
20+
@pytest.fixture(scope='module')
21+
def all_errors():
22+
"""Loads all errors from the package."""
23+
return list(
24+
map(itemgetter(1), inspect.getmembers(errors, _is_error_class)),
25+
)
26+
727

828
@pytest.fixture(scope='session')
929
def absolute_path():

tests/fixtures/noqa.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
This file contains all possible errors.
5+
"""
6+
7+
from .version import get_version # noqa: Z100
8+
def some():
9+
from my_module import some_function # noqa: Z101
10+
11+
12+
del {'a': 1}['a'] # noqa: Z102
13+
raise # noqa: Z103
14+
raise NotImplemented # noqa: Z104
15+
hasattr(object, 'some') # noqa: Z105
16+
value = 1 # noqa: Z106
17+
x = 2 # noqa: Z107
18+
__private = 3 # noqa: Z108
19+
__author__ = 'Nikita Sobolev' # noqa: Z109
20+

tests/test_options/test_config.py renamed to tests/test_checkers/test_config.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def test_max_variables_cli_option(absolute_path):
1414
stderr=subprocess.PIPE,
1515
)
1616
stdout, _ = process.communicate()
17-
assert stdout.count(b'WPS150') == 0
17+
assert stdout.count(b'Z150') == 0
1818

1919

2020
def test_max_arguments_cli_option(absolute_path):
@@ -28,7 +28,7 @@ def test_max_arguments_cli_option(absolute_path):
2828
stderr=subprocess.PIPE,
2929
)
3030
stdout, _ = process.communicate()
31-
assert stdout.count(b'WPS151') == 0
31+
assert stdout.count(b'Z151') == 0
3232

3333

3434
def test_max_returns_cli_option(absolute_path):
@@ -42,7 +42,7 @@ def test_max_returns_cli_option(absolute_path):
4242
stderr=subprocess.PIPE,
4343
)
4444
stdout, _ = process.communicate()
45-
assert stdout.count(b'WPS153') == 0
45+
assert stdout.count(b'Z153') == 0
4646

4747

4848
def test_max_expressions_cli_options(absolute_path):
@@ -56,4 +56,4 @@ def test_max_expressions_cli_options(absolute_path):
5656
stderr=subprocess.PIPE,
5757
)
5858
stdout, _ = process.communicate()
59-
assert stdout.count(b'WPS154') == 0
59+
assert stdout.count(b'Z154') == 0

tests/test_checkers/test_high_complexity.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,49 @@ def test_too_many_variables_in_fixture(absolute_path):
77
"""End-to-End test to check variables count."""
88
filename = absolute_path('fixtures', 'complexity', 'wrong_variables.py')
99
process = subprocess.Popen(
10-
['flake8', filename],
10+
['flake8', '--select', 'Z', filename],
1111
stdout=subprocess.PIPE,
1212
stderr=subprocess.PIPE,
1313
)
1414
stdout, _ = process.communicate()
1515

16-
assert stdout.count(b'WPS150') == 2
16+
assert stdout.count(b'Z202') == 2
1717

1818

1919
def test_too_many_arguments_in_fixture(absolute_path):
2020
"""End-to-End test to check arguments count."""
2121
filename = absolute_path('fixtures', 'complexity', 'wrong_arguments.py')
2222
process = subprocess.Popen(
23-
['flake8', filename],
23+
['flake8', '--select', 'Z', filename],
2424
stdout=subprocess.PIPE,
2525
stderr=subprocess.PIPE,
2626
)
2727
stdout, _ = process.communicate()
2828

29-
assert stdout.count(b'WPS151') == 4
29+
assert stdout.count(b'Z203') == 4
3030

3131

3232
def test_too_many_returns_in_fixture(absolute_path):
3333
"""End-to-End test to check returns count."""
3434
filename = absolute_path('fixtures', 'complexity', 'wrong_returns.py')
3535
process = subprocess.Popen(
36-
['flake8', filename],
36+
['flake8', '--select', 'Z', filename],
3737
stdout=subprocess.PIPE,
3838
stderr=subprocess.PIPE,
3939
)
4040
stdout, _ = process.communicate()
4141

42-
assert stdout.count(b'WPS153') == 1
42+
assert stdout.count(b'Z205') == 1
4343

4444

4545
def test_too_many_expressions_in_fixture(absolute_path):
4646
"""End-to-End test to check expressions count."""
4747
filename = absolute_path('fixtures', 'complexity', 'wrong_expressions.py')
4848
process = subprocess.Popen(
49-
['flake8', filename],
49+
['flake8', '--select', 'Z', filename],
5050
stdout=subprocess.PIPE,
5151
stderr=subprocess.PIPE,
5252
)
5353
stdout, _ = process.communicate()
5454

55-
assert stdout.count(b'WPS154') == 1
55+
assert stdout.count(b'Z206') == 1

tests/test_checkers/test_noqa.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import subprocess
4+
5+
6+
def test_noqa_fixture_disabled(absolute_path):
7+
"""End-to-End test to check that all errors are present."""
8+
filename = absolute_path('fixtures', 'noqa.py')
9+
process = subprocess.Popen(
10+
['flake8', '--disable-noqa', '--select', 'Z', filename],
11+
stdout=subprocess.PIPE,
12+
stderr=subprocess.PIPE,
13+
)
14+
stdout, _ = process.communicate()
15+
16+
assert stdout.count(b'Z') > 0
17+
18+
19+
def test_noqa_fixture(absolute_path):
20+
"""End-to-End test to check that `noqa` works."""
21+
filename = absolute_path('fixtures', 'noqa.py')
22+
process = subprocess.Popen(
23+
['flake8', '--select', 'Z', filename],
24+
stdout=subprocess.PIPE,
25+
stderr=subprocess.PIPE,
26+
)
27+
stdout, _ = process.communicate()
28+
29+
assert stdout.count(b'Z') == 0

tests/test_checkers/test_wrong_variable.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ def test_wrong_variables_in_fixture(absolute_path):
77
"""End-to-End test to check variable rules."""
88
filename = absolute_path('fixtures', 'wrong_variable.py')
99
process = subprocess.Popen(
10-
['flake8', filename],
10+
['flake8', '--select', 'Z', filename],
1111
stdout=subprocess.PIPE,
1212
stderr=subprocess.PIPE,
1313
)
1414
stdout, _ = process.communicate()
1515

16-
assert stdout.count(b'WPS120') == 12
17-
assert stdout.count(b'WPS121') == 6
18-
assert stdout.count(b'WPS122') == 1
19-
assert stdout.count(b'WPS123') == 1
16+
assert stdout.count(b'Z106') == 12
17+
assert stdout.count(b'Z107') == 6
18+
assert stdout.count(b'Z108') == 1
19+
assert stdout.count(b'Z109') == 1

tests/test_errors.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,5 @@
11
# -*- coding: utf-8 -*-
22

3-
import inspect
4-
from operator import itemgetter
5-
6-
import pytest
7-
8-
from wemake_python_styleguide import errors
9-
10-
11-
def _is_error_class(cls) -> bool:
12-
return (
13-
inspect.isclass(cls) and
14-
issubclass(cls, errors.BaseStyleViolation) and
15-
cls is not errors.BaseStyleViolation
16-
)
17-
18-
19-
@pytest.fixture(scope='module')
20-
def all_errors():
21-
"""Loads all errors from the package."""
22-
return list(
23-
map(itemgetter(1), inspect.getmembers(errors, _is_error_class)),
24-
)
25-
263

274
def test_all_unique_error_codes(all_errors):
285
"""Ensures that all errors have unique error codes."""

tests/test_visitors/conftest.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,15 @@
66

77
import pytest
88

9+
from wemake_python_styleguide.compat import maybe_set_parent
910
from wemake_python_styleguide.visitors.base.visitor import BaseNodeVisitor
1011

1112

1213
@pytest.fixture(scope='session')
1314
def parse_ast_tree():
1415
"""Helper function to convert code to ast."""
1516
def factory(code: str) -> ast.AST:
16-
tree = ast.parse(dedent(code))
17-
18-
for statement in ast.walk(tree):
19-
for child in ast.iter_child_nodes(statement):
20-
child.parent = statement
21-
22-
return tree
17+
return maybe_set_parent(ast.parse(dedent(code)))
2318

2419
return factory
2520

tests/test_visitors/test_wrong_import/test_dynamic_imports.py

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)