Skip to content

Commit

Permalink
Merge pull request #33 from jamescooke/linting
Browse files Browse the repository at this point in the history
Add pylint and fix convention messages
  • Loading branch information
jamescooke committed Jul 7, 2018
2 parents 6c90b1f + 4f2afeb commit 6c0ce31
Show file tree
Hide file tree
Showing 15 changed files with 610 additions and 27 deletions.
548 changes: 548 additions & 0 deletions .pylintrc

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Versioning <http://semver.org/spec/v2.0.0.html>`_.
Unreleased_
-----------

See also `latest documentation
<https://flake8-aaa.readthedocs.io/en/latest/>`_.

0.3.0_ - 2018/06/28
-------------------

Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ tox:
lint:
@echo "=== flake8 ==="
flake8 $(lint_files)
@echo "=== pylint ==="
./run_pylint.sh
@echo "=== isort ==="
isort --quiet --recursive --diff $(lint_files) > isort.out
if [ "$$(wc -l isort.out)" != "0 isort.out" ]; then cat isort.out; exit 1; fi
Expand Down
2 changes: 1 addition & 1 deletion flake8_aaa/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .checker import Checker

__all__ = [
Checker,
'Checker',
]
10 changes: 5 additions & 5 deletions flake8_aaa/act_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .types import ActBlockType


class ActBlock:
class ActBlock(object):
"""
Attributes:
node
Expand All @@ -20,7 +20,7 @@ def __init__(self, node, block_type):
self.block_type = block_type

@classmethod
def build(obj, node):
def build(cls, node):
"""
Args:
node (ast.node): A node, decorated with ``ASTTokens``.
Expand All @@ -32,12 +32,12 @@ def build(obj, node):
NotActionBlock: When ``node`` does not look like an Act block.
"""
if node_is_result_assignment(node):
return obj(node, ActBlockType.result_assignment)
return cls(node, ActBlockType.result_assignment)
elif node_is_pytest_raises(node):
return obj(node, ActBlockType.pytest_raises)
return cls(node, ActBlockType.pytest_raises)

# Check if line marked with '# act'
if node.first_token.line.strip().endswith('# act'):
return obj(node, ActBlockType.marked_act)
return cls(node, ActBlockType.marked_act)

raise NotActionBlock()
2 changes: 1 addition & 1 deletion flake8_aaa/arrange_block.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ast


class ArrangeBlock:
class ArrangeBlock(object):
"""
Attributes:
nodes (list (ast Node))
Expand Down
2 changes: 1 addition & 1 deletion flake8_aaa/assert_block.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class AssertBlock:
class AssertBlock(object):
"""
Attributes:
nodes (list (ast Node))
Expand Down
2 changes: 1 addition & 1 deletion flake8_aaa/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .helpers import find_test_functions, is_test_file


class Checker:
class Checker(object):
"""
Attributes:
ast_tokens (asttokens.ASTTokens): Tokens for the file.
Expand Down
18 changes: 8 additions & 10 deletions flake8_aaa/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .types import ActBlockType


class Function:
class Function(object):
"""
Attributes:
act_block (ActBlock): Act block for the test.
Expand Down Expand Up @@ -57,16 +57,14 @@ def load_act_block(self):
except NotActionBlock:
continue

# Allow `pytest.raises` in assert blocks - ignore all act blocks that
# are `pytest.raises` blocks that are not the first.
if len(act_blocks) > 1:
act_blocks = [act_blocks[0]
] + list(filter(lambda ab: ab.block_type != ActBlockType.pytest_raises, act_blocks[1:]))

if len(act_blocks) < 1:
raise ValidationError(self.node.lineno, self.node.col_offset, 'AAA01 no Act block found in test')

if len(act_blocks) > 1:
raise ValidationError(self.node.lineno, self.node.col_offset, 'AAA02 multiple Act blocks found in test')
# Allow `pytest.raises` in assert blocks - if any of the additional
# act blocks are `pytest.raises` blocks, then raise
if list(filter(lambda ab: ab.block_type != ActBlockType.pytest_raises, act_blocks[1:])):
raise ValidationError(self.node.lineno, self.node.col_offset, 'AAA02 multiple Act blocks found in test')

return act_blocks[0]

Expand All @@ -81,7 +79,7 @@ def load_arrange_block(self):
break
arrange_block.add_node(node)

if len(arrange_block.nodes) > 0:
if arrange_block.nodes:
return arrange_block

return None
Expand All @@ -96,7 +94,7 @@ def load_assert_block(self):
if node.lineno > self.act_block.node.lineno:
assert_block.add_node(node)

if len(assert_block.nodes) > 0:
if assert_block.nodes:
return assert_block

return None
Expand Down
4 changes: 2 additions & 2 deletions flake8_aaa/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __init__(self, *args, **kwargs):
super(TestFuncLister, self).__init__(*args, **kwargs)
self._found_funcs = []

def visit_FunctionDef(self, node):
def visit_FunctionDef(self, node): # pylint: disable=invalid-name
if node.name.startswith('test'):
self._found_funcs.append(node)

Expand Down Expand Up @@ -93,7 +93,7 @@ def node_is_noop(node):
Returns:
bool: Node does nothing.
"""
return (type(node) is ast.Expr and type(node.value) is ast.Str) or (type(node) is ast.Pass)
return (isinstance(node, ast.Expr) and isinstance(node.value, ast.Str)) or isinstance(node, ast.Pass)


