diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2019-05-23 08:53:09 +0200 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2019-05-23 08:54:25 +0200 |
commit | 3bfec9f63b53abd11131c164c4fa941ee1743286 (patch) | |
tree | 4c6b3135caf1c7b78fd21ec9d19fa67ea49b08eb /pylint | |
parent | b657df6644740e77fa167d4f95c76c4c007d2a49 (diff) | |
download | pylint-git-3bfec9f63b53abd11131c164c4fa941ee1743286.tar.gz |
``unused-import`` emitted for the right import names in function scopes.
Close #2928
Diffstat (limited to 'pylint')
-rw-r--r-- | pylint/checkers/variables.py | 13 | ||||
-rw-r--r-- | pylint/test/functional/unused_variable.py | 5 | ||||
-rw-r--r-- | pylint/test/functional/unused_variable.txt | 1 |
3 files changed, 16 insertions, 3 deletions
diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index c91880c24..921b8e749 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -841,7 +841,6 @@ class VariablesChecker(BaseChecker): 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. @@ -1034,10 +1033,18 @@ class VariablesChecker(BaseChecker): 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" diff --git a/pylint/test/functional/unused_variable.py b/pylint/test/functional/unused_variable.py index 4e6d58fe4..e475a9038 100644 --- a/pylint/test/functional/unused_variable.py +++ b/pylint/test/functional/unused_variable.py @@ -53,3 +53,8 @@ def locals_does_not_account_for_subscopes(): 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 diff --git a/pylint/test/functional/unused_variable.txt b/pylint/test/functional/unused_variable.txt index 231528be3..e9f7ef022 100644 --- a/pylint/test/functional/unused_variable.txt +++ b/pylint/test/functional/unused_variable.txt @@ -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 |