Skip to content

Commit 567300a

Browse files
authored
Skip unused code for functions (#164)
* feat: support skip single function using `# skip-unused-code` * feat: support skip single function using `# skip-unused-code` * Add ast_comments depds and fix "skip_with_comment" test
1 parent 5f76d48 commit 567300a

File tree

6 files changed

+46
-6
lines changed

6 files changed

+46
-6
lines changed

apps/unused_code/README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# pyutils-unusedcode
2+
23
Helper to identify unused code in a pytest repository. It should be run from inside the test repository using this tool.
34

45
## Usage
@@ -9,18 +10,20 @@ pyutils-unusedcode --help
910
```
1011

1112
## Config file
13+
1214
To skip unused code check on specific files or functions of a repository, a config file with the list of names of such files and function prefixes should be added to
1315
`~/.config/python-utility-scripts/config.yaml`
1416

15-
### Example:
17+
### Example
1618

1719
```yaml
1820
pyutils-unusedcode:
1921
exclude_files:
20-
- "my_exclude_file.py"
22+
- "my_exclude_file.py"
2123
exclude_function_prefix:
22-
- "my_exclude_function_prefix"
24+
- "my_exclude_function_prefix"
2325
```
26+
2427
This would exclude any functions with prefix my_exclude_function_prefix and file my_exclude_file.py from unused code check
2528
2629
To run from CLI with `--exclude-function-prefixes`
@@ -34,3 +37,12 @@ To run from CLI with `--exclude-files`
3437
```bash
3538
pyutils-unusedcode --exclude-files 'my_exclude_file1.py,my_exclude_file2.py'
3639
```
40+
41+
### Skip single function in file
42+
43+
Add `# skip-unused-code` comment in the function name list to skip it from check.
44+
45+
```python
46+
def my_function(): # skip-unused-code
47+
pass
48+
```

apps/unused_code/unused_code.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import Any, Iterable
1010

1111
import click
12+
from ast_comments import parse
1213
from simple_logger.logger import get_logger
1314

1415
from apps.utils import ListParamType, all_python_files, get_util_config
@@ -58,7 +59,7 @@ def process_file(py_file: str, func_ignore_prefix: list[str], file_ignore_list:
5859
return ""
5960

6061
with open(py_file) as fd:
61-
tree = ast.parse(source=fd.read())
62+
tree = parse(source=fd.read())
6263

6364
for func in _iter_functions(tree=tree):
6465
if func_ignore_prefix and is_ignore_function_list(ignore_prefix_list=func_ignore_prefix, function=func):
@@ -69,8 +70,12 @@ def process_file(py_file: str, func_ignore_prefix: list[str], file_ignore_list:
6970
LOGGER.debug(f"Skipping `autouse` fixture function: {func.name}")
7071
continue
7172

73+
if any(getattr(item, "value", None) == "# skip-unused-code" for item in func.body):
74+
LOGGER.debug(f"Skipping function {func.name}: found `# skip-unused-code`")
75+
continue
76+
7277
used = False
73-
_func_grep_found = subprocess.check_output(["git", "grep", "-w", func.name], shell=False)
78+
_func_grep_found = subprocess.check_output(["git", "grep", "-wE", f"{func.name}(.*)"], shell=False)
7479

7580
for entry in _func_grep_found.decode().splitlines():
7681
_, _line = entry.split(":", 1)

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ dependencies = [
1616
"jira>=3.6.0,<4",
1717
"tenacity>=9.0.0,<10",
1818
"python-simple-logger>=2.0.0,<3",
19-
"pyhelper-utils>=1.0.1,<2"
19+
"pyhelper-utils>=1.0.1,<2",
20+
"ast-comments>=1.2.2",
2021
]
2122

2223
[[project.authors]]

tests/unused_code/test_unused_code.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,10 @@ def test_unused_code_function_list_exclude():
3232
LOGGER.info(f"Result output: {result.output}, exit code: {result.exit_code}, exceptions: {result.exception}")
3333
assert result.exit_code == 1
3434
assert "Is not used anywhere in the code" in result.output
35+
36+
37+
def test_unused_code_check_skip_with_comment():
38+
result = get_cli_runner().invoke(get_unused_functions)
39+
LOGGER.info(f"Result output: {result.output}, exit code: {result.exit_code}, exceptions: {result.exception}")
40+
assert result.exit_code == 1
41+
assert "skip_with_comment" not in result.output

tests/unused_code/unused_code_file_for_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ def unused_code_check_fail():
44

55
def unused_code_check_file():
66
pass
7+
8+
9+
def skip_with_comment(): # skip-unused-code
10+
pass

uv.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)