summaryrefslogtreecommitdiff
path: root/checkers/variables.py
diff options
context:
space:
mode:
authorcpopa <devnull@localhost>2014-08-09 12:40:24 +0300
committercpopa <devnull@localhost>2014-08-09 12:40:24 +0300
commit958c0ee204bf8de510738c5fd744ac917b1c772b (patch)
treea80441e9e80cd16fdbe701b857e75bf3a442864d /checkers/variables.py
parent2b3c049494f126d6b7f784ce250e9419e02e47fd (diff)
downloadpylint-958c0ee204bf8de510738c5fd744ac917b1c772b.tar.gz
Fix an 'unused-import' false positive, when the error was emitted for all the members imported with 'from import' form. Closes issue #304.
Diffstat (limited to 'checkers/variables.py')
-rw-r--r--checkers/variables.py18
1 files changed, 13 insertions, 5 deletions
diff --git a/checkers/variables.py b/checkers/variables.py
index 19e526b..747195b 100644
--- a/checkers/variables.py
+++ b/checkers/variables.py
@@ -333,14 +333,19 @@ builtins. Remember that you should avoid to define new builtins when possible.'
if not isinstance(stmt, (astroid.From, astroid.Import)):
continue
for imports in stmt.names:
+ name2 = None
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:
+ if imports[0].find(".") > -1 or name in imports:
+ # Most likely something like 'xml.etree',
+ # which will appear in the .locals as
+ # 'xml'.
+ # Only pick the name if it wasn't consumed.
+ name2 = imports[0]
+ if name2 and name2 not in local_names:
local_names[name2] = stmt
local_names = sorted(local_names.iteritems(),
key=lambda a: a[1].fromlineno)
@@ -355,10 +360,13 @@ builtins. Remember that you should avoid to define new builtins when possible.'
as_name = imports[1]
if real_name in checked:
continue
+ if name not in (real_name, as_name):
+ 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: