Skip to content

Commit a352e3c

Browse files
committed
Fixes multiple bugs, closes #332, closes #318
1 parent f027023 commit a352e3c

File tree

7 files changed

+84
-56
lines changed

7 files changed

+84
-56
lines changed

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
We follow Semantic Versions since the `0.1.0` release.
44
We used to have incremental versioning before `0.1.0`.
55

6-
## WIP
6+
## 0.5.1
77

8-
### Misc
8+
### Bugfixes
99

10-
- Fixes build failing on Windows
10+
- Fixes all possible error that happens
11+
because of unset `parent` and `function_type` nodes
1112

1213

1314
## 0.5.0

tests/fixtures/noqa.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,10 @@ class SomeClass(FirstParent, SecondParent, object): # noqa: Z315
178178

179179

180180
class Example(object):
181+
"""Correct class docstring."""
182+
181183
def __init__(self): # noqa: Z439
184+
"""Correct function docstring."""
182185
yield 10
183186

184187

tests/fixtures/regression.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class MqttListener:
2+
"""MqttListener is a wrapper for (hb)mqtt connection."""
3+
4+
def __init__(self, user: str, password: str, topic: str):
5+
"""Init (hb)mqtt client."""
6+
self.mqtt_client = MQTTClient()
7+
self.user = user
8+
self.password = password
9+
self.topic = topic

tests/test_visitors/test_ast/conftest.py

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,8 @@
44
from textwrap import dedent
55

66
import pytest
7-
from pep8ext_naming import NamingChecker
87

9-
10-
class _ClassVisitor(ast.NodeVisitor):
11-
def __init__(self, transformer: NamingChecker) -> None:
12-
super().__init__()
13-
self.transformer = transformer
14-
15-
def visit_ClassDef(self, node: ast.ClassDef) -> None: # noqa: N802
16-
self.transformer.tag_class_functions(node)
17-
self.generic_visit(node)
18-
19-
20-
def _maybe_set_parent(tree: ast.AST) -> ast.AST:
21-
"""
22-
Sets parents for all nodes that do not have this prop.
23-
24-
This step is required due to how `flake8` works.
25-
It does not set the same properties as `ast` module.
26-
27-
This function was the cause of `issue-112`.
28-
29-
.. versionchanged:: 0.0.11
30-
31-
"""
32-
for statement in ast.walk(tree):
33-
for child in ast.iter_child_nodes(statement):
34-
setattr(child, 'parent', statement)
35-
return tree
36-
37-
38-
def _maybe_set_function_type(tree: ast.AST) -> ast.AST:
39-
"""
40-
Sets the function type for methods.
41-
42-
Can set: `method`, `classmethod`, `staticmethod`.
43-
44-
.. versionchanged:: 0.3.0
45-
46-
"""
47-
transformer = _ClassVisitor(NamingChecker(tree, 'stdin'))
48-
transformer.visit(tree)
49-
return tree
8+
from wemake_python_styleguide.transformation.ast_tree import transform
509

5110

5211
@pytest.fixture(scope='session')
@@ -65,11 +24,6 @@ def parse_ast_tree():
6524
6625
Order is important.
6726
"""
68-
transformation_pipeline = [
69-
_maybe_set_parent,
70-
_maybe_set_function_type,
71-
]
72-
7327
def factory(code: str, do_compile: bool = True) -> ast.AST:
7428
code_to_parse = dedent(code)
7529

@@ -78,10 +32,6 @@ def factory(code: str, do_compile: bool = True) -> ast.AST:
7832
# that are validated after the `ast` is processed:
7933
# like double arguments or `break` outside of loops.
8034
compile(code_to_parse, '<filename>', 'exec') # noqa: Z421
81-
tree = ast.parse(code_to_parse)
82-
83-
for transform in transformation_pipeline:
84-
tree = transform(tree)
85-
return tree
35+
return transform(ast.parse(code_to_parse))
8636

8737
return factory

wemake_python_styleguide/checker.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
from wemake_python_styleguide import constants, types
6565
from wemake_python_styleguide import version as pkg_version
6666
from wemake_python_styleguide.options.config import Configuration
67+
from wemake_python_styleguide.transformation.ast_tree import transform
6768
from wemake_python_styleguide.visitors import base
6869
from wemake_python_styleguide.visitors.presets import (
6970
complexity,
@@ -127,7 +128,7 @@ def __init__(
127128
http://flake8.pycqa.org/en/latest/plugin-development/index.html
128129
129130
"""
130-
self.tree = tree
131+
self.tree = transform(tree)
131132
self.filename = filename
132133
self.file_tokens = file_tokens
133134

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# -*- coding: utf-8 -*-
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import ast
4+
5+
from pep8ext_naming import NamingChecker
6+
7+
8+
class _ClassVisitor(ast.NodeVisitor):
9+
def __init__(self, transformer: NamingChecker) -> None:
10+
super().__init__()
11+
self.transformer = transformer
12+
13+
def visit_ClassDef(self, node: ast.ClassDef) -> None: # noqa: N802
14+
self.transformer.tag_class_functions(node)
15+
self.generic_visit(node)
16+
17+
18+
def _set_parent(tree: ast.AST) -> ast.AST:
19+
"""
20+
Sets parents for all nodes that do not have this prop.
21+
22+
This step is required due to how `flake8` works.
23+
It does not set the same properties as `ast` module.
24+
25+
This function was the cause of `issue-112`.
26+
27+
.. versionchanged:: 0.0.11
28+
29+
"""
30+
for statement in ast.walk(tree):
31+
for child in ast.iter_child_nodes(statement):
32+
setattr(child, 'parent', statement)
33+
return tree
34+
35+
36+
def _set_function_type(tree: ast.AST) -> ast.AST:
37+
"""
38+
Sets the function type for methods.
39+
40+
Can set: `method`, `classmethod`, `staticmethod`.
41+
42+
.. versionchanged:: 0.3.0
43+
44+
"""
45+
transformer = _ClassVisitor(NamingChecker(tree, 'stdin'))
46+
transformer.visit(tree)
47+
return tree
48+
49+
50+
def transform(tree: ast.AST) -> ast.AST:
51+
"""
52+
Mutates the given ``ast`` tree.
53+
54+
Applies all possible tranformations.
55+
"""
56+
pipeline = (
57+
_set_parent,
58+
_set_function_type,
59+
)
60+
61+
for tranformation in pipeline:
62+
tree = tranformation(tree)
63+
return tree

0 commit comments

Comments
 (0)