mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 22:14:27 +08:00
Implement stub tests and a first iteration of loading them from some random place
This commit is contained in:
@@ -128,6 +128,35 @@ def _try_to_load_stub(evaluator, actual_context_set, parent_module_context, impo
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
for c in actual_context_set:
|
||||
try:
|
||||
method = c.py__file__
|
||||
except AttributeError:
|
||||
pass
|
||||
else:
|
||||
file_path = method()
|
||||
if file_path is not None and file_path.endswith('.py'):
|
||||
m = _try_to_load_stub_from_file(
|
||||
evaluator,
|
||||
actual_context_set,
|
||||
# The file path should end with .pyi
|
||||
file_path + 'i',
|
||||
import_names
|
||||
)
|
||||
if m is not None:
|
||||
return m
|
||||
|
||||
m = _load_from_typeshed(evaluator, actual_context_set, parent_module_context, import_names)
|
||||
if m is not None:
|
||||
return m
|
||||
|
||||
evaluator.stub_module_cache[import_names] = None
|
||||
# If no stub is found, just return the default.
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def _load_from_typeshed(evaluator, actual_context_set, parent_module_context, import_names):
|
||||
import_name = import_names[-1]
|
||||
map_ = None
|
||||
if len(import_names) == 1:
|
||||
@@ -143,22 +172,20 @@ def _try_to_load_stub(evaluator, actual_context_set, parent_module_context, impo
|
||||
if map_ is not None:
|
||||
path = map_.get(import_name)
|
||||
if path is not None:
|
||||
try:
|
||||
stub_module_node = _load_stub(evaluator, path)
|
||||
except FileNotFoundError:
|
||||
# The file has since been removed after looking for it.
|
||||
# TODO maybe empty cache?
|
||||
pass
|
||||
else:
|
||||
return create_stub_module(
|
||||
evaluator, actual_context_set, stub_module_node, path,
|
||||
import_names
|
||||
)
|
||||
evaluator.stub_module_cache[import_names] = None
|
||||
# If no stub is found, just return the default.
|
||||
return _try_to_load_stub_from_file(evaluator, actual_context_set, path, import_names)
|
||||
|
||||
return None
|
||||
|
||||
def _try_to_load_stub_from_file(evaluator, actual_context_set, path, import_names):
|
||||
try:
|
||||
stub_module_node = _load_stub(evaluator, path)
|
||||
except FileNotFoundError:
|
||||
# The file that you're looking for doesn't exist (anymore).
|
||||
return None
|
||||
else:
|
||||
return create_stub_module(
|
||||
evaluator, actual_context_set, stub_module_node, path,
|
||||
import_names
|
||||
)
|
||||
|
||||
def create_stub_module(evaluator, actual_context_set, stub_module_node, path, import_names):
|
||||
if import_names == ('typing',):
|
||||
|
||||
1
test/completion/stub_folder/stub_only.pyi
Normal file
1
test/completion/stub_folder/stub_only.pyi
Normal file
@@ -0,0 +1 @@
|
||||
in_stub_only: int
|
||||
2
test/completion/stub_folder/with_stub.py
Normal file
2
test/completion/stub_folder/with_stub.py
Normal file
@@ -0,0 +1,2 @@
|
||||
in_with_stub_both = 5
|
||||
in_with_stub_python = 8
|
||||
2
test/completion/stub_folder/with_stub.pyi
Normal file
2
test/completion/stub_folder/with_stub.pyi
Normal file
@@ -0,0 +1,2 @@
|
||||
in_with_stub_both: str
|
||||
in_with_stub_stub: float
|
||||
11
test/completion/stubs.py
Normal file
11
test/completion/stubs.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from stub_folder import with_stub, stub_only
|
||||
|
||||
#? int()
|
||||
stub_only.in_stub_only
|
||||
|
||||
#? str()
|
||||
with_stub.in_with_stub_both
|
||||
#? int()
|
||||
with_stub.in_with_stub_python
|
||||
#? float()
|
||||
with_stub.in_with_stub_stub
|
||||
Reference in New Issue
Block a user