Skip to content

Commit bbbdb9d

Browse files
authored
Fix unused-variable false positive with __all__ or __future__.annotations (#10643)
Closes #10019
1 parent b970208 commit bbbdb9d

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Exclude ``__all__`` and ``__future__.annotations`` from ``unused-variable``.
2+
3+
Closes #10019

pylint/checkers/variables.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3285,6 +3285,14 @@ def _check_globals(self, not_consumed: Consumption) -> None:
32853285
continue
32863286
if self._is_exception_binding_used_in_handler(node, name):
32873287
continue
3288+
if isinstance(node, nodes.AssignName) and node.name == "__all__":
3289+
continue
3290+
if (
3291+
isinstance(node, nodes.ImportFrom)
3292+
and name == "annotations"
3293+
and node.modname == "__future__"
3294+
):
3295+
continue
32883296
self.add_message("unused-variable", args=(name,), node=node)
32893297

32903298
# pylint: disable = too-many-branches
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""Test `unused-variable` is not emitted for either case of `__future__.annotations` or `__all__`"""
2+
3+
4+
from __future__ import annotations
5+
6+
7+
__all__ = [ "apple" ]
8+
9+
10+
def apple():
11+
"""A public function"""
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[variables]
2+
allow-global-unused-variables=no

0 commit comments

Comments
 (0)