Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type hints for the get_unused_code function and the fields of Item. #361

Merged
merged 23 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
cb5d69d
Add type hints for the `get_unused_code` function and `Item` fields.
johndoknjas Jul 22, 2024
f13f3be
Add type tests for `Item`.
johndoknjas Jul 22, 2024
b67c8e5
Add a test that runs mypy with some error codes disabled.
johndoknjas Jul 22, 2024
2906201
Replace mypy with pytype.
johndoknjas Oct 2, 2024
52ec404
Merge main and resolve conflicts.
johndoknjas Oct 2, 2024
1e1cdce
Satisfy black and ruff.
johndoknjas Oct 2, 2024
7a0498c
Add version number back in for pytype.
johndoknjas Oct 2, 2024
6ce5dbf
Remove version number for pytype.
johndoknjas Oct 3, 2024
6bc297f
Only run pytype for python < 3.13.
johndoknjas Oct 3, 2024
ac165f0
Run pytype directly in tox.ini.
johndoknjas Oct 4, 2024
ce6aed1
Initialize the `noqa_lines` field in the constructor.
johndoknjas Oct 4, 2024
89477ed
Try a different syntax for conditional statements in commands.
johndoknjas Oct 4, 2024
a8dcb91
Try a different syntax.
johndoknjas Oct 4, 2024
54ef88c
test
johndoknjas Oct 4, 2024
cfac816
See if adding `postargs` helps.
johndoknjas Oct 4, 2024
3344912
Test just py311
johndoknjas Oct 4, 2024
dcf4b87
remove py311
johndoknjas Oct 4, 2024
6e8735e
different syntax mentioned in docs
johndoknjas Oct 4, 2024
3074a3a
Revert attempted changes to call `pytype` directly.
johndoknjas Oct 4, 2024
9415be7
Apply PR feedback (includes defining `self.noqa_lines` in the constru…
johndoknjas Oct 4, 2024
4334ca6
Update changelog.
johndoknjas Oct 8, 2024
c27055d
Update the readme.
johndoknjas Oct 8, 2024
b7c0f89
For the pytype test, just check that the return code is 0.
johndoknjas Oct 8, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# next (unreleased)

* Add typehints for `get_unused_code` and the fields of the `Item` class (John Doknjas, #361).

# 2.13 (2024-10-02)

* Add support for Python 3.13 (Jendrik Seipp, #369).
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ Vulture will automatically look for a `pyproject.toml` in the current working di

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

It's also possible to use vulture programatically. For example:

``` python
import vulture

v = vulture.Vulture()
v.scavenge(['.'])
unused_code = v.get_unused_code() # returns a list of `Item` objects
```

## Integrations

You can use a [pre-commit](https://pre-commit.com/#install) hook to run
Expand Down
11 changes: 11 additions & 0 deletions tests/test_pytype.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import subprocess
import sys

import pytest
jendrikseipp marked this conversation as resolved.
Show resolved Hide resolved


@pytest.mark.skipif(
sys.version_info >= (3, 13), reason="needs Python < 3.13 for pytype"
)
def test_pytype():
jendrikseipp marked this conversation as resolved.
Show resolved Hide resolved
assert subprocess.run(["pytype", "vulture/core.py"]).returncode == 0
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ deps =
pint # Use latest version to catch API changes.
pytest
pytest-cov
pytype ; python_version < '3.13'
commands =
pytest {posargs}
# Install package as wheel in all envs (https://hynek.me/articles/turbo-charge-tox/).
Expand Down
21 changes: 13 additions & 8 deletions vulture/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import re
import string
import sys
from typing import List

from vulture import lines
from vulture import noqa
Expand Down Expand Up @@ -139,13 +140,13 @@ def __init__(
message="",
confidence=DEFAULT_CONFIDENCE,
):
self.name = name
self.typ = typ
self.filename = filename
self.first_lineno = first_lineno
self.last_lineno = last_lineno
self.message = message or f"unused {typ} '{name}'"
self.confidence = confidence
self.name: str = name
jendrikseipp marked this conversation as resolved.
Show resolved Hide resolved
self.typ: str = typ
self.filename: Path = filename
self.first_lineno: int = first_lineno
self.last_lineno: int = last_lineno
self.message: str = message or f"unused {typ} '{name}'"
self.confidence: int = confidence

@property
def size(self):
Expand Down Expand Up @@ -219,6 +220,7 @@ def get_list(typ):
self.filename = Path()
self.code = []
self.exit_code = ExitCode.NoDeadCode
self.noqa_lines = {}

def scan(self, code, filename=""):
filename = Path(filename)
Expand Down Expand Up @@ -300,10 +302,13 @@ def exclude_path(path):
except OSError:
# Most imported modules don't have a whitelist.
continue
assert module_data is not None
module_string = module_data.decode("utf-8")
self.scan(module_string, filename=path)

def get_unused_code(self, min_confidence=0, sort_by_size=False):
def get_unused_code(
self, min_confidence=0, sort_by_size=False
) -> List[Item]:
"""
Return ordered list of unused Item objects.
"""
Expand Down
Loading