Permalink
Browse files

Let Item inherit from object (#71)

  • Loading branch information...
RJ722 authored and jendrikseipp committed Jul 21, 2017
1 parent 5536374 commit 99ca09ff44cc5b75a6911bdae3416933e8d83c35
Showing with 258 additions and 242 deletions.
  1. +4 −0 tests/__init__.py
  2. +10 −9 tests/test_format_strings.py
  3. +85 −84 tests/test_imports.py
  4. +138 −137 tests/test_scavenging.py
  5. +21 −12 vulture/core.py
View
@@ -0,0 +1,4 @@
# TODO: Convert used_vars and used_attrs to sets and use dedicated tests.
def check(asserted, assertion):
names = set(getattr(item, 'name', item) for item in asserted)
assert names == set(assertion)
@@ -1,7 +1,8 @@
import pytest
from vulture import Vulture
from . import check
@pytest.fixture
def v():
@@ -10,15 +11,15 @@ def v():
def test_old_format_string(v):
v.scan("a = 1\n'%(a)s, %(b)d' % locals()")
assert v.defined_funcs == []
assert v.defined_vars == ['a']
assert v.used_vars == ['a', 'b', 'locals']
assert v.unused_vars == []
check(v.defined_funcs, [])
check(v.defined_vars, ['a'])
check(v.used_vars, ['a', 'b', 'locals'])
check(v.unused_vars, [])
def test_new_format_string(v):
v.scan("a = 1\n'{a}, {b}'.format(**locals())")
assert v.defined_funcs == []
assert v.defined_vars == ['a']
assert v.used_vars == ['a', 'b', 'locals']
assert v.unused_vars == []
check(v.defined_funcs, [])
check(v.defined_vars, ['a'])
check(v.used_vars, ['a', 'b', 'locals'])
check(v.unused_vars, [])
View
@@ -1,7 +1,8 @@
import pytest
from vulture import Vulture
from . import check
@pytest.fixture
def v():
@@ -13,14 +14,14 @@ def test_import_star(v):
from a import *
from a.b import *
""")
assert v.defined_imports == ['*', '*']
assert v.unused_imports == []
check(v.defined_imports, ['*', '*'])
check(v.unused_imports, [])
def test_import_from_future(v):
v.scan("""from __future__ import division""")
assert v.defined_imports == []
assert v.unused_imports == []
check(v.defined_imports, [])
check(v.unused_imports, [])
def test_attribute_access(v):
@@ -36,20 +37,20 @@ class Foo:
import bar
bar.Foo
""")
assert v.defined_imports == ['Foo', 'bar']
assert v.unused_imports == []
check(v.defined_imports, ['Foo', 'bar'])
check(v.unused_imports, [])
def test_nested_import(v):
v.scan("""\
import os.path
os.path.expanduser("~")
""")
assert v.defined_imports == ['os']
assert v.used_vars == ['os']
assert v.unused_funcs == []
assert v.unused_imports == []
assert v.unused_vars == []
check(v.defined_imports, ['os'])
check(v.used_vars, ['os'])
check(v.unused_funcs, [])
check(v.unused_imports, [])
check(v.unused_vars, [])
definitions = """\
@@ -91,103 +92,103 @@ def C():
def test_definitions(v):
v.scan(definitions)
assert v.defined_classes == ['A', 'B']
assert v.defined_funcs == ['C']
assert v.defined_imports == []
assert v.defined_vars == ['D']
assert v.used_vars == []
assert v.unused_classes == ['A', 'B']
assert v.unused_funcs == ['C']
assert v.unused_imports == []
assert v.unused_vars == ['D']
check(v.defined_classes, ['A', 'B'])
check(v.defined_funcs, ['C'])
check(v.defined_imports, [])
check(v.defined_vars, ['D'])
check(v.used_vars, [])
check(v.unused_classes, ['A', 'B'])
check(v.unused_funcs, ['C'])
check(v.unused_imports, [])
check(v.unused_vars, ['D'])
def test_use_original(v):
v.scan(definitions + uses)
assert v.defined_classes == ['A', 'B']
assert v.defined_funcs == ['C']
assert v.defined_imports == []
assert v.defined_vars == ['D']
assert v.used_vars == ['A', 'B', 'C', 'D']
assert v.unused_funcs == []
assert v.unused_classes == []
assert v.unused_imports == []
assert v.unused_vars == []
check(v.defined_classes, ['A', 'B'])
check(v.defined_funcs, ['C'])
check(v.defined_imports, [])
check(v.defined_vars, ['D'])
check(v.used_vars, ['A', 'B', 'C', 'D'])
check(v.unused_funcs, [])
check(v.unused_classes, [])
check(v.unused_imports, [])
check(v.unused_vars, [])
def test_import_original(v):
v.scan(definitions + imports)
assert v.defined_classes == ['A', 'B']
assert v.defined_funcs == ['C']
assert v.defined_imports == ['A', 'B', 'C', 'D']
assert v.defined_vars == ['D']
assert v.used_vars == []
assert v.unused_classes == ['A', 'B']
assert v.unused_funcs == ['C']
assert v.unused_imports == ['A', 'B', 'C', 'D']
assert v.unused_vars == ['D']
check(v.defined_classes, ['A', 'B'])
check(v.defined_funcs, ['C'])
check(v.defined_imports, ['A', 'B', 'C', 'D'])
check(v.defined_vars, ['D'])
check(v.used_vars, [])
check(v.unused_classes, ['A', 'B'])
check(v.unused_funcs, ['C'])
check(v.unused_imports, ['A', 'B', 'C', 'D'])
check(v.unused_vars, ['D'])
def test_import_original_use_original(v):
v.scan(definitions + imports + uses)
assert v.defined_classes == ['A', 'B']
assert v.defined_funcs == ['C']
assert v.defined_imports == ['A', 'B', 'C', 'D']
assert v.defined_vars == ['D']
assert v.used_vars == ['A', 'B', 'C', 'D']
assert v.unused_classes == []
assert v.unused_funcs == []
assert v.unused_imports == []
assert v.unused_vars == []
check(v.defined_classes, ['A', 'B'])
check(v.defined_funcs, ['C'])
check(v.defined_imports, ['A', 'B', 'C', 'D'])
check(v.defined_vars, ['D'])
check(v.used_vars, ['A', 'B', 'C', 'D'])
check(v.unused_classes, [])
check(v.unused_funcs, [])
check(v.unused_imports, [])
check(v.unused_vars, [])
def test_import_original_use_alias(v):
v.scan(definitions + imports + aliased_uses)
assert v.defined_classes == ['A', 'B']
assert v.defined_funcs == ['C']
assert v.defined_imports == ['A', 'B', 'C', 'D']
assert v.defined_vars == ['D']
assert v.used_vars == ['AliasA', 'AliasB', 'AliasC', 'AliasD']
assert v.unused_classes == ['A', 'B']
assert v.unused_funcs == ['C']
assert v.unused_imports == ['A', 'B', 'C', 'D']
assert v.unused_vars == ['D']
check(v.defined_classes, ['A', 'B'])
check(v.defined_funcs, ['C'])
check(v.defined_imports, ['A', 'B', 'C', 'D'])
check(v.defined_vars, ['D'])
check(v.used_vars, ['AliasA', 'AliasB', 'AliasC', 'AliasD'])
check(v.unused_classes, ['A', 'B'])
check(v.unused_funcs, ['C'])
check(v.unused_imports, ['A', 'B', 'C', 'D'])
check(v.unused_vars, ['D'])
def test_import_alias(v):
v.scan(definitions + aliased_imports)
assert v.defined_classes == ['A', 'B']
assert v.defined_funcs == ['C']
assert v.defined_imports == ['AliasA', 'AliasB', 'AliasC', 'AliasD']
assert v.defined_vars == ['D']
assert v.used_vars == []
assert v.unused_classes == []
assert v.unused_funcs == []
assert v.unused_imports == ['AliasA', 'AliasB', 'AliasC', 'AliasD']
assert v.unused_vars == []
check(v.defined_classes, ['A', 'B'])
check(v.defined_funcs, ['C'])
check(v.defined_imports, ['AliasA', 'AliasB', 'AliasC', 'AliasD'])
check(v.defined_vars, ['D'])
check(v.used_vars, [])
check(v.unused_classes, [])
check(v.unused_funcs, [])
check(v.unused_imports, ['AliasA', 'AliasB', 'AliasC', 'AliasD'])
check(v.unused_vars, [])
def test_import_alias_use_original(v):
v.scan(definitions + aliased_imports + uses)
assert v.defined_classes == ['A', 'B']
assert v.defined_funcs == ['C']
assert v.defined_imports == ['AliasA', 'AliasB', 'AliasC', 'AliasD']
assert v.defined_vars == ['D']
assert v.used_vars == ['A', 'B', 'C', 'D']
assert v.unused_classes == []
assert v.unused_funcs == []
assert v.unused_imports == ['AliasA', 'AliasB', 'AliasC', 'AliasD']
assert v.unused_vars == []
check(v.defined_classes, ['A', 'B'])
check(v.defined_funcs, ['C'])
check(v.defined_imports, ['AliasA', 'AliasB', 'AliasC', 'AliasD'])
check(v.defined_vars, ['D'])
check(v.used_vars, ['A', 'B', 'C', 'D'])
check(v.unused_classes, [])
check(v.unused_funcs, [])
check(v.unused_imports, ['AliasA', 'AliasB', 'AliasC', 'AliasD'])
check(v.unused_vars, [])
def test_import_alias_use_alias(v):
v.scan(definitions + aliased_imports + aliased_uses)
assert v.defined_classes == ['A', 'B']
assert v.defined_funcs == ['C']
assert v.defined_imports == ['AliasA', 'AliasB', 'AliasC', 'AliasD']
assert v.defined_vars == ['D']
assert v.used_vars == ['AliasA', 'AliasB', 'AliasC', 'AliasD']
assert v.unused_classes == []
assert v.unused_funcs == []
assert v.unused_imports == []
assert v.unused_vars == []
check(v.defined_classes, ['A', 'B'])
check(v.defined_funcs, ['C'])
check(v.defined_imports, ['AliasA', 'AliasB', 'AliasC', 'AliasD'])
check(v.defined_vars, ['D'])
check(v.used_vars, ['AliasA', 'AliasB', 'AliasC', 'AliasD'])
check(v.unused_classes, [])
check(v.unused_funcs, [])
check(v.unused_imports, [])
check(v.unused_vars, [])
Oops, something went wrong.

0 comments on commit 99ca09f

Please sign in to comment.