From 6be2c80b1869a6b143896e444ae83dec94ef4e9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Sun, 28 Nov 2021 00:02:38 +0100 Subject: [PATCH 1/2] Refactor ``_is_type_checking_import`` of ``unused-import`` --- ChangeLog | 3 +++ pylint/checkers/variables.py | 10 +++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index cffc35b91c..493381ec7f 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 analysed 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): From dd91dcaa4735bb6d92a59cc231a9d3f95d6ee603 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Sun, 28 Nov 2021 00:14:08 +0100 Subject: [PATCH 2/2] Update ChangeLog Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com> --- ChangeLog | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 493381ec7f..53f238b338 100644 --- a/ChangeLog +++ b/ChangeLog @@ -23,7 +23,7 @@ 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 analysed properly inside typing guards + was not analyzed properly inside typing guards. .. Insert your changelog randomly, it will reduce merge conflicts