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 3 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
21 changes: 21 additions & 0 deletions tests/test_mypy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import mypy.api


def test_mypy():
result = mypy.api.run(
[
"--check-untyped-defs",
"--ignore-missing-imports",
"--disable-error-code",
"truthy-function",
"--disable-error-code",
"no-redef",
"--disable-error-code",
"attr-defined",
"--disable-error-code",
"misc",
"./vulture",
]
)
assert result[0].startswith("Success: no issues found in ")
assert result[1:] == ("", 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==7.4.2
pytest-cov==4.0.0
mypy==1.11.0
commands =
pytest {posargs}
# Install package as wheel in all envs (https://hynek.me/articles/turbo-charge-tox/).
Expand Down
20 changes: 12 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 @@ -300,10 +301,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