diff --git a/jedi/api/__init__.py b/jedi/api/__init__.py index 66c1e7fa..04c42daf 100644 --- a/jedi/api/__init__.py +++ b/jedi/api/__init__.py @@ -779,8 +779,7 @@ def preload_module(*modules): :param modules: different module names, list of string. """ for m in modules: - s = "import %s as x; x." % m - Script(s).complete(1, len(s)) + Script(f"import {m}").infer() def set_debug_function(func_cb=debug.print_to_stdout, warnings=True, diff --git a/jedi/inference/compiled/__init__.py b/jedi/inference/compiled/__init__.py index 09ac19f9..6d746251 100644 --- a/jedi/inference/compiled/__init__.py +++ b/jedi/inference/compiled/__init__.py @@ -14,8 +14,9 @@ def builtin_from_name(inference_state, string): else: filter_ = next(typing_builtins_module.get_filters()) name, = filter_.get(string) - value, = name.infer() - return value + # Most of the time there is only symbol, but sometimes there are different + # sys.version_infos, where there are multiple ones, just use the first one. + return next(iter(name.infer())) class ExactValue(LazyValueWrapper): diff --git a/jedi/inference/gradual/typeshed.py b/jedi/inference/gradual/typeshed.py index 50217cd3..651f54e3 100644 --- a/jedi/inference/gradual/typeshed.py +++ b/jedi/inference/gradual/typeshed.py @@ -58,19 +58,8 @@ def _create_stub_map(directory_path_info): def _get_typeshed_directories(version_info): - check_version_list = ['2and3', '3'] - for base in ['stdlib', 'third_party']: - base_path = TYPESHED_PATH.joinpath(base) - base_list = os.listdir(base_path) - for base_list_entry in base_list: - match = re.match(r'(\d+)\.(\d+)$', base_list_entry) - if match is not None: - if match.group(1) == '3' and int(match.group(2)) <= version_info.minor: - check_version_list.append(base_list_entry) - - for check_version in check_version_list: - is_third_party = base != 'stdlib' - yield PathInfo(str(base_path.joinpath(check_version)), is_third_party) + yield PathInfo(str(TYPESHED_PATH.joinpath("stdlib")), False) + yield PathInfo(str(TYPESHED_PATH.joinpath("stubs")), True) _version_cache: Dict[Tuple[int, int], Mapping[str, PathInfo]] = {} diff --git a/jedi/inference/gradual/utils.py b/jedi/inference/gradual/utils.py index af3703c7..0eb6d161 100644 --- a/jedi/inference/gradual/utils.py +++ b/jedi/inference/gradual/utils.py @@ -19,7 +19,7 @@ def load_proper_stub_module(inference_state, grammar, file_io, import_names, mod # /[...]/stdlib/3/os/__init__.pyi -> stdlib/3/os/__init__ rest = relative_path.with_suffix('') # Remove the stdlib/3 or third_party/3.6 part - import_names = rest.parts[2:] + import_names = rest.parts[1:] if rest.name == '__init__': import_names = import_names[:-1] diff --git a/test/test_api/test_api.py b/test/test_api/test_api.py index 8ff559f6..7bd1fc88 100644 --- a/test/test_api/test_api.py +++ b/test/test_api/test_api.py @@ -173,7 +173,7 @@ def test_get_line_code(Script): return Script(source).complete(line=line)[0].get_line_code(**kwargs).replace('\r', '') # On builtin - assert get_line_code('abs') == 'def abs(__x: SupportsAbs[_T]) -> _T: ...\n' + assert get_line_code('abs') == 'def abs(x: SupportsAbs[_T], /) -> _T: ...\n' # On custom code first_line = 'def foo():\n' diff --git a/test/test_inference/test_gradual/test_typeshed.py b/test/test_inference/test_gradual/test_typeshed.py index a7e96b50..0f9ffead 100644 --- a/test/test_inference/test_gradual/test_typeshed.py +++ b/test/test_inference/test_gradual/test_typeshed.py @@ -1,35 +1,18 @@ import os import pytest -from parso.utils import PythonVersionInfo from jedi.inference.gradual import typeshed from jedi.inference.value import TreeInstance, BoundMethod, FunctionValue, \ MethodValue, ClassValue from jedi.inference.names import StubName -TYPESHED_PYTHON3 = os.path.join(typeshed.TYPESHED_PATH, 'stdlib', '3') - - -def test_get_typeshed_directories(): - def get_dirs(version_info): - return { - p.path.replace(str(typeshed.TYPESHED_PATH), '').lstrip(os.path.sep) - for p in typeshed._get_typeshed_directories(version_info) - } - - def transform(set_): - return {x.replace('/', os.path.sep) for x in set_} - - dirs = get_dirs(PythonVersionInfo(3, 7)) - assert dirs == transform({'stdlib/2and3', 'stdlib/3', 'stdlib/3.7', - 'third_party/2and3', - 'third_party/3', 'third_party/3.7'}) +TYPESHED_PYTHON = os.path.join(typeshed.TYPESHED_PATH, 'stdlib') def test_get_stub_files(): - map_ = typeshed._create_stub_map(typeshed.PathInfo(TYPESHED_PYTHON3, is_third_party=False)) - assert map_['functools'].path == os.path.join(TYPESHED_PYTHON3, 'functools.pyi') + map_ = typeshed._create_stub_map(typeshed.PathInfo(TYPESHED_PYTHON, is_third_party=False)) + assert map_['functools'].path == os.path.join(TYPESHED_PYTHON, 'functools.pyi') def test_function(Script, environment): @@ -142,7 +125,7 @@ def test_type_var(Script): def test_math_is_stub(Script, code, full_name): s = Script(code) cos, = s.infer() - wanted = ('typeshed', 'stdlib', '2and3', 'math.pyi') + wanted = ('third_party', 'typeshed', 'stdlib', 'math.pyi') assert cos.module_path.parts[-4:] == wanted assert cos.is_stub() is True assert cos.goto(only_stubs=True) == [cos] @@ -222,14 +205,14 @@ def test_goto_stubs_on_itself(Script, code, type_): def test_module_exists_only_as_stub(Script): try: - import redis # type: ignore[import-untyped] # noqa: F401 + import six # type: ignore[import-untyped] # noqa: F401 except ImportError: pass else: - pytest.skip('redis is already installed, it should only exist as a stub for this test') - redis_path = os.path.join(typeshed.TYPESHED_PATH, 'third_party', '2and3', 'redis') - assert os.path.isdir(redis_path) - assert not Script('import redis').infer() + pytest.skip('six is already installed, it should only exist as a stub for this test') + six_path = os.path.join(typeshed.TYPESHED_PATH, 'stubs', 'six') + assert os.path.isdir(six_path) + assert not Script('import six').infer() def test_django_exists_only_as_stub(Script):