Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions apps/unused_code/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# pyutils-unusedcode

Helper to identify unused code in a pytest repository. It should be run from inside the test repository using this tool.

## Usage
Expand All @@ -9,18 +10,20 @@ pyutils-unusedcode --help
```

## Config file

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
`~/.config/python-utility-scripts/config.yaml`

### Example:
### Example

```yaml
pyutils-unusedcode:
exclude_files:
- "my_exclude_file.py"
- "my_exclude_file.py"
exclude_function_prefix:
- "my_exclude_function_prefix"
- "my_exclude_function_prefix"
```

This would exclude any functions with prefix my_exclude_function_prefix and file my_exclude_file.py from unused code check

To run from CLI with `--exclude-function-prefixes`
Expand All @@ -34,3 +37,12 @@ To run from CLI with `--exclude-files`
```bash
pyutils-unusedcode --exclude-files 'my_exclude_file1.py,my_exclude_file2.py'
```

### Skip single function in file

Add `# skip-unused-code` comment in the function name list to skip it from check.

```python
def my_function(): # skip-unused-code
pass
```
9 changes: 7 additions & 2 deletions apps/unused_code/unused_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Any, Iterable

import click
from ast_comments import parse
Comment thread
myakove marked this conversation as resolved.
from simple_logger.logger import get_logger

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

with open(py_file) as fd:
tree = ast.parse(source=fd.read())
tree = parse(source=fd.read())

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

if any(getattr(item, "value", None) == "# skip-unused-code" for item in func.body):
LOGGER.debug(f"Skipping function {func.name}: found `# skip-unused-code`")
continue

used = False
_func_grep_found = subprocess.check_output(["git", "grep", "-w", func.name], shell=False)
_func_grep_found = subprocess.check_output(["git", "grep", "-wE", f"{func.name}(.*)"], shell=False)

for entry in _func_grep_found.decode().splitlines():
_, _line = entry.split(":", 1)
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ dependencies = [
"jira>=3.6.0,<4",
"tenacity>=9.0.0,<10",
"python-simple-logger>=2.0.0,<3",
"pyhelper-utils>=1.0.1,<2"
"pyhelper-utils>=1.0.1,<2",
Comment thread
dbasunag marked this conversation as resolved.
"ast-comments>=1.2.2",
]

[[project.authors]]
Expand Down
7 changes: 7 additions & 0 deletions tests/unused_code/test_unused_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@ def test_unused_code_function_list_exclude():
LOGGER.info(f"Result output: {result.output}, exit code: {result.exit_code}, exceptions: {result.exception}")
assert result.exit_code == 1
assert "Is not used anywhere in the code" in result.output


def test_unused_code_check_skip_with_comment():
result = get_cli_runner().invoke(get_unused_functions)
LOGGER.info(f"Result output: {result.output}, exit code: {result.exit_code}, exceptions: {result.exception}")
assert result.exit_code == 1
assert "skip_with_comment" not in result.output
4 changes: 4 additions & 0 deletions tests/unused_code/unused_code_file_for_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ def unused_code_check_fail():

def unused_code_check_file():
pass


def skip_with_comment(): # skip-unused-code
pass
11 changes: 11 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.