mirror of
https://github.com/davidhalter/jedi.git
synced 2026-05-07 22:20:08 +08:00
Start fixing some of the problems with new typeshed
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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]] = {}
|
||||
|
||||
@@ -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]
|
||||
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user