mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 22:14:27 +08:00
StubOnly -> Stub, for all different classes
This commit is contained in:
@@ -15,7 +15,7 @@ from jedi.evaluate import compiled
|
|||||||
from jedi.evaluate.imports import ImportName
|
from jedi.evaluate.imports import ImportName
|
||||||
from jedi.evaluate.filters import ParamName
|
from jedi.evaluate.filters import ParamName
|
||||||
from jedi.evaluate.context import FunctionExecutionContext, MethodContext
|
from jedi.evaluate.context import FunctionExecutionContext, MethodContext
|
||||||
from jedi.evaluate.gradual.typeshed import StubOnlyModuleContext
|
from jedi.evaluate.gradual.typeshed import StubModuleContext
|
||||||
from jedi.evaluate.gradual.stub_context import name_to_stub, stub_to_actual_context_set
|
from jedi.evaluate.gradual.stub_context import name_to_stub, stub_to_actual_context_set
|
||||||
from jedi.api.keywords import KeywordName
|
from jedi.api.keywords import KeywordName
|
||||||
|
|
||||||
@@ -207,7 +207,7 @@ class BaseDefinition(object):
|
|||||||
|
|
||||||
def in_builtin_module(self):
|
def in_builtin_module(self):
|
||||||
"""Whether this is a builtin module."""
|
"""Whether this is a builtin module."""
|
||||||
if isinstance(self._module, StubOnlyModuleContext):
|
if isinstance(self._module, StubModuleContext):
|
||||||
return any(isinstance(context, compiled.CompiledObject)
|
return any(isinstance(context, compiled.CompiledObject)
|
||||||
for context in self._module.non_stub_context_set)
|
for context in self._module.non_stub_context_set)
|
||||||
return isinstance(self._module, compiled.CompiledObject)
|
return isinstance(self._module, compiled.CompiledObject)
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from parso import python_bytes_to_unicode
|
|
||||||
|
|
||||||
from jedi.evaluate.cache import evaluator_method_cache
|
from jedi.evaluate.cache import evaluator_method_cache
|
||||||
from jedi._compatibility import iter_modules, all_suffixes
|
from jedi._compatibility import iter_modules
|
||||||
from jedi.evaluate.filters import GlobalNameFilter, ContextNameMixin, \
|
from jedi.evaluate.filters import GlobalNameFilter, ContextNameMixin, \
|
||||||
AbstractNameDefinition, ParserTreeFilter, DictFilter, MergedFilter
|
AbstractNameDefinition, ParserTreeFilter, DictFilter, MergedFilter
|
||||||
from jedi.evaluate import compiled
|
from jedi.evaluate import compiled
|
||||||
@@ -170,7 +168,7 @@ class ModuleContext(ModuleMixin, TreeContext):
|
|||||||
if self._path is not None and self._path.endswith('.pyi'):
|
if self._path is not None and self._path.endswith('.pyi'):
|
||||||
# Currently this is the way how we identify stubs when e.g. goto is
|
# Currently this is the way how we identify stubs when e.g. goto is
|
||||||
# used in them. This could be changed if stubs would be identified
|
# used in them. This could be changed if stubs would be identified
|
||||||
# sooner and used as StubOnlyModuleContext.
|
# sooner and used as StubModuleContext.
|
||||||
return True
|
return True
|
||||||
return super(ModuleContext, self).is_stub()
|
return super(ModuleContext, self).is_stub()
|
||||||
|
|
||||||
|
|||||||
@@ -10,14 +10,14 @@ from jedi.evaluate.utils import to_list
|
|||||||
from jedi.evaluate.gradual.typing import TypingModuleFilterWrapper, AnnotatedClass
|
from jedi.evaluate.gradual.typing import TypingModuleFilterWrapper, AnnotatedClass
|
||||||
|
|
||||||
|
|
||||||
class _StubOnlyContextMixin(object):
|
class _StubContextMixin(object):
|
||||||
_add_non_stubs_in_filter = False
|
_add_non_stubs_in_filter = False
|
||||||
|
|
||||||
def is_stub(self):
|
def is_stub(self):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _get_stub_only_filters(self, **filter_kwargs):
|
def _get_stub_only_filters(self, **filter_kwargs):
|
||||||
return [StubOnlyFilter(
|
return [StubFilter(
|
||||||
self.evaluator,
|
self.evaluator,
|
||||||
context=self,
|
context=self,
|
||||||
**filter_kwargs
|
**filter_kwargs
|
||||||
@@ -38,11 +38,11 @@ class _StubOnlyContextMixin(object):
|
|||||||
yield f
|
yield f
|
||||||
|
|
||||||
|
|
||||||
class StubOnlyModuleContext(_StubOnlyContextMixin, ModuleContext):
|
class StubModuleContext(_StubContextMixin, ModuleContext):
|
||||||
_add_non_stubs_in_filter = True
|
_add_non_stubs_in_filter = True
|
||||||
|
|
||||||
def __init__(self, non_stub_context_set, *args, **kwargs):
|
def __init__(self, non_stub_context_set, *args, **kwargs):
|
||||||
super(StubOnlyModuleContext, self).__init__(*args, **kwargs)
|
super(StubModuleContext, self).__init__(*args, **kwargs)
|
||||||
self.non_stub_context_set = non_stub_context_set
|
self.non_stub_context_set = non_stub_context_set
|
||||||
|
|
||||||
def _get_first_non_stub_filters(self):
|
def _get_first_non_stub_filters(self):
|
||||||
@@ -50,7 +50,7 @@ class StubOnlyModuleContext(_StubOnlyContextMixin, ModuleContext):
|
|||||||
yield next(context.get_filters(search_global=False))
|
yield next(context.get_filters(search_global=False))
|
||||||
|
|
||||||
def _get_stub_only_filters(self, search_global, **filter_kwargs):
|
def _get_stub_only_filters(self, search_global, **filter_kwargs):
|
||||||
stub_filters = super(StubOnlyModuleContext, self)._get_stub_only_filters(
|
stub_filters = super(StubModuleContext, self)._get_stub_only_filters(
|
||||||
search_global=search_global, **filter_kwargs
|
search_global=search_global, **filter_kwargs
|
||||||
)
|
)
|
||||||
stub_filters += self.iter_star_filters(search_global=search_global)
|
stub_filters += self.iter_star_filters(search_global=search_global)
|
||||||
@@ -58,7 +58,7 @@ class StubOnlyModuleContext(_StubOnlyContextMixin, ModuleContext):
|
|||||||
|
|
||||||
def get_filters(self, search_global=False, until_position=None,
|
def get_filters(self, search_global=False, until_position=None,
|
||||||
origin_scope=None, **kwargs):
|
origin_scope=None, **kwargs):
|
||||||
filters = super(StubOnlyModuleContext, self).get_filters(
|
filters = super(StubModuleContext, self).get_filters(
|
||||||
search_global, until_position, origin_scope, **kwargs
|
search_global, until_position, origin_scope, **kwargs
|
||||||
)
|
)
|
||||||
for f in self._get_base_filters(filters, search_global, until_position, origin_scope):
|
for f in self._get_base_filters(filters, search_global, until_position, origin_scope):
|
||||||
@@ -74,11 +74,11 @@ class StubOnlyModuleContext(_StubOnlyContextMixin, ModuleContext):
|
|||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
class StubOnlyClass(_StubOnlyContextMixin, ClassMixin, ContextWrapper):
|
class StubClass(_StubContextMixin, ClassMixin, ContextWrapper):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class TypingModuleWrapper(StubOnlyModuleContext):
|
class TypingModuleWrapper(StubModuleContext):
|
||||||
def get_filters(self, *args, **kwargs):
|
def get_filters(self, *args, **kwargs):
|
||||||
filters = super(TypingModuleWrapper, self).get_filters(*args, **kwargs)
|
filters = super(TypingModuleWrapper, self).get_filters(*args, **kwargs)
|
||||||
yield TypingModuleFilterWrapper(next(filters))
|
yield TypingModuleFilterWrapper(next(filters))
|
||||||
@@ -123,7 +123,7 @@ def stub_to_actual_context_set(stub_context, ignore_compiled=False):
|
|||||||
if qualified_names is None:
|
if qualified_names is None:
|
||||||
return NO_CONTEXTS
|
return NO_CONTEXTS
|
||||||
|
|
||||||
assert isinstance(stub_module, StubOnlyModuleContext), stub_module
|
assert isinstance(stub_module, StubModuleContext), stub_module
|
||||||
non_stubs = stub_module.non_stub_context_set
|
non_stubs = stub_module.non_stub_context_set
|
||||||
if ignore_compiled:
|
if ignore_compiled:
|
||||||
non_stubs = non_stubs.filter(lambda c: not c.is_compiled())
|
non_stubs = non_stubs.filter(lambda c: not c.is_compiled())
|
||||||
@@ -237,27 +237,27 @@ def to_stub(context):
|
|||||||
return stub_contexts
|
return stub_contexts
|
||||||
|
|
||||||
|
|
||||||
class StubOnlyName(TreeNameDefinition):
|
class _StubName(TreeNameDefinition):
|
||||||
def infer(self):
|
def infer(self):
|
||||||
inferred = super(StubOnlyName, self).infer()
|
inferred = super(_StubName, self).infer()
|
||||||
if self.string_name == 'version_info' and self.get_root_context().py__name__() == 'sys':
|
if self.string_name == 'version_info' and self.get_root_context().py__name__() == 'sys':
|
||||||
return [VersionInfo(c) for c in inferred]
|
return [VersionInfo(c) for c in inferred]
|
||||||
|
|
||||||
return [
|
return [
|
||||||
StubOnlyClass.create_cached(c.evaluator, c) if isinstance(c, ClassContext) else c
|
StubClass.create_cached(c.evaluator, c) if isinstance(c, ClassContext) else c
|
||||||
for c in inferred
|
for c in inferred
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
class StubOnlyFilter(ParserTreeFilter):
|
class StubFilter(ParserTreeFilter):
|
||||||
name_class = StubOnlyName
|
name_class = _StubName
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
self._search_global = kwargs.pop('search_global') # Python 2 :/
|
self._search_global = kwargs.pop('search_global') # Python 2 :/
|
||||||
super(StubOnlyFilter, self).__init__(*args, **kwargs)
|
super(StubFilter, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
def _is_name_reachable(self, name):
|
def _is_name_reachable(self, name):
|
||||||
if not super(StubOnlyFilter, self)._is_name_reachable(name):
|
if not super(StubFilter, self)._is_name_reachable(name):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if not self._search_global:
|
if not self._search_global:
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ from jedi._compatibility import FileNotFoundError
|
|||||||
from jedi.parser_utils import get_cached_code_lines
|
from jedi.parser_utils import get_cached_code_lines
|
||||||
from jedi.evaluate.cache import evaluator_function_cache
|
from jedi.evaluate.cache import evaluator_function_cache
|
||||||
from jedi.evaluate.base_context import ContextSet
|
from jedi.evaluate.base_context import ContextSet
|
||||||
from jedi.evaluate.gradual.stub_context import TypingModuleWrapper, StubOnlyModuleContext
|
from jedi.evaluate.gradual.stub_context import TypingModuleWrapper, StubModuleContext
|
||||||
|
|
||||||
_jedi_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
_jedi_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||||
TYPESHED_PATH = os.path.join(_jedi_path, 'third_party', 'typeshed')
|
TYPESHED_PATH = os.path.join(_jedi_path, 'third_party', 'typeshed')
|
||||||
@@ -124,7 +124,7 @@ def _try_to_load_stub(evaluator, actual_context_set, parent_module_context, impo
|
|||||||
map_ = None
|
map_ = None
|
||||||
if len(import_names) == 1:
|
if len(import_names) == 1:
|
||||||
map_ = _cache_stub_file_map(evaluator.grammar.version_info)
|
map_ = _cache_stub_file_map(evaluator.grammar.version_info)
|
||||||
elif isinstance(parent_module_context, StubOnlyModuleContext):
|
elif isinstance(parent_module_context, StubModuleContext):
|
||||||
if not parent_module_context.is_package:
|
if not parent_module_context.is_package:
|
||||||
# Only if it's a package (= a folder) something can be
|
# Only if it's a package (= a folder) something can be
|
||||||
# imported.
|
# imported.
|
||||||
@@ -156,7 +156,7 @@ def create_stub_module(evaluator, actual_context_set, stub_module_node, path, im
|
|||||||
if import_names == ('typing',):
|
if import_names == ('typing',):
|
||||||
module_cls = TypingModuleWrapper
|
module_cls = TypingModuleWrapper
|
||||||
else:
|
else:
|
||||||
module_cls = StubOnlyModuleContext
|
module_cls = StubModuleContext
|
||||||
file_name = os.path.basename(path)
|
file_name = os.path.basename(path)
|
||||||
stub_module_context = module_cls(
|
stub_module_context = module_cls(
|
||||||
actual_context_set, evaluator, stub_module_node,
|
actual_context_set, evaluator, stub_module_node,
|
||||||
|
|||||||
Reference in New Issue
Block a user