From 11c190894d09aa92fd135f551e10ebf656bd6f52 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 13 Nov 2019 08:34:04 +0100 Subject: [PATCH] Relax type import detection for names that do not come from the ``typing`` module Close #3191 --- ChangeLog | 4 ++++ pylint/checkers/variables.py | 6 +----- tests/functional/u/unused_typing_imports.py | 6 ++++++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4d2fb506fb..2a285bef64 100644 --- a/ChangeLog +++ b/ChangeLog @@ -89,6 +89,10 @@ Release date: TBA Close #3112 +* Relax type import detection for names that do not come from the ``typing`` module + + Close #3191 + What's New in Pylint 2.4.3? =========================== diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index f2d161ada9..3a13918a0d 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -1672,17 +1672,13 @@ def _has_homonym_in_upper_function_scope(self, node, index): def _store_type_annotation_node(self, type_annotation): """Given a type annotation, store all the name nodes it refers to""" - if ( - isinstance(type_annotation, astroid.Name) - and type_annotation.name in TYPING_NAMES - ): + if isinstance(type_annotation, astroid.Name): self._type_annotation_names.append(type_annotation.name) return if not isinstance(type_annotation, astroid.Subscript): return - # Check if it is namespaced by typing or not. if ( isinstance(type_annotation.value, astroid.Attribute) and isinstance(type_annotation.value.expr, astroid.Name) diff --git a/tests/functional/u/unused_typing_imports.py b/tests/functional/u/unused_typing_imports.py index 4c122a6220..c0a2f49863 100644 --- a/tests/functional/u/unused_typing_imports.py +++ b/tests/functional/u/unused_typing_imports.py @@ -7,6 +7,7 @@ import re import typing +from collections import defaultdict from datetime import datetime from typing import ( Any, @@ -70,3 +71,8 @@ def magic(alpha, beta, gamma): # type: (str, Optional[str], Optional[datetime]) -> Any """going strong""" return alpha, beta, gamma + + +def unused_assignment_import(): + foo_or_bar = defaultdict(int) # type: defaultdict + return foo_or_bar