Relative imports with more than one level did not work

Fixes #784.
This commit is contained in:
Dave Halter
2017-09-14 22:05:04 +02:00
parent ab84030ad2
commit e389c61377
5 changed files with 26 additions and 4 deletions

View File

@@ -223,9 +223,17 @@ class Importer(object):
import_path = []
# TODO add import error.
debug.warning('Attempted relative import beyond top-level package.')
# If no path is defined in the module we have no ideas where we
# are in the file system. Therefore we cannot know what to do.
# In this case we just let the path there and ignore that it's
# a relative path. Not sure if that's a good idea.
else:
# Here we basically rewrite the level to 0.
import_path = tuple(base) + tuple(import_path)
base = tuple(base)
if level > 1:
base = base[:-level + 1]
import_path = base + tuple(import_path)
self.import_path = import_path
@property

View File

@@ -1 +1,3 @@
a = 1.0
from ..random import foobar

View File

@@ -2,3 +2,5 @@
Here because random is also a builtin module.
"""
a = set
foobar = 0

View File

@@ -49,7 +49,7 @@ def scope_nested():
#? float()
import_tree.pkg.mod1.a
#? ['a', '__name__', '__package__', '__file__', '__doc__']
#? ['a', 'foobar', '__name__', '__package__', '__file__', '__doc__']
a = import_tree.pkg.mod1.
import import_tree.random
@@ -283,3 +283,13 @@ def underscore():
# Does that also work for the our own module?
#? ['__file__']
__file__
# -----------------
# complex relative imports #784
# -----------------
def relative():
#? ['foobar']
from import_tree.pkg.mod1 import foobar
#? int()
foobar

View File

@@ -65,9 +65,9 @@ import datetime.date
#? 21 ['import']
from import_tree.pkg import pkg
#? 49 ['a', '__name__', '__doc__', '__file__', '__package__']
#? 49 ['a', 'foobar', '__name__', '__doc__', '__file__', '__package__']
from import_tree.pkg.mod1 import not_existant, # whitespace before
#? ['a', '__name__', '__doc__', '__file__', '__package__']
#? ['a', 'foobar', '__name__', '__doc__', '__file__', '__package__']
from import_tree.pkg.mod1 import not_existant,
#? 22 ['mod1']
from import_tree.pkg. import mod1