Skip to content

Commit

Permalink
core(ComplexityVisitor): don't count lambdas in cyclomatic complexity
Browse files Browse the repository at this point in the history
As part of #68
  • Loading branch information
rubik committed Nov 13, 2014
1 parent 298b229 commit b78d81d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
5 changes: 5 additions & 0 deletions radon/tests/test_complexity_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,13 @@
finally: pass
''', 3),

# Lambda are not counted anymore as per #68
('''
k = lambda a, b: k(b, a)
''', 1),

('''
k = lambda a, b, c: c if a else b
''', 2),

('''
Expand Down
7 changes: 4 additions & 3 deletions radon/visitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,9 @@ def generic_visit(self, node):
self.complexity += len(node.handlers) + len(node.orelse)
elif name == 'BoolOp':
self.complexity += len(node.values) - 1
# Lambda functions, ifs, with and assert statements count all as 1.
elif name in ('Lambda', 'With', 'If', 'IfExp'):
# Ifs, with and assert statements count all as 1.
# Note: Lambda functions are not counted anymore, see #68
elif name in ('With', 'If', 'IfExp'):
self.complexity += 1
# The For and While blocks count as 1 plus the `else` block.
elif name in ('For', 'While'):
Expand Down Expand Up @@ -237,7 +238,7 @@ def visit_FunctionDef(self, node):
visitor = ComplexityVisitor(off=False, no_assert=self.no_assert)
visitor.visit(child)
clojures.extend(visitor.functions)
# Add general complexity and clojures' complexity
# Add general complexity but not clojures' complexity, see #68
body_complexity += visitor.complexity

func = Function(node.name, node.lineno, node.col_offset,
Expand Down

0 comments on commit b78d81d

Please sign in to comment.