diff options
author | Michal Nowikowski <godfryd@gmail.com> | 2014-08-06 06:56:17 +0200 |
---|---|---|
committer | Michal Nowikowski <godfryd@gmail.com> | 2014-08-06 06:56:17 +0200 |
commit | f5c6351be7ecbf7c8f6d7afa1a336d854c80eb64 (patch) | |
tree | 956b1c48f40de448ca246a6007262a5a35a8d919 | |
parent | af7fffd376ee43a0e6af4770cef01c53037cfd3e (diff) | |
parent | 1be680429bd86e93fe47c4398b82dccded80ea6d (diff) | |
download | pylint-fix-293.tar.gz |
mergefix-293
-rw-r--r-- | ChangeLog | 2 | ||||
-rw-r--r-- | checkers/variables.py | 58 | ||||
-rw-r--r-- | test/functional/unused_import.py | 8 | ||||
-rw-r--r-- | test/functional/unused_import.txt | 8 |
4 files changed, 67 insertions, 9 deletions
@@ -2,6 +2,8 @@ ChangeLog for Pylint ==================== -- + * Improved presenting unused-import message. Closes issue #293. + * Add new checker for finding spelling errors. New messages: wrong-spelling-in-comment, wrong-spelling-in-docstring. New options: spelling-dict, spelling-ignore-words. diff --git a/checkers/variables.py b/checkers/variables.py index 338615f..687e777 100644 --- a/checkers/variables.py +++ b/checkers/variables.py @@ -170,7 +170,7 @@ MSGS = { 'global-at-module-level', 'Used when you use the "global" statement at the module level \ since it has no effect'), - 'W0611': ('Unused import %s', + 'W0611': ('Unused %s', 'unused-import', 'Used when an imported module or variable is not used.'), 'W0612': ('Unused variable %r', @@ -314,19 +314,59 @@ 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) for stmt in stmts): continue - stmt = stmts[0] - if isinstance(stmt, astroid.Import): - self.add_message('unused-import', args=name, node=stmt) - elif isinstance(stmt, astroid.From) and stmt.modname != '__future__': - if stmt.names[0][0] == '*': - self.add_message('unused-wildcard-import', args=name, node=stmt) - else: - self.add_message('unused-import', args=name, node=stmt) + 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' + real_name = imported_name = imports[0] + if imported_name == "*": + real_name = name + # 'import imported_name as as_name' + 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 as_name is None: + msg = "import %s" % imported_name + else: + msg = "%s imported as %s" % (imported_name, as_name) + self.add_message('unused-import', args=msg, node=stmt) + elif isinstance(stmt, astroid.From) and stmt.modname != '__future__': + if imported_name == '*': + self.add_message('unused-wildcard-import', args=name, node=stmt) + else: + if as_name is None: + msg = "%s imported from %s" % (imported_name, stmt.modname) + else: + msg = "%s imported from %s as %s" % (imported_name, stmt.modname, as_name) + self.add_message('unused-import', args=msg, node=stmt) del self._to_consume def visit_class(self, node): diff --git a/test/functional/unused_import.py b/test/functional/unused_import.py new file mode 100644 index 0000000..c4e5186 --- /dev/null +++ b/test/functional/unused_import.py @@ -0,0 +1,8 @@ +"""unused import""" +import xml.etree # [unused-import] +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 new file mode 100644 index 0000000..768082c --- /dev/null +++ b/test/functional/unused_import.txt @@ -0,0 +1,8 @@ +unused-import:2::Unused import xml.etree +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 |