1
0
forked from VimPlug/jedi

Fix relative imports outside of the proper paths

This commit is contained in:
Dave Halter
2019-03-08 14:25:54 +01:00
parent 6b579d53ec
commit 1914d10836
3 changed files with 49 additions and 28 deletions
+8 -5
View File
@@ -82,7 +82,8 @@ class Script(object):
:type sys_path: Environment
"""
def __init__(self, source=None, line=None, column=None, path=None,
encoding='utf-8', sys_path=None, environment=None):
encoding='utf-8', sys_path=None, environment=None,
_project=None):
self._orig_path = path
# An empty path (also empty string) should always result in no path.
self.path = os.path.abspath(path) if path else None
@@ -98,10 +99,12 @@ class Script(object):
if sys_path is not None and not is_py3:
sys_path = list(map(force_unicode, sys_path))
# Load the Python grammar of the current interpreter.
project = get_default_project(
os.path.dirname(self.path)if path else os.getcwd()
)
project = _project
if project is None:
# Load the Python grammar of the current interpreter.
project = get_default_project(
os.path.dirname(self.path)if path else os.getcwd()
)
# TODO deprecate and remove sys_path from the Script API.
if sys_path is not None:
project._sys_path = sys_path
+14 -14
View File
@@ -211,7 +211,7 @@ def _level_to_base_import_path(project_path, directory, level):
# import path for it.
while True:
if d == project_path:
return level_import_paths, None
return level_import_paths, d
dir_name = os.path.basename(d)
if dir_name:
level_import_paths.insert(0, dir_name)
@@ -280,18 +280,19 @@ class Importer(object):
base_import_path, base_directory = _level_to_base_import_path(
self._evaluator.project._path, directory, level,
)
if base_directory is None:
# Everything is lost, the relative import does point
# somewhere out of the filesystem.
self._inference_possible = False
else:
self._fixed_sys_path = [base_directory]
if base_import_path is None:
if import_path:
_add_error(
module_context, import_path[0],
message='Attempted relative import beyond top-level package.'
)
if base_directory is None:
# Everything is lost, the relative import does point
# somewhere out of the filesystem.
self._inference_possible = False
else:
self._fixed_sys_path = [base_directory]
else:
import_path = base_import_path + import_path
self.import_path = import_path
@@ -375,6 +376,9 @@ class Importer(object):
:param only_modules: Indicates wheter it's possible to import a
definition that is not defined in a module.
"""
if not self._inference_possible:
return []
names = []
if self.import_path:
# flask
@@ -416,15 +420,11 @@ class Importer(object):
for filter in context.get_filters(search_global=False):
names += filter.values()
else:
# Empty import path=completion after import
if self.level:
if self.file_path is not None:
path = self.file_path
for i in range(self.level):
path = os.path.dirname(path)
raise 1
names += self._get_module_names([path])
# We only get here if the level cannot be properly calculated.
names += self._get_module_names(self._fixed_sys_path)
else:
# This is just the list of global imports.
names += self._get_module_names()
return names