diff --git a/ChangeLog b/ChangeLog index cffc35b91c..53f238b338 100644 --- a/ChangeLog +++ b/ChangeLog @@ -22,6 +22,9 @@ Release date: TBA .. Put bug fixes that should not wait for a new minor version here +* Fixed a false positive for ``unused-import`` where everything + was not analyzed properly inside typing guards. + .. Insert your changelog randomly, it will reduce merge conflicts (Ie. not necessarily at the end) diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index fa3c0867e0..0d327aea8e 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -362,11 +362,11 @@ def _assigned_locally(name_node): def _is_type_checking_import(node: Union[nodes.Import, nodes.ImportFrom]) -> bool: """Check if an import node is guarded by a TYPE_CHECKS guard""" - for ancestor in node.node_ancestors(): - if isinstance(ancestor, nodes.If): - if ancestor.test.as_string() in TYPING_TYPE_CHECKS_GUARDS: - return True - return False + return any( + isinstance(ancestor, nodes.If) + and ancestor.test.as_string() in TYPING_TYPE_CHECKS_GUARDS + for ancestor in node.node_ancestors() + ) def _has_locals_call_after_node(stmt, scope):