diff options
| author | Michal Nowikowski <godfryd@gmail.com> | 2014-08-04 06:43:41 +0200 |
|---|---|---|
| committer | Michal Nowikowski <godfryd@gmail.com> | 2014-08-04 06:43:41 +0200 |
| commit | a964ddf2b4c4e828824e6db037983fdf14622d62 (patch) | |
| tree | 0b6dbab75186eaad295e8478fd1a812941177551 | |
| parent | 712a62d4f9c3d2c2ada9cb100a672472c41953b3 (diff) | |
| parent | 813773747653ee85ea4d52bf9cb3339f42833844 (diff) | |
| download | pylint-a964ddf2b4c4e828824e6db037983fdf14622d62.tar.gz | |
merge
| -rw-r--r-- | ChangeLog | 2 | ||||
| -rw-r--r-- | checkers/variables.py | 32 | ||||
| -rw-r--r-- | test/functional/unused_import.py | 6 | ||||
| -rw-r--r-- | test/functional/unused_import.txt | 5 |
4 files changed, 36 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..b2f27de 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', @@ -319,14 +319,28 @@ builtins. Remember that you should avoid to define new builtins when possible.' 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 + + imported_name = stmt.names[0][0] # this is: 'import imported_name' or 'from something import imported_name' + as_name = stmt.names[0][1] # this is: 'import imported_name as as_name' + + if isinstance(stmt, astroid.Import): + 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..08aa0ee --- /dev/null +++ b/test/functional/unused_import.py @@ -0,0 +1,6 @@ +"""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] diff --git a/test/functional/unused_import.txt b/test/functional/unused_import.txt new file mode 100644 index 0000000..53243c1 --- /dev/null +++ b/test/functional/unused_import.txt @@ -0,0 +1,5 @@ +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 |
