summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichal Nowikowski <godfryd@gmail.com>2014-08-06 06:54:10 +0200
committerMichal Nowikowski <godfryd@gmail.com>2014-08-06 06:54:10 +0200
commit1be680429bd86e93fe47c4398b82dccded80ea6d (patch)
treef3dba682bee770d8ec4eaa9e58a5e2b346227669
parentec317953df3176260f51df5bca42d576c320af33 (diff)
downloadpylint-1be680429bd86e93fe47c4398b82dccded80ea6d.tar.gz
Fixed checking a list of imported names from a module in unused-import case.
-rw-r--r--checkers/variables.py32
-rw-r--r--test/functional/unused_import.py2
-rw-r--r--test/functional/unused_import.txt3
3 files changed, 33 insertions, 4 deletions
diff --git a/checkers/variables.py b/checkers/variables.py
index 85349e6..687e777 100644
--- a/checkers/variables.py
+++ b/checkers/variables.py
@@ -314,6 +314,10 @@ builtins. Remember that you should avoid to define new builtins when possible.'
# don't check unused imports in __init__ files
if not self.config.init_import and node.package:
return
+
+ # fix local names in node.locals dict (xml.etree instead of xml)
+ # TODO: this should be improved in issue astroid#46
+ local_names = {}
for name, stmts in not_consumed.iteritems():
if any(isinstance(stmt, astroid.AssName)
and isinstance(stmt.ass_type(), astroid.AugAssign)
@@ -322,13 +326,33 @@ builtins. Remember that you should avoid to define new builtins when possible.'
for stmt in stmts:
if not isinstance(stmt, astroid.Import) and not isinstance(stmt, astroid.From):
continue
-
+ for imports in stmt.names:
+ if imports[0] == "*":
+ # in case of wildcard import pick the name from inside of imported module
+ name2 = name
+ else:
+ # pick explicitly imported name
+ name2 = imports[0]
+ if name2 not in local_names:
+ local_names[name2] = stmt
+ local_names = sorted(local_names.iteritems(), key=lambda a: a[1].fromlineno)
+
+ checked = set()
+ for name, stmt in local_names:
+ for imports in stmt.names:
# 'import imported_name' or 'from something import imported_name'
- imported_name = stmt.names[0][0]
+ real_name = imported_name = imports[0]
+ if imported_name == "*":
+ real_name = name
# 'import imported_name as as_name'
- as_name = stmt.names[0][1]
+ as_name = imports[1]
+
+ if real_name in checked:
+ continue
+ checked.add(real_name)
- if isinstance(stmt, astroid.Import) or (isinstance(stmt, astroid.From) and not stmt.modname):
+ if isinstance(stmt, astroid.Import) or (isinstance(stmt, astroid.From) \
+ and not stmt.modname):
if as_name is None:
msg = "import %s" % imported_name
else:
diff --git a/test/functional/unused_import.py b/test/functional/unused_import.py
index 08aa0ee..c4e5186 100644
--- a/test/functional/unused_import.py
+++ b/test/functional/unused_import.py
@@ -4,3 +4,5 @@ import xml.sax # [unused-import]
import os.path as test # [unused-import]
from sys import argv as test2 # [unused-import]
from sys import flags # [unused-import]
+# +1:[unused-import,unused-import,unused-import]
+from collections import deque, OrderedDict, Counter
diff --git a/test/functional/unused_import.txt b/test/functional/unused_import.txt
index 53243c1..768082c 100644
--- a/test/functional/unused_import.txt
+++ b/test/functional/unused_import.txt
@@ -3,3 +3,6 @@ unused-import:3::Unused import xml.sax
unused-import:4::Unused os.path imported as test
unused-import:5::Unused argv imported from sys as test2
unused-import:6::Unused flags imported from sys
+unused-import:8::Unused Counter imported from collections
+unused-import:8::Unused OrderedDict imported from collections
+unused-import:8::Unused deque imported from collections