Skip to content

Commit

Permalink
fix(visitor): do not count with and async with statements towards CC
Browse files Browse the repository at this point in the history
Fixes #123.
  • Loading branch information
rubik committed Jan 26, 2019
1 parent fc30c72 commit 29a671d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions radon/tests/test_complexity_visitor.py
Expand Up @@ -82,9 +82,10 @@
else: pass
''', 5, {}),

# With and async-with statements no longer count towards CC, see #123
('''
with open('raw.py') as fobj: print(fobj.read())
''', 2, {}),
''', 1, {}),

('''
[i for i in range(4)]
Expand Down Expand Up @@ -271,13 +272,14 @@ def f(a, b):
]

if sys.version_info[:2] >= (3, 5):
# With and async-with statements no longer count towards CC, see #123
SINGLE_FUNCTIONS_CASES.append(
('''
async def f(a, b):
async with open('blabla.log', 'w') as f:
async for i in range(100):
f.write(str(i) + '\\n')
''', (1, 3)),
''', (1, 2)),
)


Expand All @@ -289,6 +291,7 @@ def test_visitor_single_functions(code, expected):


FUNCTIONS_CASES = [
# With and async-with statements no longer count towards CC, see #123
('''
def f(a, b):
return a if b else 2
Expand Down Expand Up @@ -326,7 +329,7 @@ def g(a, b, c):
k += sum(1 / j for j in range(i ** 2) if j > 2)
fobj.write(str(k))
return k - 1
''', (5, 10)),
''', (5, 9)),
]


Expand Down
2 changes: 1 addition & 1 deletion radon/visitors.py
Expand Up @@ -206,7 +206,7 @@ def generic_visit(self, node):
self.complexity += len(node.values) - 1
# Ifs, with and assert statements count all as 1.
# Note: Lambda functions are not counted anymore, see #68
elif name in ('With', 'If', 'IfExp', 'AsyncWith'):
elif name in ('If', 'IfExp'):
self.complexity += 1
# The For and While blocks count as 1 plus the `else` block.
elif name in ('For', 'While', 'AsyncFor'):
Expand Down

0 comments on commit 29a671d

Please sign in to comment.