mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 05:54:25 +08:00
Try to use a CompiledStubClass to wrap functions inside classes
This commit is contained in:
@@ -4,7 +4,7 @@ from parso.python import tree
|
||||
|
||||
from jedi._compatibility import zip_longest
|
||||
from jedi import debug
|
||||
from jedi.evaluate.utils import to_list, PushBackIterator
|
||||
from jedi.evaluate.utils import PushBackIterator
|
||||
from jedi.evaluate import analysis
|
||||
from jedi.evaluate.lazy_context import LazyKnownContext, LazyKnownContexts, \
|
||||
LazyTreeContext, get_merged_lazy_context
|
||||
|
||||
@@ -303,11 +303,9 @@ class TreeInstance(AbstractInstanceContext):
|
||||
filter = next(self.class_context.get_filters())
|
||||
for init_name in filter.get('__init__'):
|
||||
for init in init_name.infer():
|
||||
if isinstance(init, OverloadedFunctionContext):
|
||||
for func in init.overloaded_functions:
|
||||
yield func
|
||||
elif isinstance(init, FunctionContext):
|
||||
yield init
|
||||
if init.is_function():
|
||||
for signature in init.get_signatures():
|
||||
yield signature.context
|
||||
|
||||
|
||||
class AnonymousInstance(TreeInstance):
|
||||
|
||||
@@ -3,12 +3,12 @@ from abc import abstractproperty
|
||||
|
||||
class AbstractSignature(object):
|
||||
def __init__(self, context, is_bound=False):
|
||||
self._context = context
|
||||
self.context = context
|
||||
self.is_bound = is_bound
|
||||
|
||||
@abstractproperty
|
||||
def name(self):
|
||||
return self._context.name
|
||||
return self.context.name
|
||||
|
||||
def annotation(self):
|
||||
return None
|
||||
@@ -44,7 +44,7 @@ class TreeSignature(AbstractSignature):
|
||||
class BuiltinSignature(AbstractSignature):
|
||||
@property
|
||||
def _function_context(self):
|
||||
return self._context
|
||||
return self.context
|
||||
|
||||
def bind(self):
|
||||
raise NotImplementedError('pls implement, need test case')
|
||||
|
||||
@@ -5,7 +5,7 @@ from jedi._compatibility import FileNotFoundError
|
||||
from jedi.plugins.base import BasePlugin
|
||||
from jedi.evaluate.cache import evaluator_function_cache
|
||||
from jedi.cache import memoize_method
|
||||
from jedi.parser_utils import get_call_signature_for_any
|
||||
from jedi.parser_utils import get_call_signature_for_any, get_cached_code_lines
|
||||
from jedi.evaluate.base_context import ContextSet, iterator_to_context_set, \
|
||||
ContextWrapper, NO_CONTEXTS
|
||||
from jedi.evaluate.filters import ParserTreeFilter, \
|
||||
@@ -163,12 +163,11 @@ class TypeshedPlugin(BasePlugin):
|
||||
module_cls = TypingModuleWrapper
|
||||
else:
|
||||
module_cls = StubOnlyModuleContext
|
||||
# TODO use code_lines
|
||||
stub_module_context = module_cls(
|
||||
context_set, evaluator, stub_module_node,
|
||||
path=path,
|
||||
string_names=import_names,
|
||||
code_lines=[],
|
||||
code_lines=get_cached_code_lines(evaluator.grammar, path),
|
||||
)
|
||||
modules = _merge_modules(context_set, stub_module_context)
|
||||
return ContextSet(modules)
|
||||
@@ -421,7 +420,7 @@ class StubFunctionContext(FunctionMixin, ContextWrapper):
|
||||
return self.stub_context.get_function_execution(arguments)
|
||||
|
||||
|
||||
class _StubOnlyContext(object):
|
||||
class _StubOnlyContextMixin(object):
|
||||
def _get_stub_only_filters(self, **filter_kwargs):
|
||||
return [StubOnlyFilter(
|
||||
self.evaluator,
|
||||
@@ -438,8 +437,23 @@ class _StubOnlyContext(object):
|
||||
self._get_stub_only_filters(**filter_kwargs),
|
||||
)
|
||||
|
||||
def _get_base_filters(self, filters, search_global=False,
|
||||
until_position=None, origin_scope=None):
|
||||
next(filters) # Ignore the first filter and replace it with our own
|
||||
yield self.get_stub_only_filter(
|
||||
parent_context=self,
|
||||
non_stub_filters=list(self._get_first_non_stub_filters()),
|
||||
search_global=search_global,
|
||||
until_position=until_position,
|
||||
origin_scope=origin_scope,
|
||||
# add_non_stubs=False # TODO add something like this
|
||||
)
|
||||
|
||||
class StubOnlyModuleContext(_StubOnlyContext, ModuleContext):
|
||||
for f in filters:
|
||||
yield f
|
||||
|
||||
|
||||
class StubOnlyModuleContext(_StubOnlyContextMixin, ModuleContext):
|
||||
def __init__(self, non_stub_context_set, *args, **kwargs):
|
||||
super(StubOnlyModuleContext, self).__init__(*args, **kwargs)
|
||||
self.non_stub_context_set = non_stub_context_set
|
||||
@@ -460,27 +474,21 @@ class StubOnlyModuleContext(_StubOnlyContext, ModuleContext):
|
||||
filters = super(StubOnlyModuleContext, self).get_filters(
|
||||
search_global, until_position, origin_scope, **kwargs
|
||||
)
|
||||
next(filters) # Ignore the first filter and replace it with our own
|
||||
|
||||
yield self.get_stub_only_filter(
|
||||
parent_context=self,
|
||||
non_stub_filters=list(self._get_first_non_stub_filters()),
|
||||
search_global=search_global,
|
||||
until_position=until_position,
|
||||
origin_scope=origin_scope,
|
||||
# add_non_stubs=False # TODO add something like this
|
||||
)
|
||||
for f in filters:
|
||||
for f in self._get_base_filters(filters, search_global, until_position, origin_scope):
|
||||
yield f
|
||||
|
||||
|
||||
class StubOnlyClass(_StubOnlyContext, ClassMixin, ContextWrapper):
|
||||
class StubOnlyClass(_StubOnlyContextMixin, ClassMixin, ContextWrapper):
|
||||
pass
|
||||
|
||||
|
||||
class _StubContextMixin(object):
|
||||
class _CompiledStubContext(ContextWrapper):
|
||||
def __init__(self, stub_context, compiled_context):
|
||||
super(_CompiledStubContext, self).__init__(stub_context)
|
||||
self._compiled_context = compiled_context
|
||||
|
||||
def py__doc__(self, include_call_signature=False):
|
||||
doc = self.compiled_context.py__doc__()
|
||||
doc = self._compiled_context.py__doc__()
|
||||
if include_call_signature:
|
||||
call_sig = get_call_signature_for_any(self._wrapped_context.tree_node)
|
||||
if call_sig is not None:
|
||||
@@ -488,16 +496,23 @@ class _StubContextMixin(object):
|
||||
return doc
|
||||
|
||||
|
||||
class CompiledStubFunction(_StubContextMixin, ContextWrapper):
|
||||
def __init__(self, stub_context, compiled_context):
|
||||
super(CompiledStubFunction, self).__init__(stub_context)
|
||||
self.compiled_context = compiled_context
|
||||
|
||||
|
||||
class CompiledStubClass(CompiledStubFunction, ClassMixin):
|
||||
class CompiledStubFunction(_CompiledStubContext):
|
||||
pass
|
||||
|
||||
|
||||
class CompiledStubClass(_StubOnlyContextMixin, _CompiledStubContext, ClassMixin):
|
||||
def _get_first_non_stub_filters(self):
|
||||
yield next(self._compiled_context.get_filters(search_global=False))
|
||||
|
||||
def get_filters(self, search_global=False, until_position=None,
|
||||
origin_scope=None, **kwargs):
|
||||
filters = self._wrapped_context.get_filters(
|
||||
search_global, until_position, origin_scope, **kwargs
|
||||
)
|
||||
for f in self._get_base_filters(filters, search_global, until_position, origin_scope):
|
||||
yield f
|
||||
|
||||
|
||||
class TypingModuleWrapper(StubOnlyModuleContext):
|
||||
# TODO should use this instead of the isinstance check
|
||||
def get_filterss(self, *args, **kwargs):
|
||||
|
||||
Reference in New Issue
Block a user