Skip to content

Commit

Permalink
Merge pull request PyCQA#38 from PyCQA/bug/36
Browse files Browse the repository at this point in the history
Add support for PEP 0492 keywords
  • Loading branch information
sigmavirus24 committed Jan 25, 2016
2 parents c9bb16e + 4155ad6 commit 98036c6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
8 changes: 6 additions & 2 deletions mccabe.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ def visitFunctionDef(self, node):
self.graphs["%s%s" % (self.classname, node.name)] = self.graph
self.reset()

visitAsyncFunctionDef = visitFunctionDef

def visitClassDef(self, node):
old_classname = self.classname
self.classname += node.name + "."
Expand All @@ -158,13 +160,13 @@ def visitSimpleStatement(self, node):
visitAssert = visitAssign = visitAugAssign = visitDelete = visitPrint = \
visitRaise = visitYield = visitImport = visitCall = visitSubscript = \
visitPass = visitContinue = visitBreak = visitGlobal = visitReturn = \
visitSimpleStatement
visitAwait = visitSimpleStatement

def visitLoop(self, node):
name = "Loop %d" % node.lineno
self._subgraph(node, name)

visitFor = visitWhile = visitLoop
visitAsyncFor = visitFor = visitWhile = visitLoop

def visitIf(self, node):
name = "If %d" % node.lineno
Expand Down Expand Up @@ -216,6 +218,8 @@ def visitWith(self, node):
self.appendPathNode(name)
self.dispatch_list(node.body)

visitAsyncWith = visitWith


class McCabeChecker(object):
"""McCabe cyclomatic complexity checker."""
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[wheel]
universal = 1

[aliases]
test = pytest
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def get_long_description():
license='Expat license',
py_modules=['mccabe'],
zip_safe=False,
test_suite='test_mccabe',
setup_requires=['pytest-runner'],
tests_require=['pytest'],
entry_points={
'flake8.extension': [
'C90 = mccabe:McCabeChecker',
Expand Down
22 changes: 22 additions & 0 deletions test_mccabe.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
except ImportError:
from io import StringIO

import pytest

import mccabe
from mccabe import get_code_complexity

Expand Down Expand Up @@ -84,6 +86,19 @@ def c():
print(4)
"""

async_keywords = """\
async def foobar(a, b, c):
await whatever(a, b, c)
if await b:
pass
async with c:
pass
async for x in a:
pass
"""


def get_complexity_number(snippet, strio, max=0):
"""Get the complexity number from the printed string."""
Expand Down Expand Up @@ -164,6 +179,13 @@ def test_trivial(self):
def test_try_else(self):
self.assert_complexity(try_else, 4)

@pytest.mark.skipif(sys.version_info < (3, 5),
reason="Async keywords are only valid on Python 3.5+")
def test_async_keywords(self):
"""Validate that we properly process async keyword usage."""
complexity = get_complexity_number(async_keywords, self.strio)
self.assertEqual(complexity, 3)


class RegressionTests(unittest.TestCase):
def setUp(self):
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tox]
envlist =
py26,py27,py33,py34,flake8
py26,py27,py33,py34,py35,flake8

[testenv]
deps =
Expand Down

0 comments on commit 98036c6

Please sign in to comment.