Skip to content

Commit

Permalink
Fixes multiple bugs, closes #332, closes #318
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Nov 15, 2018
1 parent f027023 commit a352e3c
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 56 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
We follow Semantic Versions since the `0.1.0` release.
We used to have incremental versioning before `0.1.0`.

## WIP
## 0.5.1

### Misc
### Bugfixes

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


## 0.5.0
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/noqa.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ class SomeClass(FirstParent, SecondParent, object): # noqa: Z315


class Example(object):
"""Correct class docstring."""

def __init__(self): # noqa: Z439
"""Correct function docstring."""
yield 10


Expand Down
9 changes: 9 additions & 0 deletions tests/fixtures/regression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class MqttListener:
"""MqttListener is a wrapper for (hb)mqtt connection."""

def __init__(self, user: str, password: str, topic: str):
"""Init (hb)mqtt client."""
self.mqtt_client = MQTTClient()
self.user = user
self.password = password
self.topic = topic
54 changes: 2 additions & 52 deletions tests/test_visitors/test_ast/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,8 @@
from textwrap import dedent

import pytest
from pep8ext_naming import NamingChecker


class _ClassVisitor(ast.NodeVisitor):
def __init__(self, transformer: NamingChecker) -> None:
super().__init__()
self.transformer = transformer

def visit_ClassDef(self, node: ast.ClassDef) -> None: # noqa: N802
self.transformer.tag_class_functions(node)
self.generic_visit(node)


def _maybe_set_parent(tree: ast.AST) -> ast.AST:
"""
Sets parents for all nodes that do not have this prop.
This step is required due to how `flake8` works.
It does not set the same properties as `ast` module.
This function was the cause of `issue-112`.
.. versionchanged:: 0.0.11
"""
for statement in ast.walk(tree):
for child in ast.iter_child_nodes(statement):
setattr(child, 'parent', statement)
return tree


def _maybe_set_function_type(tree: ast.AST) -> ast.AST:
"""
Sets the function type for methods.
Can set: `method`, `classmethod`, `staticmethod`.
.. versionchanged:: 0.3.0
"""
transformer = _ClassVisitor(NamingChecker(tree, 'stdin'))
transformer.visit(tree)
return tree
from wemake_python_styleguide.transformation.ast_tree import transform


@pytest.fixture(scope='session')
Expand All @@ -65,11 +24,6 @@ def parse_ast_tree():
Order is important.
"""
transformation_pipeline = [
_maybe_set_parent,
_maybe_set_function_type,
]

def factory(code: str, do_compile: bool = True) -> ast.AST:
code_to_parse = dedent(code)

Expand All @@ -78,10 +32,6 @@ def factory(code: str, do_compile: bool = True) -> ast.AST:
# that are validated after the `ast` is processed:
# like double arguments or `break` outside of loops.
compile(code_to_parse, '<filename>', 'exec') # noqa: Z421
tree = ast.parse(code_to_parse)

for transform in transformation_pipeline:
tree = transform(tree)
return tree
return transform(ast.parse(code_to_parse))

return factory
3 changes: 2 additions & 1 deletion wemake_python_styleguide/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
from wemake_python_styleguide import constants, types
from wemake_python_styleguide import version as pkg_version
from wemake_python_styleguide.options.config import Configuration
from wemake_python_styleguide.transformation.ast_tree import transform
from wemake_python_styleguide.visitors import base
from wemake_python_styleguide.visitors.presets import (
complexity,
Expand Down Expand Up @@ -127,7 +128,7 @@ def __init__(
http://flake8.pycqa.org/en/latest/plugin-development/index.html
"""
self.tree = tree
self.tree = transform(tree)
self.filename = filename
self.file_tokens = file_tokens

Expand Down
1 change: 1 addition & 0 deletions wemake_python_styleguide/transformation/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# -*- coding: utf-8 -*-
63 changes: 63 additions & 0 deletions wemake_python_styleguide/transformation/ast_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# -*- coding: utf-8 -*-

import ast

from pep8ext_naming import NamingChecker


class _ClassVisitor(ast.NodeVisitor):
def __init__(self, transformer: NamingChecker) -> None:
super().__init__()
self.transformer = transformer

def visit_ClassDef(self, node: ast.ClassDef) -> None: # noqa: N802
self.transformer.tag_class_functions(node)
self.generic_visit(node)


def _set_parent(tree: ast.AST) -> ast.AST:
"""
Sets parents for all nodes that do not have this prop.
This step is required due to how `flake8` works.
It does not set the same properties as `ast` module.
This function was the cause of `issue-112`.
.. versionchanged:: 0.0.11
"""
for statement in ast.walk(tree):
for child in ast.iter_child_nodes(statement):
setattr(child, 'parent', statement)
return tree


def _set_function_type(tree: ast.AST) -> ast.AST:
"""
Sets the function type for methods.
Can set: `method`, `classmethod`, `staticmethod`.
.. versionchanged:: 0.3.0
"""
transformer = _ClassVisitor(NamingChecker(tree, 'stdin'))
transformer.visit(tree)
return tree


def transform(tree: ast.AST) -> ast.AST:
"""
Mutates the given ``ast`` tree.
Applies all possible tranformations.
"""
pipeline = (
_set_parent,
_set_function_type,
)

for tranformation in pipeline:
tree = tranformation(tree)
return tree

0 comments on commit a352e3c

Please sign in to comment.