diff options
Diffstat (limited to 'pylint/checkers/variables.py')
-rw-r--r-- | pylint/checkers/variables.py | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index d77cba6fd..b2e38e113 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -60,6 +60,7 @@ import itertools import os import re from functools import lru_cache +from typing import DefaultDict, List, Tuple import astroid from astroid import nodes @@ -449,7 +450,7 @@ MSGS = { "Used when a function or method argument is not used.", ), "W0614": ( - "Unused import %s from wildcard import", + "Unused import(s) %s from wildcard import of %s", "unused-wildcard-import", "Used when an imported module or variable is not used from a " "`'from X import *'` style import.", @@ -2079,6 +2080,9 @@ class VariablesChecker(BaseChecker): def _check_imports(self, not_consumed): local_names = _fix_dot_imports(not_consumed) checked = set() + unused_wildcard_imports: DefaultDict[ + Tuple[str, nodes.ImportFrom], List[str] + ] = collections.defaultdict(list) for name, stmt in local_names: for imports in stmt.names: real_name = imported_name = imports[0] @@ -2133,7 +2137,7 @@ class VariablesChecker(BaseChecker): continue if imported_name == "*": - self.add_message("unused-wildcard-import", args=name, node=stmt) + unused_wildcard_imports[(stmt.modname, stmt)].append(name) else: if as_name is None: msg = f"{imported_name} imported from {stmt.modname}" @@ -2141,6 +2145,18 @@ class VariablesChecker(BaseChecker): msg = f"{imported_name} imported from {stmt.modname} as {as_name}" if not _is_type_checking_import(stmt): self.add_message("unused-import", args=msg, node=stmt) + + # Construct string for unused-wildcard-import message + for module, unused_list in unused_wildcard_imports.items(): + if len(unused_list) == 1: + arg_string = unused_list[0] + else: + arg_string = ( + f"{', '.join(i for i in unused_list[:-1])} and {unused_list[-1]}" + ) + self.add_message( + "unused-wildcard-import", args=(arg_string, module[0]), node=module[1] + ) del self._to_consume def _check_metaclasses(self, node): |