Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/rubik/radon
Browse files Browse the repository at this point in the history
  • Loading branch information
rubik committed Jul 14, 2015
2 parents ff4d07d + 81a8d6e commit 7d1da6c
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 5 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG
@@ -1,3 +1,8 @@
1.2.2 (Jul 09, 2015)
--------------------

- Add plugin for flake8 tool: #76.

1.2.1 (May 07, 2015)
--------------------

Expand All @@ -6,8 +11,9 @@
1.2 (Jan 16, 2015)
------------------

- Breaking change regarding to CC of lambda functions and nested functions: #68
- Fix the bug that caused classes with only one method have a CC of 2: #70
- **Backwards incompatible** change regarding to CC of lambda functions and
nested functions: #68.
- Fix the bug that caused classes with only one method have a CC of 2: #70.

1.1 (Sep 6, 2014)
-----------------
Expand Down
24 changes: 24 additions & 0 deletions docs/flake8.rst
@@ -0,0 +1,24 @@
Flake8 plugin
=============

.. program:: flake8

Radon exposes a plugin for the Flake8 tool. In order to use it you will have to
install both `radon` and `flake8`.

To enable the Radon checker, you will have to supply *at least* one of the
following options:

.. option:: --radon-max-cc <int>

Set the cyclomatic complexity threshold. The default value is `10`. Blocks
with a greater complexity will be reported by the tool.

.. option:: --radon-no-assert

Instruct radon not to count `assert` statements towards cyclomatic
complexity. The default behaviour is the opposite.


For more information visit the `Flake8 documentation
<http://flake8.readthedocs.org/en/latest/>`_.
1 change: 1 addition & 0 deletions docs/index.rst
Expand Up @@ -50,6 +50,7 @@ Contents:

intro
commandline
flake8
api
changelog

Expand Down
6 changes: 5 additions & 1 deletion docs/intro.rst
Expand Up @@ -131,5 +131,9 @@ Further Reading
<http://doi.ieeecomputersociety.org/10.1109/2.303623>`_, `postprint
<http://www.ecs.csun.edu/~rlingard/comp589/ColemanPaper.pdf>`_)

3. `Maintainability Index Range and Meaning <http://blogs.msdn.com/b/codeanalysis/archive/2007/11/20/maintainability-index-range-and-meaning.aspx`_.
3. `Maintainability Index Range and Meaning
<http://blogs.msdn.com/b/codeanalysis/archive/2007/11/20/maintainability-index-range-and-meaning.aspx>`_.
Code Analysis Team Blog, blogs.msdn, 20 November 2007.

4. Arie van Deursen, `Think Twice Before Using the “Maintainability Index”
<http://avandeursen.com/2014/08/29/think-twice-before-using-the-maintainability-index/>`_.
2 changes: 1 addition & 1 deletion radon/__init__.py
@@ -1,7 +1,7 @@
'''This module contains the main() function, which is the entry point for the
command line interface.'''

__version__ = '1.2.1'
__version__ = '1.2.2'


def main():
Expand Down
46 changes: 46 additions & 0 deletions radon/complexity.py
Expand Up @@ -98,3 +98,49 @@ def cc_visit_ast(ast_node, **kwargs):
the keyword arguments are directly passed to the visitor.
'''
return ComplexityVisitor.from_ast(ast_node, **kwargs).blocks


class Flake8Checker(object):
'''Entry point for the Flake8 tool.'''

name = 'radon'
_code = 'R701'
_error_tmpl = 'R701: %r is too complex (%d)'
no_assert = False
max_cc = -1

def __init__(self, tree, filename):
'''Accept the AST tree and a filename (unused).'''
self.tree = tree

version = property(lambda self: __import__('radon').__version__)

@classmethod
def add_options(cls, parser): # pragma: no cover
'''Add custom options to the global parser.'''
parser.add_option('--radon-max-cc', default=-1, action='store',
type='int', help='Radon complexity threshold')
parser.add_option('--radon-no-assert', dest='no_assert',
action='store_true', default=False,
help='Radon will ignore assert statements')
parser.config_options.append('radon-max-cc')
parser.config_options.append('radon-no-assert')

@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

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)
for block in visitor.blocks:
if block.complexity > self.max_cc:
text = self._error_tmpl % (block.name, block.complexity)
yield block.lineno, 0, text, type(self)
14 changes: 14 additions & 0 deletions radon/tests/test_complexity_utils.py
@@ -1,4 +1,6 @@
import ast
import operator
import unittest
from paramunittest import *
from radon.complexity import *
from radon.visitors import Class, Function
Expand Down Expand Up @@ -104,3 +106,15 @@ def test_add_closures(self):
names = set(map(operator.attrgetter('name'), with_closures))
self.assertEqual(len(with_closures) - len(blocks), 1)
self.assertTrue('f.inner' in names)


class TestFlake8Checker(unittest.TestCase):

def test_run(self):
c = Flake8Checker(ast.parse(dedent(GENERAL_CASES[0][0])), 'test case')
self.assertEqual(c.max_cc, -1)
self.assertEqual(c.no_assert, False)
self.assertEqual(list(c.run()), [])
c.max_cc = 3
self.assertEqual(list(c.run()), [(7, 0, "R701: 'f' is too complex (4)",
type(c))])
7 changes: 6 additions & 1 deletion setup.py
Expand Up @@ -19,7 +19,12 @@
packages=find_packages(),
tests_require=['tox'],
install_requires=['mando', 'colorama'],
entry_points={'console_scripts': ['radon = radon:main']},
entry_points={
'console_scripts': ['radon = radon:main'],
'flake8.extension': [
'R70 = radon.complexity:Flake8Checker',
],
},
keywords='static analysis code complexity metrics',
classifiers=[
'Development Status :: 5 - Production/Stable',
Expand Down

0 comments on commit 7d1da6c

Please sign in to comment.