import-checker: recognize relative imports from parents of current package
authorliscju <piotr.listkiewicz@gmail.com>
Sat, 07 May 2016 19:59:30 +0200
changeset 29122 660d8d4ec7aa
parent 29121 dc406c7e41d6
child 29123 0e6b5a5aca22
import-checker: recognize relative imports from parents of current package So far fromlocal recognizes relative imports of the form: from . import D from .. import E It wasn't prepared for recognizing relative imports like: from ..F import G The bug was not found so far because all relative imports starting from the parent was in the list of allowsymbolicimports like: from ..i18n import from ..node import
contrib/import-checker.py
--- a/contrib/import-checker.py	Fri May 06 21:44:41 2016 +0530
+++ b/contrib/import-checker.py	Sat May 07 19:59:30 2016 +0200
@@ -126,9 +126,15 @@
     False
     >>> fromlocal(None, 1)
     ('foo', 'foo.__init__', True)
+    >>> fromlocal('foo1', 1)
+    ('foo.foo1', 'foo.foo1', False)
     >>> fromlocal2 = fromlocalfunc('foo.xxx.yyy', localmods)
     >>> fromlocal2(None, 2)
     ('foo', 'foo.__init__', True)
+    >>> fromlocal2('bar2', 1)
+    False
+    >>> fromlocal2('bar', 2)
+    ('foo.bar', 'foo.bar.__init__', True)
     """
     prefix = '.'.join(modulename.split('.')[:-1])
     if prefix:
@@ -140,8 +146,12 @@
             assert level > 0
             candidates = ['.'.join(modulename.split('.')[:-level])]
         else:
-            # Check relative name first.
-            candidates = [prefix + name, name]
+            if not level:
+                # Check relative name first.
+                candidates = [prefix + name, name]
+            else:
+                candidates = ['.'.join(modulename.split('.')[:-level]) +
+                              '.' + name]
 
         for n in candidates:
             if n in localmods: