mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-07 22:44:27 +08:00
Refactor load_module_from_path to be simpler
This commit is contained in:
@@ -444,13 +444,14 @@ def _load_python_module(inference_state, file_io,
|
||||
folder_io = FolderIO(folder_io.path[:-6])
|
||||
if file_io.path.endswith('__init__.pyi'):
|
||||
python_file_io = folder_io.get_file_io('__init__.py')
|
||||
stub_import_names = import_names
|
||||
else:
|
||||
python_file_io = folder_io.get_file_io(import_names[-1] + '.py')
|
||||
stub_import_names = import_names[:-1]
|
||||
|
||||
try:
|
||||
v = load_module_from_path(inference_state, python_file_io, stub_import_names)
|
||||
v = load_module_from_path(
|
||||
inference_state, python_file_io,
|
||||
import_names, is_package=is_package
|
||||
)
|
||||
values = ValueSet([v])
|
||||
except FileNotFoundError:
|
||||
values = NO_VALUES
|
||||
@@ -486,24 +487,18 @@ def _load_builtin_module(inference_state, import_names=None, sys_path=None):
|
||||
return module
|
||||
|
||||
|
||||
def load_module_from_path(inference_state, file_io, base_names=None):
|
||||
def load_module_from_path(inference_state, file_io, import_names=None, is_package=None):
|
||||
"""
|
||||
This should pretty much only be used for get_modules_containing_name. It's
|
||||
here to ensure that a random path is still properly loaded into the Jedi
|
||||
module structure.
|
||||
"""
|
||||
path = file_io.path
|
||||
if base_names:
|
||||
module_name = os.path.basename(path)
|
||||
module_name = sys_path.remove_python_path_suffix(module_name)
|
||||
is_package = module_name == '__init__'
|
||||
if is_package:
|
||||
import_names = base_names
|
||||
else:
|
||||
import_names = base_names + (module_name,)
|
||||
else:
|
||||
if import_names is None:
|
||||
e_sys_path = inference_state.get_sys_path()
|
||||
import_names, is_package = sys_path.transform_path_to_dotted(e_sys_path, path)
|
||||
else:
|
||||
assert isinstance(is_package, bool)
|
||||
|
||||
module = _load_python_module(
|
||||
inference_state, file_io,
|
||||
|
||||
@@ -7,7 +7,7 @@ import os
|
||||
|
||||
import pytest
|
||||
|
||||
from jedi.file_io import FileIO, KnownContentFileIO
|
||||
from jedi.file_io import FileIO
|
||||
from jedi._compatibility import find_module_py33, find_module
|
||||
from jedi.inference import compiled
|
||||
from jedi.inference import imports
|
||||
@@ -344,23 +344,6 @@ def test_get_modules_containing_name(inference_state, path, goal, is_package):
|
||||
assert found_module.string_names == goal
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
('path', 'base_names', 'is_package', 'names'), [
|
||||
('/foo/bar.py', ('foo',), False, ('foo', 'bar')),
|
||||
('/foo/bar.py', ('foo', 'baz'), False, ('foo', 'baz', 'bar')),
|
||||
('/foo/__init__.py', ('foo',), True, ('foo',)),
|
||||
('/__init__.py', ('foo',), True, ('foo',)),
|
||||
('/foo/bar/__init__.py', ('foo',), True, ('foo',)),
|
||||
('/foo/bar/__init__.py', ('foo', 'bar'), True, ('foo', 'bar')),
|
||||
]
|
||||
)
|
||||
def test_load_module_from_path(inference_state, path, base_names, is_package, names):
|
||||
file_io = KnownContentFileIO(path, '')
|
||||
m = imports.load_module_from_path(inference_state, file_io, base_names)
|
||||
assert m.is_package() == is_package
|
||||
assert m.string_names == names
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'path', ('api/whatever/test_this.py', 'api/whatever/file'))
|
||||
@pytest.mark.parametrize('empty_sys_path', (False, True))
|
||||
|
||||
Reference in New Issue
Block a user