mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-15 18:17:07 +08:00
Refactor the compiled name stub wrappers a bit
This commit is contained in:
@@ -8,7 +8,7 @@ from jedi.evaluate.cache import evaluator_function_cache
|
|||||||
from jedi.cache import memoize_method
|
from jedi.cache import memoize_method
|
||||||
from jedi.evaluate.base_context import ContextSet, iterator_to_context_set
|
from jedi.evaluate.base_context import ContextSet, iterator_to_context_set
|
||||||
from jedi.evaluate.filters import AbstractTreeName, ParserTreeFilter, \
|
from jedi.evaluate.filters import AbstractTreeName, ParserTreeFilter, \
|
||||||
TreeNameDefinition
|
TreeNameDefinition, NameWrapper
|
||||||
from jedi.evaluate.context import ModuleContext, FunctionContext, \
|
from jedi.evaluate.context import ModuleContext, FunctionContext, \
|
||||||
ClassContext, BoundMethod
|
ClassContext, BoundMethod
|
||||||
from jedi.evaluate.context.typing import TypingModuleFilterWrapper, \
|
from jedi.evaluate.context.typing import TypingModuleFilterWrapper, \
|
||||||
@@ -177,7 +177,7 @@ class NameWithStubMixin(object):
|
|||||||
@memoize_method
|
@memoize_method
|
||||||
@iterator_to_context_set
|
@iterator_to_context_set
|
||||||
def infer(self):
|
def infer(self):
|
||||||
actual_contexts = super(NameWithStubMixin, self).infer()
|
actual_contexts = self._get_actual_contexts()
|
||||||
stub_contexts = self._stub_name.infer()
|
stub_contexts = self._stub_name.infer()
|
||||||
|
|
||||||
if not actual_contexts:
|
if not actual_contexts:
|
||||||
@@ -215,12 +215,21 @@ class NameWithStub(NameWithStubMixin, TreeNameDefinition):
|
|||||||
super(NameWithStub, self).__init__(parent_context, tree_name)
|
super(NameWithStub, self).__init__(parent_context, tree_name)
|
||||||
self._stub_name = stub_name
|
self._stub_name = stub_name
|
||||||
|
|
||||||
|
def _get_actual_contexts(self):
|
||||||
|
# This is intentionally a subclass of NameWithStubMixin.
|
||||||
|
return super(NameWithStubMixin, self).infer()
|
||||||
|
|
||||||
class CompiledNameWithStub(NameWithStubMixin, CompiledName):
|
|
||||||
def __init__(self, evaluator, parent_context, name, stub_name):
|
class CompiledNameWithStub(NameWithStubMixin, NameWrapper):
|
||||||
super(CompiledNameWithStub, self).__init__(evaluator, parent_context, name)
|
def __init__(self, compiled_name, stub_name):
|
||||||
|
super(CompiledNameWithStub, self).__init__(stub_name)
|
||||||
|
self._compiled_name = compiled_name
|
||||||
self._stub_name = stub_name
|
self._stub_name = stub_name
|
||||||
|
|
||||||
|
def _get_actual_contexts(self):
|
||||||
|
# This is intentionally a subclass of NameWithStubMixin.
|
||||||
|
return self._compiled_name.infer()
|
||||||
|
|
||||||
|
|
||||||
class StubParserTreeFilter(ParserTreeFilter):
|
class StubParserTreeFilter(ParserTreeFilter):
|
||||||
name_class = NameWithStub
|
name_class = NameWithStub
|
||||||
@@ -266,9 +275,7 @@ class StubParserTreeFilter(ParserTreeFilter):
|
|||||||
for non_stub_name in non_stub_names:
|
for non_stub_name in non_stub_names:
|
||||||
if isinstance(non_stub_name, CompiledName):
|
if isinstance(non_stub_name, CompiledName):
|
||||||
yield CompiledNameWithStub(
|
yield CompiledNameWithStub(
|
||||||
self.context.evaluator,
|
non_stub_name,
|
||||||
non_stub_name.parent_context,
|
|
||||||
non_stub_name.string_name,
|
|
||||||
n
|
n
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
@@ -338,7 +345,7 @@ class StubClassContext(_StubContextFilterMixin, ClassContext):
|
|||||||
|
|
||||||
|
|
||||||
class StubFunctionContext(_MixedStubContextMixin, FunctionContext):
|
class StubFunctionContext(_MixedStubContextMixin, FunctionContext):
|
||||||
def get_function_execution(self, arguments):
|
def get_function_execution(self, arguments=None):
|
||||||
return self.stub_context.get_function_execution(arguments)
|
return self.stub_context.get_function_execution(arguments)
|
||||||
return super().get_function_execution(arguments, tree_node=self.stub_context.tree_node)
|
return super().get_function_execution(arguments, tree_node=self.stub_context.tree_node)
|
||||||
|
|
||||||
|
|||||||
@@ -120,7 +120,8 @@ def test_sys_getwindowsversion(Script, environment):
|
|||||||
def test_sys_hexversion(Script):
|
def test_sys_hexversion(Script):
|
||||||
script = Script('import sys; sys.hexversion')
|
script = Script('import sys; sys.hexversion')
|
||||||
def_, = script.completions()
|
def_, = script.completions()
|
||||||
assert isinstance(def_._name, TreeNameDefinition)
|
assert isinstance(def_._name, typeshed.CompiledNameWithStub), def_._name
|
||||||
|
assert isinstance(def_._name._stub_name, TreeNameDefinition)
|
||||||
assert typeshed._TYPESHED_PATH in def_.module_path
|
assert typeshed._TYPESHED_PATH in def_.module_path
|
||||||
def_, = script.goto_definitions()
|
def_, = script.goto_definitions()
|
||||||
assert def_.name == 'int'
|
assert def_.name == 'int'
|
||||||
|
|||||||
Reference in New Issue
Block a user