mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-09 23:34:45 +08:00
Implement is_stub and goto_stubs for the API
This commit is contained in:
@@ -301,6 +301,18 @@ class BaseDefinition(object):
|
|||||||
|
|
||||||
return '.'.join(path if path[0] else path[1:])
|
return '.'.join(path if path[0] else path[1:])
|
||||||
|
|
||||||
|
def is_stub(self):
|
||||||
|
return all(c.is_stub() for c in self._name.infer())
|
||||||
|
|
||||||
|
def goto_stubs(self):
|
||||||
|
if self.is_stub():
|
||||||
|
return [self]
|
||||||
|
|
||||||
|
return [
|
||||||
|
Definition(self._evaluator, d.stub_context.name)
|
||||||
|
for d in self._name.infer() if d.stub_context is not None
|
||||||
|
]
|
||||||
|
|
||||||
def goto_assignments(self):
|
def goto_assignments(self):
|
||||||
if self._name.tree_name is None:
|
if self._name.tree_name is None:
|
||||||
return self
|
return self
|
||||||
|
|||||||
@@ -102,6 +102,10 @@ class Context(HelperContextMixin, BaseContext):
|
|||||||
To be defined by subclasses.
|
To be defined by subclasses.
|
||||||
"""
|
"""
|
||||||
tree_node = None
|
tree_node = None
|
||||||
|
stub_context = None
|
||||||
|
|
||||||
|
def is_stub(self):
|
||||||
|
return False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def api_type(self):
|
def api_type(self):
|
||||||
|
|||||||
@@ -87,6 +87,9 @@ class StubMethodContext(StubFunctionContext):
|
|||||||
class _StubOnlyContextMixin(object):
|
class _StubOnlyContextMixin(object):
|
||||||
_add_non_stubs_in_filter = False
|
_add_non_stubs_in_filter = False
|
||||||
|
|
||||||
|
def is_stub(self):
|
||||||
|
return True
|
||||||
|
|
||||||
def _get_stub_only_filters(self, **filter_kwargs):
|
def _get_stub_only_filters(self, **filter_kwargs):
|
||||||
return [StubOnlyFilter(
|
return [StubOnlyFilter(
|
||||||
self.evaluator,
|
self.evaluator,
|
||||||
@@ -155,6 +158,9 @@ class _CompiledStubContext(ContextWrapper):
|
|||||||
super(_CompiledStubContext, self).__init__(stub_context)
|
super(_CompiledStubContext, self).__init__(stub_context)
|
||||||
self._compiled_context = compiled_context
|
self._compiled_context = compiled_context
|
||||||
|
|
||||||
|
def is_stub(self):
|
||||||
|
return True
|
||||||
|
|
||||||
def py__doc__(self, include_call_signature=False):
|
def py__doc__(self, include_call_signature=False):
|
||||||
doc = self._compiled_context.py__doc__()
|
doc = self._compiled_context.py__doc__()
|
||||||
if include_call_signature:
|
if include_call_signature:
|
||||||
|
|||||||
@@ -152,12 +152,34 @@ def test_type_var(Script):
|
|||||||
assert def_.description == 'TypeVar = object()'
|
assert def_.description == 'TypeVar = object()'
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('code', ('import math', 'from math import cos'))
|
@pytest.mark.parametrize(
|
||||||
def test_math_is_stub(Script, code):
|
'code, full_name', (
|
||||||
|
('import math', 'math'),
|
||||||
|
('from math import cos', 'math.cos')
|
||||||
|
)
|
||||||
|
)
|
||||||
|
def test_math_is_stub(Script, code, full_name):
|
||||||
s = Script(code)
|
s = Script(code)
|
||||||
cos, = s.goto_definitions()
|
cos, = s.goto_definitions()
|
||||||
wanted = os.path.join('typeshed', 'stdlib', '2and3', 'math.pyi')
|
wanted = os.path.join('typeshed', 'stdlib', '2and3', 'math.pyi')
|
||||||
assert cos.module_path.endswith(wanted)
|
assert cos.module_path.endswith(wanted)
|
||||||
|
assert cos.is_stub() is True
|
||||||
|
assert cos.goto_stubs() == [cos]
|
||||||
|
assert cos.full_name == full_name
|
||||||
|
|
||||||
cos, = s.goto_assignments()
|
cos, = s.goto_assignments()
|
||||||
assert cos.module_path.endswith(wanted)
|
assert cos.module_path.endswith(wanted)
|
||||||
|
assert cos.goto_stubs() == [cos]
|
||||||
|
assert cos.is_stub() is True
|
||||||
|
assert cos.full_name == full_name
|
||||||
|
|
||||||
|
|
||||||
|
def test_goto_stubs(Script):
|
||||||
|
s = Script('import os; os')
|
||||||
|
os_module, = s.goto_definitions()
|
||||||
|
assert os_module.full_name == 'os'
|
||||||
|
assert os_module.is_stub() is False
|
||||||
|
stub, = os_module.goto_stubs()
|
||||||
|
assert stub.is_stub() is True
|
||||||
|
|
||||||
|
os_module, = s.goto_assignments()
|
||||||
|
|||||||
Reference in New Issue
Block a user