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

Decorator name regression #284

Merged
merged 8 commits into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 2.9 (unreleased)
* Use exit code 3 when dead code is found (whosayn, #319).
* Simplify decorator names that are too hard to parse to "@" (Llandy3d and Jendrik Seipp, #284).

# 2.8 (2023-08-10)

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ starting with `foo` and the names `bar` and `baz`. Additionally, the
`--ignore-decorators` option can be used to ignore functions decorated
with the given decorator. This is helpful for example in Flask projects,
where you can use `--ignore-decorators "@app.route"` to ignore all
functions with the `@app.route` decorator.
functions with the `@app.route` decorator. Note that Vulture simplifies
decorators it cannot parse: `@foo.bar(x, y)` becomes "@foo.bar" and
`@foo.bar(x, y).baz` becomes "@" internally.

We recommend using whitelists instead of `--ignore-names` or
`--ignore-decorators` whenever possible, since whitelists are
Expand Down
38 changes: 38 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ast
import os
import pathlib
import sys

import pytest

Expand Down Expand Up @@ -119,3 +120,40 @@ class Foo:
pass
"""
check_decorator_names(code, ["@foo", "@bar.yz"])


def test_get_decorator_name_end_function_call():
code = """\
@foo.bar(x, y, z)
def bar():
pass
"""
check_decorator_names(code, ["@foo.bar"])


@pytest.mark.skipif(
sys.version_info < (3, 9), reason="requires Python 3.9 or higher"
)
@pytest.mark.parametrize(
"decorated",
[
("def foo():"),
("async def foo():"),
("class Foo:"),
],
)
def test_get_decorator_name_multiple_callables(decorated):
decorated = f"{decorated}\n pass"
code = f"""\
@foo
@bar.prop
@z.func("hi").bar().k.foo
@k("hello").doo("world").x
@k.hello("world")
@foo[2]
{decorated}
"""
check_decorator_names(
code,
["@foo", "@bar.prop", "@", "@", "@k.hello", "@"],
)
13 changes: 8 additions & 5 deletions vulture/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,14 @@ def format_path(path):
def get_decorator_name(decorator):
if isinstance(decorator, ast.Call):
decorator = decorator.func
parts = []
while isinstance(decorator, ast.Attribute):
parts.append(decorator.attr)
decorator = decorator.value
parts.append(decorator.id)
try:
parts = []
while isinstance(decorator, ast.Attribute):
parts.append(decorator.attr)
decorator = decorator.value
parts.append(decorator.id)
except AttributeError:
parts = []
return "@" + ".".join(reversed(parts))


Expand Down