def function_is_noop(function_node):
Expand Down
2 changes: 1 addition & 1 deletion flake8_aaa/types.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from enum import Enum

ActBlockType = Enum('ActBlockType', 'marked_act pytest_raises result_assignment')
ActBlockType = Enum('ActBlockType', 'marked_act pytest_raises result_assignment') # pylint: disable=invalid-name
10 changes: 7 additions & 3 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# pip-compile --output-file dev.txt dev.in
#
alabaster==0.7.11
astroid==1.6.5
asttokens==1.1.10
atomicwrites==1.1.5
attrs==18.1.0
Expand All @@ -25,6 +26,7 @@ ipython==6.4.0
isort==4.3.4
jedi==0.12.0 # via ipython
jinja2==2.10
lazy-object-proxy==1.3.1
markupsafe==1.0
mccabe==0.6.1
more-itertools==4.2.0
Expand All @@ -36,15 +38,16 @@ pip-tools==2.0.2
pkginfo==1.4.2 # via twine
pluggy==0.6.0
prompt-toolkit==1.0.15 # via ipython
ptyprocess==0.5.2 # via pexpect
py==1.5.3
ptyprocess==0.6.0 # via pexpect
py==1.5.4
pycodestyle==2.3.1
pyflakes==1.6.0
pygments==2.2.0
pylint==1.9.2
pyparsing==2.2.0
pytest-flake8dir==1.2.0
pytest==3.6.2
pytz==2018.4
pytz==2018.5
requests-toolbelt==0.8.0 # via twine
requests==2.19.1
restructuredtext-lint==1.1.3
Expand All @@ -61,4 +64,5 @@ twine==1.11.0
urllib3==1.23
virtualenv==16.0.0
wcwidth==0.1.7 # via prompt-toolkit
wrapt==1.10.11
yapf==0.22.0
1 change: 1 addition & 0 deletions requirements/test.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Sphinx
flake8
isort
pygments
pylint
pytest
pytest-flake8dir
restructuredtext-lint
Expand Down
8 changes: 6 additions & 2 deletions requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# pip-compile --output-file test.txt test.in
#
alabaster==0.7.11 # via sphinx
astroid==1.6.5 # via pylint
asttokens==1.1.10
atomicwrites==1.1.5 # via pytest
attrs==18.1.0 # via pytest
Expand All @@ -17,19 +18,21 @@ idna==2.7 # via requests
imagesize==1.0.0 # via sphinx
isort==4.3.4
jinja2==2.10 # via sphinx
lazy-object-proxy==1.3.1 # via astroid
markupsafe==1.0 # via jinja2
mccabe==0.6.1
more-itertools==4.2.0 # via pytest
packaging==17.1 # via sphinx
pluggy==0.6.0 # via pytest, tox
py==1.5.3 # via pytest, tox
py==1.5.4 # via pytest, tox
pycodestyle==2.3.1
pyflakes==1.6.0
pygments==2.2.0
pylint==1.9.2
pyparsing==2.2.0 # via packaging
pytest-flake8dir==1.2.0
pytest==3.6.2
pytz==2018.4 # via babel
pytz==2018.5 # via babel
requests==2.19.1 # via sphinx
restructuredtext-lint==1.1.3
six==1.11.0
Expand All @@ -40,4 +43,5 @@ sphinxcontrib-websupport==1.1.0 # via sphinx
tox==3.0.0
urllib3==1.23 # via requests
virtualenv==16.0.0 # via tox
wrapt==1.10.11 # via astroid
yapf==0.22.0
23 changes: 23 additions & 0 deletions run_pylint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

set -eo pipefail

# ERROR CODES
# NO_ERROR=0
FATAL_MESSAGE=1
ERROR_MESSAGE=2
# WARNING_MESSAGE=4
# REFACTOR_MESSAGE=8
CONVENTION_MESSAGE=16
USAGE_ERROR=32

set +e
pylint flake8_aaa
lint_code=$?
set -e

(((lint_code&FATAL_MESSAGE)>0 || (lint_code&ERROR_MESSAGE)>0 || (lint_code&USAGE_ERROR)>0 || (lint_code&CONVENTION_MESSAGE)>0)) && exit $lint_code

echo "Ignoring WARNING_MESSAGE, REFACTOR_MESSAGE"

exit 0

0 comments on commit 6c0ce31

Please sign in to comment.