diff --git a/jedi/evaluate/imports.py b/jedi/evaluate/imports.py index bb056b0d..fb8f48c3 100644 --- a/jedi/evaluate/imports.py +++ b/jedi/evaluate/imports.py @@ -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 diff --git a/test/completion/import_tree/pkg/mod1.py b/test/completion/import_tree/pkg/mod1.py index 2b6777c8..fe1d27fa 100644 --- a/test/completion/import_tree/pkg/mod1.py +++ b/test/completion/import_tree/pkg/mod1.py @@ -1 +1,3 @@ a = 1.0 + +from ..random import foobar diff --git a/test/completion/import_tree/random.py b/test/completion/import_tree/random.py index a63ed059..7a34c4e3 100644 --- a/test/completion/import_tree/random.py +++ b/test/completion/import_tree/random.py @@ -2,3 +2,5 @@ Here because random is also a builtin module. """ a = set + +foobar = 0 diff --git a/test/completion/imports.py b/test/completion/imports.py index 246b91e5..5c8626fb 100644 --- a/test/completion/imports.py +++ b/test/completion/imports.py @@ -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 diff --git a/test/completion/on_import.py b/test/completion/on_import.py index 73619e73..733d741e 100644 --- a/test/completion/on_import.py +++ b/test/completion/on_import.py @@ -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