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 support for del keyword for overridden functions #279

Merged
merged 3 commits into from
May 19, 2022
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
Expand Up @@ -3,6 +3,7 @@
* Print absolute filepaths as relative again (as with 2.1 and before) if they
are below the current directory (The-Compiler, #246).
* Run tests and add PyPI trove for Python 3.10 (chayim, #266).
* Allow using the `del` keyword to mark unused variables (sshishov, #279).

# 2.3 (2021-01-16)

Expand Down
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,20 @@ check that all whitelisted code actually still exists in your project.
**Marking unused variables**

There are situations where you can't just remove unused variables, e.g.,
in tuple assignments or function signatures. Vulture will ignore these
variables if they start with an underscore (e.g., `_x, y = get_pos()` or
`def my_method(self, widget, **_kwargs)`).
in function signatures. The recommended solution is to use the `del`
keyword as described in the
[PyLint manual](http://pylint-messages.wikidot.com/messages:w0613) and on
[StackOverflow](https://stackoverflow.com/a/14836005):

```python
def foo(x, y):
del y
return x + 3
```

Vulture will also ignore all variables that start with an underscore, so
you can use `_x, y = get_pos()` to mark unused tuple assignments or
function arguments, e.g., `def foo(x, _y)`.

**Minimum confidence**

Expand Down
18 changes: 18 additions & 0 deletions tests/test_scavenging.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,3 +741,21 @@ def bad():
assert not v.found_dead_code_or_error
else:
assert v.found_dead_code_or_error


def test_unused_args_with_del(v):
v.scan(
"""\
def foo(a, b, c, d=3):
del c, d
return a + b

foo(1, 2)
"""
)

check(v.defined_funcs, ["foo"])
check(v.defined_vars, ["a", "b", "c", "d"])
check(v.used_names, ["foo", "a", "b", "c", "d"])
check(v.unused_vars, [])
check(v.unused_funcs, [])
2 changes: 1 addition & 1 deletion vulture/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ def visit_ImportFrom(self, node):

def visit_Name(self, node):
if (
isinstance(node.ctx, ast.Load)
isinstance(node.ctx, (ast.Load, ast.Del))
and node.id not in IGNORED_VARIABLE_NAMES
):
self.used_names.add(node.id)
Expand Down