diff options
author | cpopa <devnull@localhost> | 2014-08-09 12:40:24 +0300 |
---|---|---|
committer | cpopa <devnull@localhost> | 2014-08-09 12:40:24 +0300 |
commit | 958c0ee204bf8de510738c5fd744ac917b1c772b (patch) | |
tree | a80441e9e80cd16fdbe701b857e75bf3a442864d | |
parent | 2b3c049494f126d6b7f784ce250e9419e02e47fd (diff) | |
download | pylint-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.
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | checkers/variables.py | 18 | ||||
-rw-r--r-- | test/functional/unused_import.py | 3 | ||||
-rw-r--r-- | test/functional/unused_import.txt | 1 |
4 files changed, 19 insertions, 7 deletions
@@ -40,6 +40,10 @@ ChangeLog for Pylint * Don't emit 'import-error' if an import was protected by a try-except, which excepted ImportError. + + * Fix an 'unused-import' false positive, when the error was emitted + for all the members imported with 'from import' form. + Closes issue #304. 2014-07-26 -- 1.3.0 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: diff --git a/test/functional/unused_import.py b/test/functional/unused_import.py index c4e5186..458798c 100644 --- a/test/functional/unused_import.py +++ b/test/functional/unused_import.py @@ -4,5 +4,6 @@ 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] +# +1:[unused-import,unused-import] from collections import deque, OrderedDict, Counter +DATA = Counter() diff --git a/test/functional/unused_import.txt b/test/functional/unused_import.txt index 768082c..e50c299 100644 --- a/test/functional/unused_import.txt +++ b/test/functional/unused_import.txt @@ -3,6 +3,5 @@ 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 |