forked from rubik/radon
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: move
Flake8Checker
into separate module (rubik#200)
* Move `Flake8Checker` into separete module Due to extraction of `flake8_polyfill` into extra requirement, it's import in `radon.complexity` should be conditional. As a consequences, the whole `Flake8Checker` should be conditional, too. Because it's dependent on this import. It is much easier to move the whole thing into it's own module, rather than check for presence of `flake8_polyfill. It is guaranteed that this module could be imported only by `flake8`, when extra requirement is installed. * Add missing `__init__.py` for `radon.contrib` package
- Loading branch information
Anthony
authored
Sep 18, 2020
1 parent
361baea
commit 12f59d2
Showing
5 changed files
with
79 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from flake8_polyfill import options | ||
|
||
from radon.complexity import add_inner_blocks | ||
from radon.visitors import ComplexityVisitor | ||
|
||
|
||
class Flake8Checker(object): | ||
'''Entry point for the Flake8 tool.''' | ||
|
||
name = 'radon' | ||
version = __import__('radon').__version__ | ||
_code = 'R701' | ||
_error_tmpl = 'R701 %r is too complex (%d)' | ||
no_assert = False | ||
show_closures = False | ||
max_cc = -1 | ||
|
||
def __init__(self, tree, filename): | ||
'''Accept the AST tree and a filename (unused).''' | ||
self.tree = tree | ||
|
||
@classmethod | ||
def add_options(cls, parser): # pragma: no cover | ||
'''Add custom options to the global parser.''' | ||
options.register( | ||
parser, | ||
'--radon-max-cc', | ||
default=-1, | ||
action='store', | ||
type='int', | ||
help='Radon complexity threshold', | ||
parse_from_config=True, | ||
) | ||
options.register( | ||
parser, | ||
'--radon-no-assert', | ||
dest='no_assert', | ||
action='store_true', | ||
default=False, | ||
help='Radon will ignore assert statements', | ||
parse_from_config=True, | ||
) | ||
options.register( | ||
parser, | ||
'--radon-show-closures', | ||
dest='show_closures', | ||
action='store_true', | ||
default=False, | ||
help='Add closures/inner classes to the output', | ||
parse_from_config=True, | ||
) | ||
|
||
@classmethod | ||
def parse_options(cls, options): # pragma: no cover | ||
'''Save actual options as class attributes.''' | ||
cls.max_cc = options.radon_max_cc | ||
cls.no_assert = options.no_assert | ||
cls.show_closures = options.show_closures | ||
|
||
def run(self): | ||
'''Run the ComplexityVisitor over the AST tree.''' | ||
if self.max_cc < 0: | ||
if not self.no_assert: | ||
return | ||
self.max_cc = 10 | ||
visitor = ComplexityVisitor.from_ast( | ||
self.tree, no_assert=self.no_assert | ||
) | ||
|
||
blocks = visitor.blocks | ||
if self.show_closures: | ||
blocks = add_inner_blocks(blocks) | ||
|
||
for block in blocks: | ||
if block.complexity > self.max_cc: | ||
text = self._error_tmpl % (block.name, block.complexity) | ||
yield block.lineno, block.col_offset, text, type(self) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters