Implement stub tests and a first iteration of loading them from some random place

This commit is contained in:
Dave Halter
2019-05-13 10:13:59 +02:00
parent 8b1d4a7824
commit 5ff3e4d1d1
5 changed files with 57 additions and 14 deletions

View File

@@ -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',):

View File

@@ -0,0 +1 @@
in_stub_only: int

View File

@@ -0,0 +1,2 @@
in_with_stub_both = 5
in_with_stub_python = 8

View File

@@ -0,0 +1,2 @@
in_with_stub_both: str
in_with_stub_stub: float

11
test/completion/stubs.py Normal file
View 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