Skip to content

Commit

Permalink
Allow import aliases to exempt import-error when used in type ann…
Browse files Browse the repository at this point in the history
…otations.

Close #3178
  • Loading branch information
PCManticore committed Nov 14, 2019
1 parent a8f8b02 commit 333e4c1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Expand Up @@ -16,6 +16,10 @@ Release date: TBA

Close #3235

* Allow import aliases to exempt ``import-error`` when used in type annotations.

Close #3178

* ``Ellipsis` is exempted from ``multiple-statements`` for function overloads.

Close #3224
Expand Down
8 changes: 6 additions & 2 deletions pylint/checkers/variables.py
Expand Up @@ -1879,6 +1879,10 @@ def _check_imports(self, not_consumed):
continue
checked.add(real_name)

is_type_annotation_import = (
imported_name in self._type_annotation_names
or as_name in self._type_annotation_names
)
if isinstance(stmt, astroid.Import) or (
isinstance(stmt, astroid.ImportFrom) and not stmt.modname
):
Expand All @@ -1889,7 +1893,7 @@ def _check_imports(self, not_consumed):
# because they can be imported for exporting.
continue

if imported_name in self._type_annotation_names:
if is_type_annotation_import:
# Most likely a typing import if it wasn't used so far.
continue

Expand All @@ -1912,7 +1916,7 @@ def _check_imports(self, not_consumed):
# __future__ import in another module.
continue

if imported_name in self._type_annotation_names:
if is_type_annotation_import:
# Most likely a typing import if it wasn't used so far.
continue

Expand Down
9 changes: 8 additions & 1 deletion tests/functional/u/unused_typing_imports.py
Expand Up @@ -7,6 +7,7 @@

import re
import typing
from collections import Counter as CollectionCounter
from collections import defaultdict
from datetime import datetime
from typing import (
Expand Down Expand Up @@ -74,5 +75,11 @@ def magic(alpha, beta, gamma):


def unused_assignment_import():
foo_or_bar = defaultdict(int) # type: defaultdict
foo_or_bar = 42 # type: defaultdict
return foo_or_bar


def unused_reassigned_import(counter):
# type: (CollectionCounter) -> int
print(counter)
return 42

0 comments on commit 333e4c1

Please sign in to comment.