Skip to content

Commit

Permalink
Add type tests for Item.
Browse files Browse the repository at this point in the history
  • Loading branch information
johndoknjas committed Jul 22, 2024
1 parent cb5d69d commit f13f3be
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tests/test_item.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from pathlib import Path

from . import v
from vulture.core import Item

assert v # Silence pyflakes

Expand Down Expand Up @@ -99,3 +102,29 @@ def test_item_variable(v):
assert var.name == "v"
assert var.first_lineno == 1
assert var.last_lineno == 1


def test_item_types(v):
v.scan(
"""\
import os
message = "foobar"
class Foo:
def bar():
pass
"""
)
for item in (unused_code := v.get_unused_code()):
assert isinstance(item, Item)
assert isinstance(item.filename, Path)
assert all(
isinstance(field, str)
for field in (item.name, item.typ, item.message)
)
assert all(
isinstance(field, int)
for field in (item.first_lineno, item.last_lineno, item.confidence)
)
assert isinstance(unused_code, list)

1 comment on commit f13f3be

@jendrikseipp
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test has the drawback that now we need to maintain the types in two locations: the code and the test. What I meant was to add a call to a type checker that complains if and only if any type annotations are wrong. For the choice of type checker, I'd try them in the following order:

  1. mypy with the --check-untyped-defs option
  2. pyright
  3. pytype

Please sign in to comment.