diff options
| author | Claudiu Popa <pcmanticore@gmail.com> | 2014-08-09 12:40:24 +0300 | 
|---|---|---|
| committer | Claudiu Popa <pcmanticore@gmail.com> | 2014-08-09 12:40:24 +0300 | 
| commit | 3d35ce6874ff5c44e347ee7c69d75fb456fa4c04 (patch) | |
| tree | dfcc95400d317d6230d224fd60f1eb39e6035f49 | |
| parent | 18f67ca80f257c5d9edf8fc181934a3d9ed7476b (diff) | |
| download | pylint-git-3d35ce6874ff5c44e347ee7c69d75fb456fa4c04.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 19e526bc4..747195b5f 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 c4e51867b..458798c1b 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 768082c01..e50c2990d 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 | 
