Skip to content

Commit 16fe498

Browse files
Fix unused-import to checkdummy-variables-rgx (#8566) (#8568)
Resolve #8500 Co-authored-by: Pierre Sassoulas <[email protected]> (cherry picked from commit 0cd41b1) Co-authored-by: RSTdefg <[email protected]>
1 parent 61dae1e commit 16fe498

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed `unused-import` so that it observes the `dummy-variables-rgx` option.
2+
3+
Closes #8500

pylint/checkers/variables.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -3062,6 +3062,13 @@ def _check_imports(self, not_consumed: dict[str, list[nodes.NodeNG]]) -> None:
30623062
imported_name in self._type_annotation_names
30633063
or as_name in self._type_annotation_names
30643064
)
3065+
3066+
is_dummy_import = (
3067+
as_name
3068+
and self.linter.config.dummy_variables_rgx
3069+
and self.linter.config.dummy_variables_rgx.match(as_name)
3070+
)
3071+
30653072
if isinstance(stmt, nodes.Import) or (
30663073
isinstance(stmt, nodes.ImportFrom) and not stmt.modname
30673074
):
@@ -3072,12 +3079,11 @@ def _check_imports(self, not_consumed: dict[str, list[nodes.NodeNG]]) -> None:
30723079
# because they can be imported for exporting.
30733080
continue
30743081

3075-
if is_type_annotation_import:
3082+
if is_type_annotation_import or is_dummy_import:
30763083
# Most likely a typing import if it wasn't used so far.
3084+
# Also filter dummy variables.
30773085
continue
30783086

3079-
if as_name == "_":
3080-
continue
30813087
if as_name is None:
30823088
msg = f"import {imported_name}"
30833089
else:
@@ -3095,8 +3101,9 @@ def _check_imports(self, not_consumed: dict[str, list[nodes.NodeNG]]) -> None:
30953101
# __future__ import in another module.
30963102
continue
30973103

3098-
if is_type_annotation_import:
3104+
if is_type_annotation_import or is_dummy_import:
30993105
# Most likely a typing import if it wasn't used so far.
3106+
# Also filter dummy variables.
31003107
continue
31013108

31023109
if imported_name == "*":

tests/checkers/unittest_typecheck.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from pylint.testutils import CheckerTestCase, MessageTest, set_config
1111

1212
try:
13-
from coverage import tracer as _ # pylint: disable=unused-import
13+
from coverage import tracer as _
1414

1515
C_EXTENTIONS_AVAILABLE = True
1616
except ImportError:

tests/functional/i/import_dummy.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""Testing importing module as dummy variable."""
2+
import gettext as _
3+
import sys as __
4+
import typing as ___dummy
5+
from base64 import b64encode as ____dummy

0 commit comments

Comments
 (0)