-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a LineLoc dataclass to store the line num and filename for lin…
…es and functions. Use this to more precisely deal with locations of functions and their references.
- Loading branch information
1 parent
a891661
commit 77ab29f
Showing
4 changed files
with
52 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,36 @@ | ||
from __future__ import annotations | ||
from . import Utils | ||
from .Utils import Func | ||
from .Utils import Func, Line | ||
|
||
def find_func_references(lines: list[str], func: Func) -> list[int]: | ||
def find_func_references(lines: list[Line], func: Func) -> list[Line]: | ||
"""Note that this doesn't include the function's definition.""" | ||
return [i for (i, line) in enumerate(lines) if func.name in line and i != func.line_index] | ||
return [l for l in lines if func.name in l.line_str and func.line_loc != l.line_loc] | ||
|
||
def func_ref_distance(elem: tuple[Func, Line]) -> tuple[int, int, int]: | ||
"""Returns a 'greater' tuple if the 'distance' between the function and reference is greater. | ||
Order of properties by importance: being in diff files, the reference being before the function, | ||
and the line distance between the two. The latter properties are only considered if the func and ref | ||
are in the same file.""" | ||
in_same_file = elem[0].line_loc.filename == elem[1].line_loc.filename | ||
if not in_same_file: | ||
return (1,1,1) | ||
ref_after_func = elem[0].line_loc.line_index < elem[1].line_loc.line_index | ||
line_abs_dist = abs(elem[0].line_loc.line_index - elem[1].line_loc.line_index) | ||
return (-1, (-1 if ref_after_func else 1), line_abs_dist) | ||
|
||
def main() -> None: | ||
lines = Utils.get_lines_all_py_files(["tests.py"]) | ||
funcs: list[Func] = Utils.find_funcs(lines) | ||
funcs_used_once: list[tuple[Func, int]] = [] | ||
funcs_used_once: list[tuple[Func, Line]] = [] | ||
print("\n\nUnused functions:\n") | ||
for func in funcs: | ||
references = find_func_references(lines, func) | ||
if len(references) == 0: | ||
print(f"******{func} is unused******") | ||
elif len(references) == 1: | ||
funcs_used_once.append((func, references[0])) | ||
funcs_used_once.sort(key=lambda f: | ||
((defined_vs_used := f[0].line_index-f[1]) < 0, -abs(defined_vs_used)), | ||
reverse=True) | ||
funcs_used_once.sort(key=func_ref_distance) | ||
print("\n\nFunctions used only once:\n") | ||
for f in funcs_used_once: | ||
print(f"{f[0]} is only referenced at line index {f[1]}") | ||
print(f"Func {f[0].name} (line {f[0].line_loc.line_index} of {f[0].line_loc.filename}) " + | ||
f"is only referenced at line {f[1].line_loc.line_index} of {f[1].line_loc.filename}") |