Skip to content

Commit 0cd41b1

Browse files
Fix unused-import to checkdummy-variables-rgx (#8566)
Resolve #8500 Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent 1a1452a commit 0cd41b1

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
@@ -3055,6 +3055,13 @@ def _check_imports(self, not_consumed: dict[str, list[nodes.NodeNG]]) -> None:
30553055
imported_name in self._type_annotation_names
30563056
or as_name in self._type_annotation_names
30573057
)
3058+
3059+
is_dummy_import = (
3060+
as_name
3061+
and self.linter.config.dummy_variables_rgx
3062+
and self.linter.config.dummy_variables_rgx.match(as_name)
3063+
)
3064+
30583065
if isinstance(stmt, nodes.Import) or (
30593066
isinstance(stmt, nodes.ImportFrom) and not stmt.modname
30603067
):
@@ -3065,12 +3072,11 @@ def _check_imports(self, not_consumed: dict[str, list[nodes.NodeNG]]) -> None:
30653072
# because they can be imported for exporting.
30663073
continue
30673074

3068-
if is_type_annotation_import:
3075+
if is_type_annotation_import or is_dummy_import:
30693076
# Most likely a typing import if it wasn't used so far.
3077+
# Also filter dummy variables.
30703078
continue
30713079

3072-
if as_name == "_":
3073-
continue
30743080
if as_name is None:
30753081
msg = f"import {imported_name}"
30763082
else:
@@ -3088,8 +3094,9 @@ def _check_imports(self, not_consumed: dict[str, list[nodes.NodeNG]]) -> None:
30883094
# __future__ import in another module.
30893095
continue
30903096

3091-
if is_type_annotation_import:
3097+
if is_type_annotation_import or is_dummy_import:
30923098
# Most likely a typing import if it wasn't used so far.
3099+
# Also filter dummy variables.
30933100
continue
30943101

30953102
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)