fix: resolve CI linting issues across codebase
Some checks failed
CI / test (push) Has been cancelled
Some checks failed
CI / test (push) Has been cancelled
- Remove unused imports (sys, Path, defaultdict, etc.) - Convert Optional types to modern | syntax - Fix trailing whitespace and blank line whitespace issues - Sort imports alphabetically - Add __future__ annotations for type syntax support
This commit is contained in:
36
src/codesnap/core/complexity.py
Normal file
36
src/codesnap/core/complexity.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import ast
|
||||
from codesnap.core.models import FunctionInfo
|
||||
|
||||
|
||||
class ComplexityCalculator:
|
||||
def calculate_function_complexity(self, node: ast.FunctionDef | ast.AsyncFunctionDef, source: str) -> FunctionInfo:
|
||||
lines = source.split("\n")
|
||||
start_line = node.lineno
|
||||
end_line = node.end_lineno or node.lineno
|
||||
|
||||
body = lines[start_line - 1 : end_line]
|
||||
code = "\n".join(body)
|
||||
|
||||
complexity = 1
|
||||
for child in ast.walk(node):
|
||||
if isinstance(child, (ast.If, ast.For, ast.While, ast.Try, ast.With, ast.AsyncWith)):
|
||||
complexity += 1
|
||||
elif isinstance(child, ast.BoolOp):
|
||||
complexity += len(child.values) - 1
|
||||
elif isinstance(child, ast.comprehension):
|
||||
complexity += 1
|
||||
|
||||
params = len(node.args.args)
|
||||
|
||||
docstring = ast.get_docstring(node)
|
||||
|
||||
return FunctionInfo(
|
||||
name=node.name,
|
||||
complexity=complexity,
|
||||
parameters=params,
|
||||
lines=end_line - start_line + 1,
|
||||
code=code,
|
||||
docstring=docstring,
|
||||
)
|
||||
Reference in New Issue
Block a user