summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTorsten Marek <tmarek@google.com>2013-01-08 19:19:03 +0100
committerTorsten Marek <tmarek@google.com>2013-01-08 19:19:03 +0100
commite5837c47144284bb5465add6147fe6860c602f83 (patch)
tree886bee262259b5dd99d81b93836384681d4eb632
parent5824d2fca1fde84120b6e74c4f1af1a3023a9fe6 (diff)
downloadpylint-git-e5837c47144284bb5465add6147fe6860c602f83.tar.gz
Fix emission of reimport warnings and extend the testcase.
Closes #112667 --HG-- branch : stable
-rw-r--r--ChangeLog3
-rw-r--r--checkers/imports.py20
-rw-r--r--test/input/func_w0404.py24
-rw-r--r--test/messages/func_w0404.txt6
4 files changed, 43 insertions, 10 deletions
diff --git a/ChangeLog b/ChangeLog
index e5e3e491d..0b0cbbd63 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -14,6 +14,9 @@ ChangeLog for PyLint
* #110839: bind <F5> to Run button in pylint-gui
+ * #112667: Fix emission of reimport warnings for mixed imports and extend
+ the testcase. (patch by Torsten Marek)
+
2012-10-05 -- 0.26.0
* #106534: add --ignore-imports option to code similarity checking
diff --git a/checkers/imports.py b/checkers/imports.py
index 5939a2a90..d264f9dc2 100644
--- a/checkers/imports.py
+++ b/checkers/imports.py
@@ -29,19 +29,25 @@ from pylint.checkers import BaseChecker, EmptyReport
def get_first_import(node, context, name, base, level):
"""return the node where [base.]<name> is imported or None if not found
"""
+ fullname = '%s.%s' % (base, name) if base else name
+
first = None
found = False
- for first in context.values():
+ for first in context.body:
+ if first is node:
+ continue
+ if first.scope() is node.scope() and first.lineno > node.lineno:
+ continue
if isinstance(first, astng.Import):
- if name in [iname[0] for iname in first.names]:
+ if any(fullname == iname[0] for iname in first.names):
found = True
break
elif isinstance(first, astng.From):
- if base == first.modname and level == first.level and \
- name in [iname[0] for iname in first.names]:
+ if level == first.level and any(
+ fullname == '%s.%s' % (first.modname, iname[0]) for iname in first.names):
found = True
break
- if found and first is not node and not are_exclusive(first, node):
+ if found and not are_exclusive(first, node):
return first
# utilities to represents import dependencies as tree and dot graph ###########
@@ -319,7 +325,7 @@ given file (report RP0402 must not be disabled)'}
if mod_path == mod_name or mod_path.startswith(mod_name + '.'):
self.add_message('W0402', node=node, args=mod_path)
- def _check_reimport(self, node, name, basename=None, level=0):
+ def _check_reimport(self, node, name, basename=None, level=None):
"""check if the import is necessary (i.e. not already done)"""
if 'W0404' not in self.active_msgs:
return
@@ -327,7 +333,7 @@ given file (report RP0402 must not be disabled)'}
root = node.root()
contexts = [(frame, level)]
if root is not frame:
- contexts.append((root, 0))
+ contexts.append((root, None))
for context, level in contexts:
first = get_first_import(node, context, name, basename, level)
if first is not None:
diff --git a/test/input/func_w0404.py b/test/input/func_w0404.py
index 563e824fc..0e2f727f3 100644
--- a/test/input/func_w0404.py
+++ b/test/input/func_w0404.py
@@ -1,7 +1,27 @@
-"""pylint ticket #60828"""
+"""Unittests for W0404 (reimport)"""
__revision__ = 0
-def reimport():
+import sys
+
+import xml.etree.ElementTree
+from xml.etree import ElementTree
+
+from multiprocessing import pool
+import multiprocessing.pool
+
+import sys
+
+def no_reimport():
"""docstring"""
import os
+ print os
+
+
+def reimport():
+ """This function contains a reimport."""
+ import sys
+ del sys
+
+
+del sys, ElementTree, xml.etree.ElementTree, pool, multiprocessing.pool
diff --git a/test/messages/func_w0404.txt b/test/messages/func_w0404.txt
index 423121aa7..73d770f86 100644
--- a/test/messages/func_w0404.txt
+++ b/test/messages/func_w0404.txt
@@ -1 +1,5 @@
-W: 7:reimport: Unused variable 'os'
+W: 8: Reimport 'ElementTree' (imported line 7)
+W: 11: Reimport 'multiprocessing.pool' (imported line 10)
+W: 13: Reimport 'sys' (imported line 5)
+W: 23:reimport: Redefining name 'sys' from outer scope (line 5)
+W: 23:reimport: Reimport 'sys' (imported line 5)