From 16fe498b68c170b317eaea94bf85efd568a1dd0a Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 12 Apr 2023 17:34:34 +0200 Subject: Fix `unused-import` to check`dummy-variables-rgx` (#8566) (#8568) Resolve #8500 Co-authored-by: Pierre Sassoulas (cherry picked from commit 0cd41b1fb15e31eb311b61f93bc27f7cacc2f7ac) Co-authored-by: RSTdefg <34202999+RSTdefg@users.noreply.github.com> --- doc/whatsnew/fragments/8500.false_positive | 3 +++ pylint/checkers/variables.py | 15 +++++++++++---- tests/checkers/unittest_typecheck.py | 2 +- tests/functional/i/import_dummy.py | 5 +++++ 4 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 doc/whatsnew/fragments/8500.false_positive create mode 100644 tests/functional/i/import_dummy.py diff --git a/doc/whatsnew/fragments/8500.false_positive b/doc/whatsnew/fragments/8500.false_positive new file mode 100644 index 000000000..ced61766a --- /dev/null +++ b/doc/whatsnew/fragments/8500.false_positive @@ -0,0 +1,3 @@ +Fixed `unused-import` so that it observes the `dummy-variables-rgx` option. + +Closes #8500 diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 79d9ded08..8cd1ed83c 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -3062,6 +3062,13 @@ class VariablesChecker(BaseChecker): 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 ): @@ -3072,12 +3079,11 @@ class VariablesChecker(BaseChecker): # 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: @@ -3095,8 +3101,9 @@ class VariablesChecker(BaseChecker): # __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 == "*": diff --git a/tests/checkers/unittest_typecheck.py b/tests/checkers/unittest_typecheck.py index afcc2dc2e..dba69eaf3 100644 --- a/tests/checkers/unittest_typecheck.py +++ b/tests/checkers/unittest_typecheck.py @@ -10,7 +10,7 @@ from pylint.interfaces import INFERENCE, UNDEFINED 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: diff --git a/tests/functional/i/import_dummy.py b/tests/functional/i/import_dummy.py new file mode 100644 index 000000000..eecfa0d8a --- /dev/null +++ b/tests/functional/i/import_dummy.py @@ -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 -- cgit v1.2.1