forked from VimPlug/jedi
Fix builtin import issues
This commit is contained in:
@@ -185,7 +185,9 @@ class Script(object):
|
|||||||
module, = try_to_merge_with_stub(
|
module, = try_to_merge_with_stub(
|
||||||
self._evaluator, None, module.string_names, ContextSet([module])
|
self._evaluator, None, module.string_names, ContextSet([module])
|
||||||
)
|
)
|
||||||
self._evaluator.module_cache.add(names, ContextSet([module]))
|
if names[0] not in ('builtins', '__builtin__', 'typing'):
|
||||||
|
# These modules are essential for Jedi, so don't overwrite them.
|
||||||
|
self._evaluator.module_cache.add(names, ContextSet([module]))
|
||||||
return module
|
return module
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
|||||||
@@ -138,6 +138,12 @@ class Evaluator(object):
|
|||||||
),
|
),
|
||||||
self,
|
self,
|
||||||
)
|
)
|
||||||
|
# TODO remove this `if`
|
||||||
|
if project is not None:
|
||||||
|
# These imports are essential for Jedi and should therefore always
|
||||||
|
# be present in the module cache.
|
||||||
|
self.builtins_module
|
||||||
|
self.typing_module
|
||||||
|
|
||||||
def import_module(self, import_names, parent_module_context=None,
|
def import_module(self, import_names, parent_module_context=None,
|
||||||
sys_path=None, load_stub=True):
|
sys_path=None, load_stub=True):
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ from jedi._compatibility import find_module_py33, find_module
|
|||||||
from jedi.evaluate import compiled
|
from jedi.evaluate import compiled
|
||||||
from jedi.evaluate import imports
|
from jedi.evaluate import imports
|
||||||
from jedi.api.project import Project
|
from jedi.api.project import Project
|
||||||
from ..helpers import cwd_at, get_example_dir, test_dir
|
from ..helpers import cwd_at, get_example_dir, test_dir, root_dir
|
||||||
|
|
||||||
THIS_DIR = os.path.dirname(__file__)
|
THIS_DIR = os.path.dirname(__file__)
|
||||||
|
|
||||||
@@ -293,8 +293,9 @@ def test_compiled_import_none(monkeypatch, Script):
|
|||||||
"""
|
"""
|
||||||
Related to #1079. An import might somehow fail and return None.
|
Related to #1079. An import might somehow fail and return None.
|
||||||
"""
|
"""
|
||||||
|
script = Script('import sys')
|
||||||
monkeypatch.setattr(compiled, 'load_module', lambda *args, **kwargs: None)
|
monkeypatch.setattr(compiled, 'load_module', lambda *args, **kwargs: None)
|
||||||
assert not Script('import sys').goto_definitions()
|
assert not script.goto_definitions()
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
@@ -402,3 +403,35 @@ def test_import_name_calculation(Script):
|
|||||||
s = Script(path=os.path.join(test_dir, 'completion', 'isinstance.py'))
|
s = Script(path=os.path.join(test_dir, 'completion', 'isinstance.py'))
|
||||||
m = s._get_module()
|
m = s._get_module()
|
||||||
assert m.string_names == ('test', 'completion', 'isinstance')
|
assert m.string_names == ('test', 'completion', 'isinstance')
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('name', ('builtins', 'typing'))
|
||||||
|
def test_pre_defined_imports_module(Script, environment, name):
|
||||||
|
if environment.version_info.major < 3 and name == 'builtins':
|
||||||
|
name = '__builtin__'
|
||||||
|
|
||||||
|
path = os.path.join(root_dir, name + '.py')
|
||||||
|
module = Script('', path=path)._get_module()
|
||||||
|
module_cache = module.evaluator.module_cache
|
||||||
|
assert module.string_names == (name,)
|
||||||
|
m, = module_cache.get((name,))
|
||||||
|
assert m.py__file__() != path
|
||||||
|
assert m.evaluator.builtins_module.py__file__() != path
|
||||||
|
assert m.evaluator.typing_module.py__file__() != path
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('name', ('builtins', 'typing'))
|
||||||
|
def test_import_needed_modules_by_jedi(Script, environment, tmpdir, name):
|
||||||
|
if environment.version_info.major < 3 and name == 'builtins':
|
||||||
|
name = '__builtin__'
|
||||||
|
|
||||||
|
module_path = tmpdir.join(name + '.py')
|
||||||
|
module_path.write('int = ...')
|
||||||
|
script = Script(
|
||||||
|
'import ' + name,
|
||||||
|
path=tmpdir.join('something.py').strpath,
|
||||||
|
sys_path=[tmpdir.strpath] + environment.get_sys_path(),
|
||||||
|
)
|
||||||
|
module, = script.goto_definitions()
|
||||||
|
assert module._evaluator.builtins_module.py__file__() != module_path
|
||||||
|
assert module._evaluator.typing_module.py__file__() != module_path
|
||||||
|
|||||||
Reference in New Issue
Block a user