Get compiled name working a bit better with stubs

This commit is contained in:
Dave Halter
2018-09-04 01:51:02 +02:00
parent 6036ea60d1
commit 74db580671
2 changed files with 33 additions and 23 deletions

View File

@@ -14,6 +14,7 @@ from jedi.evaluate.context import ModuleContext, FunctionContext, \
from jedi.evaluate.context.typing import TypingModuleFilterWrapper, \ from jedi.evaluate.context.typing import TypingModuleFilterWrapper, \
TypingModuleName TypingModuleName
from jedi.evaluate.compiled import CompiledObject from jedi.evaluate.compiled import CompiledObject
from jedi.evaluate.compiled.context import CompiledName
from jedi.evaluate.utils import to_list from jedi.evaluate.utils import to_list
@@ -83,7 +84,6 @@ def _merge_modules(context_set, stub_context):
return return
for context in context_set: for context in context_set:
# TODO what about compiled?
if isinstance(context, ModuleContext): if isinstance(context, ModuleContext):
yield StubModuleContext( yield StubModuleContext(
context.evaluator, context.evaluator,
@@ -94,8 +94,8 @@ def _merge_modules(context_set, stub_context):
code_lines=context.code_lines code_lines=context.code_lines
) )
else: else:
# TODO do we want this? # TODO do we want this? This includes compiled?!
yield context yield stub_context
class TypeshedPlugin(BasePlugin): class TypeshedPlugin(BasePlugin):
@@ -132,11 +132,6 @@ class TypeshedPlugin(BasePlugin):
parent_module_context, parent_module_context,
sys_path sys_path
) )
# Don't use CompiledObjects, they are just annoying and don't
# really help with anything. Just use the stub files instead.
context_set = ContextSet.from_iterable(
c for c in context_set if not isinstance(c, CompiledObject)
)
import_name = import_names[-1] import_name = import_names[-1]
map_ = None map_ = None
if len(import_names) == 1: if len(import_names) == 1:
@@ -173,21 +168,16 @@ class TypeshedPlugin(BasePlugin):
return wrapper return wrapper
class NameWithStub(TreeNameDefinition): class NameWithStubMixin(object):
""" """
This name is only here to mix stub names with non-stub names. The idea is This name is only here to mix stub names with non-stub names. The idea is
that the user can goto the actual name, but end up on the definition of the that the user can goto the actual name, but end up on the definition of the
stub when inferring types. stub when inferring types.
""" """
def __init__(self, parent_context, tree_name, stub_name):
super(NameWithStub, self).__init__(parent_context, tree_name)
self._stub_name = stub_name
@memoize_method @memoize_method
@iterator_to_context_set @iterator_to_context_set
def infer(self): def infer(self):
actual_contexts = super(NameWithStub, self).infer() actual_contexts = super(NameWithStubMixin, self).infer()
stub_contexts = self._stub_name.infer() stub_contexts = self._stub_name.infer()
if not actual_contexts: if not actual_contexts:
@@ -220,6 +210,18 @@ class NameWithStub(TreeNameDefinition):
yield actual_context yield actual_context
class NameWithStub(NameWithStubMixin, TreeNameDefinition):
def __init__(self, parent_context, tree_name, stub_name):
super(NameWithStub, self).__init__(parent_context, tree_name)
self._stub_name = stub_name
class CompiledNameWithStub(NameWithStubMixin, CompiledName):
def __init__(self, evaluator, parent_context, name, stub_name):
super(CompiledNameWithStub, self).__init__(evaluator, parent_context, name)
self._stub_name = stub_name
class StubParserTreeFilter(ParserTreeFilter): class StubParserTreeFilter(ParserTreeFilter):
name_class = NameWithStub name_class = NameWithStub
@@ -262,6 +264,14 @@ class StubParserTreeFilter(ParserTreeFilter):
n = TypingModuleName(n) n = TypingModuleName(n)
if len(non_stub_names): if len(non_stub_names):
for non_stub_name in non_stub_names: for non_stub_name in non_stub_names:
if isinstance(non_stub_name, CompiledName):
yield CompiledNameWithStub(
self.context.evaluator,
non_stub_name.parent_context,
non_stub_name.string_name,
n
)
else:
assert isinstance(non_stub_name, AbstractTreeName), non_stub_name assert isinstance(non_stub_name, AbstractTreeName), non_stub_name
yield self.name_class( yield self.name_class(
non_stub_name.parent_context, non_stub_name.parent_context,

View File

@@ -18,7 +18,7 @@ import textwrap
import pytest import pytest
import jedi import jedi
from ..helpers import TestCase, cwd_at from ..helpers import TestCase
class MixinTestFullName(object): class MixinTestFullName(object):
@@ -52,7 +52,7 @@ class TestFullNameWithGotoDefinitions(MixinTestFullName, TestCase):
self.check(""" self.check("""
import re import re
any_re = re.compile('.*') any_re = re.compile('.*')
any_re""", '_sre.SRE_Pattern') any_re""", 'typing.Pattern')
def test_from_import(self): def test_from_import(self):
self.check('from os import path', 'os.path') self.check('from os import path', 'os.path')