mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-08 14:54:47 +08:00
Start reworking the relative imports
This commit is contained in:
@@ -238,29 +238,34 @@ class Importer(object):
|
|||||||
import_path = base + tuple(import_path)
|
import_path = base + tuple(import_path)
|
||||||
else:
|
else:
|
||||||
path = module_context.py__file__()
|
path = module_context.py__file__()
|
||||||
|
import_path = list(import_path)
|
||||||
if path is None:
|
if path is None:
|
||||||
# If no path is defined in the module we have no ideas where we
|
# If no path is defined, our best case is that the current
|
||||||
# are in the file system. Therefore we cannot know what to do.
|
# file is edited by a user on the current working
|
||||||
# In this case we just let the path there and ignore that it's
|
# directory. We need to add an initial path, because it
|
||||||
# a relative path. Not sure if that's a good idea.
|
# will get removed as the name of the current file.
|
||||||
pass
|
directory = os.getcwd()
|
||||||
else:
|
else:
|
||||||
import_path = list(import_path)
|
directory = os.path.dirname(path)
|
||||||
p = path
|
level_import_paths = []
|
||||||
for i in range(level):
|
|
||||||
p = os.path.dirname(p)
|
for i in range(level - 1):
|
||||||
dir_name = os.path.basename(p)
|
directory = os.path.dirname(directory)
|
||||||
# This is not the proper way to do relative imports. However, since
|
while directory != os.path.dirname(directory):
|
||||||
# Jedi cannot be sure about the entry point, we just calculate an
|
if directory == self._evaluator.project._path:
|
||||||
# absolute path here.
|
break
|
||||||
|
dir_name = os.path.basename(directory)
|
||||||
if dir_name:
|
if dir_name:
|
||||||
import_path.insert(0, dir_name)
|
level_import_paths.insert(0, dir_name)
|
||||||
|
directory = os.path.dirname(directory)
|
||||||
else:
|
else:
|
||||||
_add_error(
|
_add_error(
|
||||||
module_context, import_path[-1],
|
module_context, import_path[-1],
|
||||||
message='Attempted relative import beyond top-level package.'
|
message='Attempted relative import beyond top-level package.'
|
||||||
)
|
)
|
||||||
import_path = []
|
self.import_path = []
|
||||||
|
return
|
||||||
|
import_path = level_import_paths + import_path
|
||||||
self.import_path = import_path
|
self.import_path = import_path
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|||||||
@@ -281,7 +281,7 @@ def test_get_modules_containing_name(evaluator, path, goal):
|
|||||||
def test_relative_imports_with_multiple_similar_directories(Script, path, empty_sys_path):
|
def test_relative_imports_with_multiple_similar_directories(Script, path, empty_sys_path):
|
||||||
dir = get_example_dir('issue1209')
|
dir = get_example_dir('issue1209')
|
||||||
script = Script(
|
script = Script(
|
||||||
"from .",
|
"from . ",
|
||||||
path=os.path.join(dir, path),
|
path=os.path.join(dir, path),
|
||||||
)
|
)
|
||||||
# TODO pass this project to the script as a param once that's possible.
|
# TODO pass this project to the script as a param once that's possible.
|
||||||
@@ -292,3 +292,19 @@ def test_relative_imports_with_multiple_similar_directories(Script, path, empty_
|
|||||||
name, import_ = script.completions()
|
name, import_ = script.completions()
|
||||||
assert import_.name == 'import'
|
assert import_.name == 'import'
|
||||||
assert name.name == 'api_test1'
|
assert name.name == 'api_test1'
|
||||||
|
|
||||||
|
|
||||||
|
@cwd_at('test/examples/issue1209/api/whatever/')
|
||||||
|
def test_relative_imports_without_path(Script):
|
||||||
|
script = Script("from . ")
|
||||||
|
name, import_ = script.completions()
|
||||||
|
assert import_.name == 'import'
|
||||||
|
assert name.name == 'api_test1'
|
||||||
|
|
||||||
|
script = Script("from .. ")
|
||||||
|
name, import_ = script.completions()
|
||||||
|
assert import_.name == 'whatever'
|
||||||
|
assert name.name == 'import'
|
||||||
|
|
||||||
|
script = Script("from ... ")
|
||||||
|
assert [c.name for c in script.completions()] == ['api', 'import', 'whatever']
|
||||||
|
|||||||
Reference in New Issue
Block a user