Skip to content

Commit 076cd68

Browse files
committed
lint: Convert Python dead code linter to Python
1 parent 15220ec commit 076cd68

File tree

3 files changed

+42
-22
lines changed

3 files changed

+42
-22
lines changed

test/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ Use the `-v` option for verbose output.
308308
| [`lint-python.sh`](lint/lint-python.sh) | [flake8](https://gitlab.com/pycqa/flake8)
309309
| [`lint-python.sh`](lint/lint-python.sh) | [mypy](https://github.com/python/mypy)
310310
| [`lint-python.sh`](lint/lint-python.sh) | [pyzmq](https://github.com/zeromq/pyzmq)
311+
| [`lint-python-dead-code.py`](lint/lint-python-dead-code.py) | [vulture](https://github.com/jendrikseipp/vulture)
311312
| [`lint-shell.sh`](lint/lint-shell.sh) | [ShellCheck](https://github.com/koalaman/shellcheck)
312313
| [`lint-spelling.sh`](lint/lint-spelling.sh) | [codespell](https://github.com/codespell-project/codespell)
313314

test/lint/lint-python-dead-code.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Copyright (c) 2022 The Bitcoin Core developers
4+
# Distributed under the MIT software license, see the accompanying
5+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
6+
7+
"""
8+
Find dead Python code.
9+
"""
10+
11+
from subprocess import check_output, STDOUT, CalledProcessError
12+
13+
FILES_ARGS = ['git', 'ls-files', '--', '*.py']
14+
15+
16+
def check_vulture_install():
17+
try:
18+
check_output(["vulture", "--version"])
19+
except FileNotFoundError:
20+
print("Skipping Python dead code linting since vulture is not installed. Install by running \"pip3 install vulture\"")
21+
exit(0)
22+
23+
24+
def main():
25+
check_vulture_install()
26+
27+
files = check_output(FILES_ARGS).decode("utf-8").splitlines()
28+
# --min-confidence 100 will only report code that is guaranteed to be unused within the analyzed files.
29+
# Any value below 100 introduces the risk of false positives, which would create an unacceptable maintenance burden.
30+
vulture_args = ['vulture', '--min-confidence=100'] + files
31+
32+
try:
33+
check_output(vulture_args, stderr=STDOUT)
34+
except CalledProcessError as e:
35+
print(e.output.decode("utf-8"), end="")
36+
print("Python dead code detection found some issues")
37+
exit(1)
38+
39+
40+
if __name__ == "__main__":
41+
main()

test/lint/lint-python-dead-code.sh

-22
This file was deleted.

0 commit comments

Comments
 (0)