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

Fix unused-import to checkdummy-variables-rgx #8566

Merged
merged 5 commits into from
Apr 12, 2023
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
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/8500.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed `unused-import` so that it observes the `dummy-variables-rgx` option.

Closes #8500
15 changes: 11 additions & 4 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3055,6 +3055,13 @@ def _check_imports(self, not_consumed: dict[str, list[nodes.NodeNG]]) -> None:
imported_name in self._type_annotation_names
or as_name in self._type_annotation_names
)

is_dummy_import = (
as_name
and self.linter.config.dummy_variables_rgx
and self.linter.config.dummy_variables_rgx.match(as_name)
)

if isinstance(stmt, nodes.Import) or (
isinstance(stmt, nodes.ImportFrom) and not stmt.modname
):
Expand All @@ -3065,12 +3072,11 @@ def _check_imports(self, not_consumed: dict[str, list[nodes.NodeNG]]) -> None:
# because they can be imported for exporting.
continue

if is_type_annotation_import:
if is_type_annotation_import or is_dummy_import:
# Most likely a typing import if it wasn't used so far.
# Also filter dummy variables.
continue

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

if is_type_annotation_import:
if is_type_annotation_import or is_dummy_import:
# Most likely a typing import if it wasn't used so far.
# Also filter dummy variables.
continue

if imported_name == "*":
Expand Down
2 changes: 1 addition & 1 deletion tests/checkers/unittest_typecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pylint.testutils import CheckerTestCase, MessageTest, set_config

try:
from coverage import tracer as _ # pylint: disable=unused-import
from coverage import tracer as _

C_EXTENTIONS_AVAILABLE = True
except ImportError:
Expand Down
5 changes: 5 additions & 0 deletions tests/functional/i/import_dummy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Testing importing module as dummy variable."""
import gettext as _
import sys as __
import typing as ___dummy
from base64 import b64encode as ____dummy