Skip to content

Commit 798809a

Browse files
authored
Add type hints for the get_unused_code function and the fields of Item. (#361)
* Test code with pytype.
1 parent d21ad35 commit 798809a

File tree

5 files changed

+39
-8
lines changed

5 files changed

+39
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# next (unreleased)
2+
3+
* Add typehints for `get_unused_code` and the fields of the `Item` class (John Doknjas, #361).
4+
15
# 2.13 (2024-10-02)
26

37
* Add support for Python 3.13 (Jendrik Seipp, #369).

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,16 @@ Vulture will automatically look for a `pyproject.toml` in the current working di
191191

192192
To use a `pyproject.toml` in another directory, you can use the `--config path/to/pyproject.toml` flag.
193193

194+
It's also possible to use vulture programatically. For example:
195+
196+
``` python
197+
import vulture
198+
199+
v = vulture.Vulture()
200+
v.scavenge(['.'])
201+
unused_code = v.get_unused_code() # returns a list of `Item` objects
202+
```
203+
194204
## Integrations
195205

196206
You can use a [pre-commit](https://pre-commit.com/#install) hook to run

tests/test_pytype.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import subprocess
2+
import sys
3+
4+
import pytest
5+
6+
7+
@pytest.mark.skipif(
8+
sys.version_info >= (3, 13), reason="needs Python < 3.13 for pytype"
9+
)
10+
def test_pytype():
11+
assert subprocess.run(["pytype", "vulture/core.py"]).returncode == 0

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ deps =
1515
pint # Use latest version to catch API changes.
1616
pytest
1717
pytest-cov
18+
pytype ; python_version < '3.13'
1819
commands =
1920
pytest {posargs}
2021
# Install package as wheel in all envs (https://hynek.me/articles/turbo-charge-tox/).

vulture/core.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import re
66
import string
77
import sys
8+
from typing import List
89

910
from vulture import lines
1011
from vulture import noqa
@@ -139,13 +140,13 @@ def __init__(
139140
message="",
140141
confidence=DEFAULT_CONFIDENCE,
141142
):
142-
self.name = name
143-
self.typ = typ
144-
self.filename = filename
145-
self.first_lineno = first_lineno
146-
self.last_lineno = last_lineno
147-
self.message = message or f"unused {typ} '{name}'"
148-
self.confidence = confidence
143+
self.name: str = name
144+
self.typ: str = typ
145+
self.filename: Path = filename
146+
self.first_lineno: int = first_lineno
147+
self.last_lineno: int = last_lineno
148+
self.message: str = message or f"unused {typ} '{name}'"
149+
self.confidence: int = confidence
149150

150151
@property
151152
def size(self):
@@ -219,6 +220,7 @@ def get_list(typ):
219220
self.filename = Path()
220221
self.code = []
221222
self.exit_code = ExitCode.NoDeadCode
223+
self.noqa_lines = {}
222224

223225
def scan(self, code, filename=""):
224226
filename = Path(filename)
@@ -300,10 +302,13 @@ def exclude_path(path):
300302
except OSError:
301303
# Most imported modules don't have a whitelist.
302304
continue
305+
assert module_data is not None
303306
module_string = module_data.decode("utf-8")
304307
self.scan(module_string, filename=path)
305308

306-
def get_unused_code(self, min_confidence=0, sort_by_size=False):
309+
def get_unused_code(
310+
self, min_confidence=0, sort_by_size=False
311+
) -> List[Item]:
307312
"""
308313
Return ordered list of unused Item objects.
309314
"""

0 commit comments

Comments
 (0)