Skip to content

Commit

Permalink
feat: cognitive complexity - tidy up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brns committed Mar 12, 2024
1 parent a817585 commit c8f0d22
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 735 deletions.
30 changes: 16 additions & 14 deletions radon/contrib/cognitive_complexity.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,15 @@ def visit_If(self, node):
name = self.get_name(node)

# Check for a lineno attribute
if hasattr(node, "lineno"):
if hasattr(node, "lineno"): # pragma: no cover
self.max_line = node.lineno

self.complexity += 1
if not self.is_elif:
self.complexity += self.nesting

# self.print_node("elif" if self.is_elif else name, "?")

# self.is_elif = False

for field, value in ast.iter_fields(node):

# print(SEP, name, field, value)
nest = 1

# Handle `elif` and `else` clauses
Expand All @@ -179,6 +174,7 @@ def visit_If(self, node):

self.nesting += nest


# same as ast.NodeVisitor.generic_visit
if isinstance(value, list):
for item in value:
Expand All @@ -197,7 +193,7 @@ def visit_ExceptHandler(self, node):
Adds 1 to complexity and a
nesting for the inner body.
'''
print("Visit TryExcept", vars(node))
# print("Visit TryExcept", vars(node))

self.complexity += 1 + self.nesting
self.nesting += 1
Expand All @@ -215,7 +211,7 @@ def visit_Try(self, node):
Only `except` and `else` blocks increase the nesting.
'''
print("Visit Try", vars(node))
# print("Visit Try", vars(node))

for item in node.body:
if isinstance(item, ast.AST):
Expand Down Expand Up @@ -245,7 +241,7 @@ def visit_Match(self, node):
Increases nesting and increments complexity.
'''
print("Vists Match", vars(node))
# print("Vists Match", vars(node))

self.complexity += 1

Expand All @@ -263,7 +259,7 @@ def generic_visit(self, node):
# Get the name of the class
name = self.get_name(node)

print(name, vars(node))
# print("Generic ", name, self.nesting, vars(node))

# Check for a lineno attribute
if hasattr(node, "lineno"):
Expand Down Expand Up @@ -308,11 +304,11 @@ def generic_visit(self, node):
# Recursion
self.complexity += 1

self.print_node(name)
# self.print_node(name)

for field, value in ast.iter_fields(node):

print("iterate generic:", field, value)
# print("iterate generic:", field, value)
nest = name in (
"For",
"While",
Expand Down Expand Up @@ -388,11 +384,14 @@ def visit_FunctionDef(self, node):

# Top level functions are ignored
# but nested functions increase the nesting level
if self.in_closure:
nested = self.in_closure

if nested:
self.nesting += 1
elif not is_decorator:
self.in_closure = 1

# print("Visit FunctionDef", self.nesting)
for child in node.body:

visitor = CognitiveComplexityVisitor(
Expand All @@ -409,7 +408,7 @@ def visit_FunctionDef(self, node):
visitor.complexity + visitor.functions_complexity
)

if self.in_closure:
if nested:
self.nesting -= 1

func = Function(
Expand All @@ -422,6 +421,7 @@ def visit_FunctionDef(self, node):
closures,
body_complexity,
)

self.functions.append(func)

def visit_ClassDef(self, node):
Expand All @@ -438,6 +438,7 @@ def visit_ClassDef(self, node):
classname = node.name
visitors_max_lines = [node.lineno]
inner_classes = []

for child in node.body:
visitor = CognitiveComplexityVisitor(
True,
Expand All @@ -454,6 +455,7 @@ def visit_ClassDef(self, node):
body_complexity += (
visitor.complexity + visitor.functions_complexity
)

visitors_max_lines.append(visitor.max_line)
inner_classes.extend(visitor.classes)

Expand Down
Loading

0 comments on commit c8f0d22

Please sign in to comment.