Skip to content

Commit

Permalink
Allow using del keyword to mark unused variables (#279)
Browse files Browse the repository at this point in the history
Co-authored-by: Sergei Shishov <[email protected]>
Co-authored-by: Jendrik Seipp <[email protected]>
  • Loading branch information
3 people authored May 19, 2022
1 parent bbef855 commit ff9fb4c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
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

0 comments on commit ff9fb4c

Please sign in to comment.