Skip to content

Commit

Permalink
Adds "no __author__" validator (#7)
Browse files Browse the repository at this point in the history
* Adds "no __author__" validator

* Fixes a typo in the comment

* Fixes import order

* Fixes flake8 violations

* Fixes mypy violation
  • Loading branch information
Pavel Vergeev authored and sobolevn committed May 19, 2018
1 parent bbf5944 commit 5d57fb2
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 1 deletion.
9 changes: 9 additions & 0 deletions tests/fixtures/wrong_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
This file contains all broken variable names.
"""

__author__ = 'John Doe' # error here

x = 1 # error here


def fixture():
__author__ = 'John Doe' # no error, because not module metadata


def check_function_args(data, t, *a, **vals): # 4 errors here
result = data + t # error here for `result`
return result
Expand Down Expand Up @@ -41,3 +47,6 @@ def __init__(self, value): # error here

val = Fixture() # error here
print(val.var) # no error here

if val:
__author__ = 'John' # no error here since it's a rare use of module meta
1 change: 1 addition & 0 deletions tests/test_checkers/test_wrong_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ def test_wrong_variables_in_fixture(absolute_path):
assert stdout.count(b'WPS122') == 3
assert stdout.count(b'WPS123') == 2
assert stdout.count(b'WPS124') == 1
assert stdout.count(b'WPS126') == 1
2 changes: 2 additions & 0 deletions wemake_python_styleguide/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
)
from wemake_python_styleguide.visitors.wrong_nested import WrongNestedVisitor
from wemake_python_styleguide.visitors.wrong_variable import (
WrongModuleMetadata,
WrongVariableVisitor,
)

Expand Down Expand Up @@ -42,6 +43,7 @@ def __init__(self, tree: Module, filename: str = '-') -> None:
WrongNestedVisitor,
ComplexityVisitor,
WrongVariableVisitor,
WrongModuleMetadata,
)

def run(self) -> Generator[tuple, None, None]:
Expand Down
4 changes: 4 additions & 0 deletions wemake_python_styleguide/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
'__import__',
))

BAD_MODULE_METADATA_VARIABLES = frozenset((
'__author__',
))

BAD_VARIABLE_NAMES = frozenset((
'data',
'result',
Expand Down
5 changes: 5 additions & 0 deletions wemake_python_styleguide/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ class TooShortAttributeNameViolation(BaseStyleViolation):
_code = 'WPS125'


class WrongModuleMetadataViolation(BaseStyleViolation):
_error_tmpl = '{} Found wrong metadata variable {}'
_code = 'WPS126'


class LocalFolderImportViolation(BaseStyleViolation):
_error_tmpl = '{} Found local folder import "{}"'
_code = 'WPS130'
Expand Down
23 changes: 22 additions & 1 deletion wemake_python_styleguide/visitors/wrong_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

import ast

from wemake_python_styleguide.constants import BAD_VARIABLE_NAMES
from wemake_python_styleguide.constants import (
BAD_MODULE_METADATA_VARIABLES,
BAD_VARIABLE_NAMES,
)
from wemake_python_styleguide.errors import (
TooShortArgumentNameViolation,
TooShortAttributeNameViolation,
TooShortVariableNameViolation,
WrongArgumentNameViolation,
WrongAttributeNameViolation,
WrongModuleMetadataViolation,
WrongVariableNameViolation,
)
from wemake_python_styleguide.helpers.variables import (
Expand Down Expand Up @@ -94,3 +98,20 @@ def visit_Name(self, node: ast.Name):
)

self.generic_visit(node)


class WrongModuleMetadata(BaseNodeVisitor):
"""This class finds wrong metadata information of a module."""

def visit_Assign(self, node: ast.Assign):
"""Used to find the bad metadata variable names."""
node_parent = getattr(node, 'parent')
if not isinstance(node_parent, ast.Module):
return

for target_node in node.targets:
target_node_id = getattr(target_node, 'id')
if target_node_id in BAD_MODULE_METADATA_VARIABLES:
self.add_error(
WrongModuleMetadataViolation(node, text=target_node_id),
)

0 comments on commit 5d57fb2

Please sign in to comment.