Skip to content

Commit

Permalink
unused-import emitted for the right import names in function scopes.
Browse files Browse the repository at this point in the history
Close #2928
  • Loading branch information
PCManticore committed May 23, 2019
1 parent b657df6 commit 3bfec9f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ What's New in Pylint 2.4.0?

Release date: TBA

* ``unused-import`` emitted for the right import names in function scopes.

Close #2928

* ``assignment-from-no-return`` not triggered for async methods.

Close #2902
Expand Down
13 changes: 10 additions & 3 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,6 @@ def _check_imports(self, not_consumed):
if not self._is_type_checking_import(stmt):
self.add_message("unused-import", args=msg, node=stmt)
elif isinstance(stmt, astroid.ImportFrom) and stmt.modname != FUTURE:

if SPECIAL_OBJ.search(imported_name):
# Filter special objects (__doc__, __all__) etc.,
# because they can be imported for exporting.
Expand Down Expand Up @@ -1034,10 +1033,18 @@ def _check_is_unused(self, name, node, stmt, global_names, nonlocal_names):
if name in nonlocal_names:
return

qname = asname = None
if isinstance(stmt, (astroid.Import, astroid.ImportFrom)):
# Need the complete name, which we don't have in .locals.
qname, asname = stmt.names[0]
name = asname or qname
if len(stmt.names) > 1:
import_names = next(
(names for names in stmt.names if name in names), None
)
else:
import_names = stmt.names[0]
if import_names:
qname, asname = import_names
name = asname or qname

if _has_locals_call_after_node(stmt, node.scope()):
message_name = "possibly-unused-variable"
Expand Down
5 changes: 5 additions & 0 deletions pylint/test/functional/unused_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@ def some_other_scope():
def unused_import_from():
from functools import wraps as abc # [unused-import]
from collections import namedtuple # [unused-import]


def unused_import_in_function(value):
from six import PY2, text_type # [unused-import]
return value.encode("utf-8") if PY2 else value
1 change: 1 addition & 0 deletions pylint/test/functional/unused_variable.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ unused-variable:41:locals_example_defined_after:Unused variable 'value'
unused-variable:46:locals_does_not_account_for_subscopes:Unused variable 'value'
unused-import:54:unused_import_from:Unused wraps imported from functools as abc
unused-import:55:unused_import_from:Unused namedtuple imported from collections
unused-import:59:unused_import_in_function:Unused text_type imported from six

0 comments on commit 3bfec9f

Please sign in to comment.