mirror of
https://github.com/davidhalter/jedi.git
synced 2026-05-19 06:49:38 +08:00
Restructure relative importing a bit and improve tests
This commit is contained in:
+16
-13
@@ -221,9 +221,23 @@ class Importer(object):
|
|||||||
base = module_context.py__package__().split('.')
|
base = module_context.py__package__().split('.')
|
||||||
if base == [''] or base == ['__main__']:
|
if base == [''] or base == ['__main__']:
|
||||||
base = []
|
base = []
|
||||||
if level > len(base):
|
# We need to care for two cases, the second is if it's a valid
|
||||||
|
# Python
|
||||||
|
if level <= len(base):
|
||||||
|
# Here we basically rewrite the level to 0.
|
||||||
|
base = tuple(base)
|
||||||
|
if level > 1:
|
||||||
|
base = base[:-level + 1]
|
||||||
|
import_path = base + tuple(import_path)
|
||||||
|
else:
|
||||||
path = module_context.py__file__()
|
path = module_context.py__file__()
|
||||||
if path is not None:
|
if path is None:
|
||||||
|
# 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.
|
||||||
|
pass
|
||||||
|
else:
|
||||||
import_path = list(import_path)
|
import_path = list(import_path)
|
||||||
p = path
|
p = path
|
||||||
for i in range(level):
|
for i in range(level):
|
||||||
@@ -240,17 +254,6 @@ class Importer(object):
|
|||||||
message='Attempted relative import beyond top-level package.'
|
message='Attempted relative import beyond top-level package.'
|
||||||
)
|
)
|
||||||
import_path = []
|
import_path = []
|
||||||
# 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.
|
|
||||||
base = tuple(base)
|
|
||||||
if level > 1:
|
|
||||||
base = base[:-level + 1]
|
|
||||||
|
|
||||||
import_path = base + tuple(import_path)
|
|
||||||
self.import_path = import_path
|
self.import_path = import_path
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|||||||
@@ -277,14 +277,22 @@ def test_get_modules_containing_name(evaluator, path, goal):
|
|||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
'path', ('api/whatever/test_this.py', 'api/whatever/file'))
|
'path', ('api/whatever/test_this.py', 'api/whatever/file'))
|
||||||
def test_relative_imports_with_multiple_similar_directories(Script, path):
|
@pytest.mark.parametrize('empty_sys_path', (False, True))
|
||||||
|
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(
|
if empty_sys_path:
|
||||||
"from .",
|
script = Script(
|
||||||
path=os.path.join(dir, path)
|
"from .",
|
||||||
)
|
path=os.path.join(dir, path),
|
||||||
# TODO pass this project to the script as a param once that's possible.
|
)
|
||||||
script._evaluator.project = Project(dir)
|
script._evaluator.project = Project(dir, smart_sys_path=False)
|
||||||
|
else:
|
||||||
|
script = Script(
|
||||||
|
"from .",
|
||||||
|
path=os.path.join(dir, path),
|
||||||
|
)
|
||||||
|
# TODO pass this project to the script as a param once that's possible.
|
||||||
|
script._evaluator.project = Project(dir)
|
||||||
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'
|
||||||
|
|||||||
Reference in New Issue
Block a user