Skip to content

Commit

Permalink
Make unused-import check all ancestors for typing guards
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielNoord committed Nov 15, 2021
1 parent 0089cf3 commit c9a8629
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
15 changes: 8 additions & 7 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
import re
import sys
from functools import lru_cache
from typing import DefaultDict, List, Optional, Set, Tuple
from typing import DefaultDict, List, Optional, Set, Tuple, Union

import astroid
from astroid import nodes
Expand Down Expand Up @@ -358,12 +358,13 @@ def _assigned_locally(name_node):
return any(a.name == name_node.name for a in assign_stmts)


def _is_type_checking_import(node):
parent = node.parent
if not isinstance(parent, nodes.If):
return False
test = parent.test
return test.as_string() in TYPING_TYPE_CHECKS_GUARDS
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


def _has_locals_call_after_node(stmt, scope):
Expand Down
4 changes: 4 additions & 0 deletions tests/functional/u/unused/unused_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,7 @@ def dummy(self, truc):
def blop(self):
"""yo"""
print(self, 'blip')

if TYPE_CHECKING:
if sys.version_info >= (3, 6, 2):
from typing import NoReturn

0 comments on commit c9a8629

Please sign in to comment.