Skip to content

Commit 4b7c25f

Browse files
Fix linting issues and formatting errors
1 parent cbd01d9 commit 4b7c25f

File tree

5 files changed

+88
-22
lines changed

5 files changed

+88
-22
lines changed

codegen-on-oss/README.md

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,7 @@ import matplotlib.pyplot as plt
8989
import networkx as nx
9090

9191
# Create a filtered call graph visualizer
92-
visualizer = CallGraphFilter(
93-
function_name="process_request",
94-
class_name="ApiHandler",
95-
method_names=["get", "post", "put", "delete"],
96-
max_depth=3
97-
)
92+
visualizer = CallGraphFilter(function_name="process_request", class_name="ApiHandler", method_names=["get", "post", "put", "delete"], max_depth=3)
9893

9994
# Generate the graph
10095
G = visualizer.visualize(codebase)
@@ -115,11 +110,7 @@ import matplotlib.pyplot as plt
115110
import networkx as nx
116111

117112
# Create a call paths visualizer
118-
visualizer = CallPathsBetweenNodes(
119-
start_function_name="start_process",
120-
end_function_name="end_process",
121-
max_depth=5
122-
)
113+
visualizer = CallPathsBetweenNodes(start_function_name="start_process", end_function_name="end_process", max_depth=5)
123114

124115
# Generate the graph
125116
G = visualizer.visualize(codebase)

codegen-on-oss/codegen_on_oss/analyzers/context/graph/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Graph utilities for analyzing code dependencies."""
22

33
import logging
4-
from typing import Any, List, Optional, Set, Tuple
4+
from typing import Any
55

66
import networkx as nx
77

@@ -75,4 +75,3 @@ def find_cycles(graph: nx.DiGraph) -> list:
7575
return list(nx.simple_cycles(graph))
7676
except nx.NetworkXNoCycle:
7777
return []
78-

codegen-on-oss/codegen_on_oss/analyzers/diff_lite.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
"""Lightweight diff utilities for analyzing code changes."""
1+
"""Lightweight diff utilities."""
22

33
import logging
44
from enum import Enum
55
from pathlib import Path
6-
from typing import Dict, List, NamedTuple, Optional, Union
6+
from typing import NamedTuple
77

88
logger = logging.getLogger(__name__)
99

@@ -53,10 +53,10 @@ class DiffLite(NamedTuple):
5353

5454
change_type: ChangeType
5555
path: Path
56-
rename_from: Optional[Path] = None
57-
rename_to: Optional[Path] = None
58-
old_content: Optional[bytes] = None
59-
new_content: Optional[bytes] = None
56+
rename_from: Path | None = None
57+
rename_to: Path | None = None
58+
old_content: bytes | None = None
59+
new_content: bytes | None = None
6060

6161
@property
6262
def is_added(self) -> bool:

codegen-on-oss/codegen_on_oss/analyzers/issues.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@ def to_dict(self) -> dict[str, Any]:
155155
result["suggestion"] = self.suggestion
156156

157157
if self.related_symbols:
158-
result["related_symbols"] = self.related_symbols # type: ignore
158+
result["related_symbols"] = self.related_symbols # type: ignore[assignment]
159159

160160
if self.related_locations:
161-
result["related_locations"] = [ # type: ignore
161+
result["related_locations"] = [ # type: ignore[assignment]
162162
loc.to_dict() for loc in self.related_locations
163163
]
164164

@@ -455,4 +455,3 @@ def create_issue(
455455
symbol=symbol,
456456
suggestion=suggestion,
457457
)
458-
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""Dead code visualization utilities."""
2+
3+
import networkx as nx
4+
5+
from codegen_on_oss.analyzers.context.graph.function import Function
6+
from codegen_on_oss.analyzers.context.graph.import_resolution import Import
7+
from codegen_on_oss.analyzers.context.graph.symbol import Symbol
8+
from codegen_on_oss.visualizers.codebase_visualizer import CodebaseVisualizer
9+
10+
11+
class DeadCodeVisualizer(CodebaseVisualizer):
12+
"""Visualizes dead code in the codebase.
13+
14+
This visualizer identifies functions that have no usages and are not in test files
15+
or decorated. These functions are considered 'dead code' and are added to a directed
16+
graph. The visualizer then explores the dependencies of these dead code functions,
17+
adding them to the graph as well. This process helps to identify not only directly
18+
unused code but also code that might only be used by other dead code (second-order
19+
dead code).
20+
"""
21+
22+
def __init__(self, exclude_test_files: bool = True, exclude_decorated: bool = True):
23+
"""Initialize the dead code visualizer.
24+
25+
Args:
26+
exclude_test_files: Whether to exclude test files from analysis
27+
exclude_decorated: Whether to exclude decorated functions from analysis
28+
"""
29+
self.exclude_test_files = exclude_test_files
30+
self.exclude_decorated = exclude_decorated
31+
32+
def visualize(self, codebase) -> nx.DiGraph:
33+
"""Create a visualization of dead code in the codebase.
34+
35+
Args:
36+
codebase: The codebase to analyze
37+
38+
Returns:
39+
A directed graph representing the dead code and its dependencies
40+
"""
41+
# Create a directed graph to visualize dead and second-order dead code
42+
G: nx.DiGraph = nx.DiGraph()
43+
44+
# First, identify all dead code
45+
dead_code: list[Function] = []
46+
47+
# Iterate through all functions in the codebase
48+
for function in codebase.functions:
49+
# Filter down functions
50+
if self.exclude_test_files and "test" in function.file.filepath:
51+
continue
52+
53+
if self.exclude_decorated and function.decorators:
54+
continue
55+
56+
# Check if the function has no usages
57+
if not function.symbol_usages:
58+
# Add the function to the dead code list
59+
dead_code.append(function)
60+
# Add the function to the graph as dead code
61+
G.add_node(function, color="red")
62+
63+
# Now, find second-order dead code
64+
for symbol in dead_code:
65+
# Get all usages of the dead code symbol
66+
for dep in symbol.dependencies:
67+
if isinstance(dep, Import):
68+
dep = dep.imported_symbol
69+
if isinstance(dep, Symbol) and not (self.exclude_test_files and "test" in getattr(dep, 'name', '')):
70+
G.add_node(dep)
71+
G.add_edge(symbol, dep, color="red")
72+
for usage_symbol in dep.symbol_usages:
73+
if isinstance(usage_symbol, Function) and not (self.exclude_test_files and "test" in usage_symbol.name):
74+
G.add_edge(usage_symbol, dep)
75+
76+
return G
77+

0 commit comments

Comments
 (0)