Don't mix up caches for stubs and python files

This commit is contained in:
Dave Halter
2020-03-09 17:48:36 +01:00
parent 7247c32990
commit cf3d83ee4f
2 changed files with 45 additions and 36 deletions

View File

@@ -2,6 +2,7 @@ import os
import re
from functools import wraps
from jedi import settings
from jedi.file_io import FileIO
from jedi._compatibility import FileNotFoundError, cast_path
from jedi.parser_utils import get_cached_code_lines
@@ -255,11 +256,7 @@ def _load_from_typeshed(inference_state, python_value_set, parent_module_value,
def _try_to_load_stub_from_file(inference_state, python_value_set, file_io, import_names):
try:
stub_module_node = inference_state.parse(
file_io=file_io,
cache=True,
use_latest_grammar=True
)
stub_module_node = parse_stub_module(inference_state, file_io)
except (OSError, IOError): # IOError is Python 2 only
# The file that you're looking for doesn't exist (anymore).
return None
@@ -270,6 +267,16 @@ def _try_to_load_stub_from_file(inference_state, python_value_set, file_io, impo
)
def parse_stub_module(inference_state, file_io):
return inference_state.parse(
file_io=file_io,
cache=True,
diff_cache=settings.fast_parser,
cache_path=settings.cache_directory,
use_latest_grammar=True
)
def create_stub_module(inference_state, python_value_set, stub_module_node, file_io, import_names):
if import_names == ('typing',):
module_cls = TypingModuleWrapper

View File

@@ -29,7 +29,8 @@ from jedi.inference.utils import unite
from jedi.inference.cache import inference_state_method_cache
from jedi.inference.names import ImportName, SubModuleName
from jedi.inference.base_value import ValueSet, NO_VALUES
from jedi.inference.gradual.typeshed import import_module_decorator, create_stub_module
from jedi.inference.gradual.typeshed import import_module_decorator, \
create_stub_module, parse_stub_module
from jedi.inference.value.module import iter_module_names as module_iter_module_names
from jedi.plugins import plugin_manager
@@ -429,36 +430,13 @@ def import_module(inference_state, import_names, parent_module_value, sys_path):
def _load_python_module(inference_state, file_io,
import_names=None, is_package=False):
is_stub = file_io.path.endswith('.pyi')
module_node = inference_state.parse(
file_io=file_io,
cache=True,
diff_cache=settings.fast_parser,
cache_path=settings.cache_directory,
use_latest_grammar=is_stub,
)
if is_stub:
folder_io = file_io.get_parent_folder()
if folder_io.path.endswith('-stubs'):
folder_io = FolderIO(folder_io.path[:-6])
if file_io.path.endswith('__init__.pyi'):
python_file_io = folder_io.get_file_io('__init__.py')
else:
python_file_io = folder_io.get_file_io(import_names[-1] + '.py')
try:
v = load_module_from_path(
inference_state, python_file_io,
import_names, is_package=is_package
)
values = ValueSet([v])
except FileNotFoundError:
values = NO_VALUES
return create_stub_module(
inference_state, values, module_node, file_io, import_names)
from jedi.inference.value import ModuleValue
return ModuleValue(
inference_state, module_node,
@@ -500,13 +478,37 @@ def load_module_from_path(inference_state, file_io, import_names=None, is_packag
else:
assert isinstance(is_package, bool)
module = _load_python_module(
inference_state, file_io,
import_names=import_names,
is_package=is_package,
)
inference_state.module_cache.add(import_names, ValueSet([module]))
return module
is_stub = file_io.path.endswith('.pyi')
if is_stub:
folder_io = file_io.get_parent_folder()
if folder_io.path.endswith('-stubs'):
folder_io = FolderIO(folder_io.path[:-6])
if file_io.path.endswith('__init__.pyi'):
python_file_io = folder_io.get_file_io('__init__.py')
else:
python_file_io = folder_io.get_file_io(import_names[-1] + '.py')
try:
v = load_module_from_path(
inference_state, python_file_io,
import_names, is_package=is_package
)
values = ValueSet([v])
except FileNotFoundError:
values = NO_VALUES
return create_stub_module(
inference_state, values, parse_stub_module(inference_state, file_io),
file_io, import_names
)
else:
module = _load_python_module(
inference_state, file_io,
import_names=import_names,
is_package=is_package,
)
inference_state.module_cache.add(import_names, ValueSet([module]))
return module
def load_namespace_from_path(inference_state, folder_io):