forked from VimPlug/jedi
context -> value
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from jedi._compatibility import unicode
|
||||
from jedi.inference.compiled.context import CompiledObject, CompiledName, \
|
||||
from jedi.inference.compiled.value import CompiledObject, CompiledName, \
|
||||
CompiledObjectFilter, CompiledContextName, create_from_access_path
|
||||
from jedi.inference.base_value import ContextWrapper, LazyContextWrapper
|
||||
|
||||
@@ -7,13 +7,13 @@ from jedi.inference.base_value import ContextWrapper, LazyContextWrapper
|
||||
def builtin_from_name(infer_state, string):
|
||||
typing_builtins_module = infer_state.builtins_module
|
||||
if string in ('None', 'True', 'False'):
|
||||
builtins, = typing_builtins_module.non_stub_context_set
|
||||
builtins, = typing_builtins_module.non_stub_value_set
|
||||
filter_ = next(builtins.get_filters())
|
||||
else:
|
||||
filter_ = next(typing_builtins_module.get_filters())
|
||||
name, = filter_.get(string)
|
||||
context, = name.infer()
|
||||
return context
|
||||
value, = name.infer()
|
||||
return value
|
||||
|
||||
|
||||
class CompiledValue(LazyContextWrapper):
|
||||
@@ -27,7 +27,7 @@ class CompiledValue(LazyContextWrapper):
|
||||
return getattr(self._compiled_obj, name)
|
||||
return super(CompiledValue, self).__getattribute__(name)
|
||||
|
||||
def _get_wrapped_context(self):
|
||||
def _get_wrapped_value(self):
|
||||
instance, = builtin_from_name(
|
||||
self.infer_state, self._compiled_obj.name.string_name).execute_with_values()
|
||||
return instance
|
||||
@@ -49,7 +49,7 @@ def create_simple_object(infer_state, obj):
|
||||
return CompiledValue(compiled_obj)
|
||||
|
||||
|
||||
def get_string_context_set(infer_state):
|
||||
def get_string_value_set(infer_state):
|
||||
return builtin_from_name(infer_state, u'str').execute_with_values()
|
||||
|
||||
|
||||
|
||||
@@ -109,7 +109,7 @@ def compiled_objects_cache(attribute_name):
|
||||
Caching the id has the advantage that an object doesn't need to be
|
||||
hashable.
|
||||
"""
|
||||
def wrapper(infer_state, obj, parent_context=None):
|
||||
def wrapper(infer_state, obj, parent_value=None):
|
||||
cache = getattr(infer_state, attribute_name)
|
||||
# Do a very cheap form of caching here.
|
||||
key = id(obj)
|
||||
@@ -119,11 +119,11 @@ def compiled_objects_cache(attribute_name):
|
||||
except KeyError:
|
||||
# TODO wuaaaarrghhhhhhhh
|
||||
if attribute_name == 'mixed_cache':
|
||||
result = func(infer_state, obj, parent_context)
|
||||
result = func(infer_state, obj, parent_value)
|
||||
else:
|
||||
result = func(infer_state, obj)
|
||||
# Need to cache all of them, otherwise the id could be overwritten.
|
||||
cache[key] = result, obj, parent_context
|
||||
cache[key] = result, obj, parent_value
|
||||
return result
|
||||
return wrapper
|
||||
|
||||
|
||||
@@ -14,12 +14,12 @@ from jedi.cache import underscore_memoization
|
||||
from jedi.file_io import FileIO
|
||||
from jedi.inference.base_value import ContextSet, ContextWrapper
|
||||
from jedi.inference.helpers import SimpleGetItemNotFound
|
||||
from jedi.inference.context import ModuleContext
|
||||
from jedi.inference.value import ModuleContext
|
||||
from jedi.inference.cache import infer_state_function_cache
|
||||
from jedi.inference.compiled.getattr_static import getattr_static
|
||||
from jedi.inference.compiled.access import compiled_objects_cache, \
|
||||
ALLOWED_GETITEM_TYPES, get_api_type
|
||||
from jedi.inference.compiled.context import create_cached_compiled_object
|
||||
from jedi.inference.compiled.value import create_cached_compiled_object
|
||||
from jedi.inference.gradual.conversion import to_stub
|
||||
|
||||
_sentinel = object()
|
||||
@@ -42,8 +42,8 @@ class MixedObject(ContextWrapper):
|
||||
fewer special cases, because we in Python you don't have the same freedoms
|
||||
to modify the runtime.
|
||||
"""
|
||||
def __init__(self, compiled_object, tree_context):
|
||||
super(MixedObject, self).__init__(tree_context)
|
||||
def __init__(self, compiled_object, tree_value):
|
||||
super(MixedObject, self).__init__(tree_value)
|
||||
self.compiled_object = compiled_object
|
||||
self.access_handle = compiled_object.access_handle
|
||||
|
||||
@@ -56,7 +56,7 @@ class MixedObject(ContextWrapper):
|
||||
return self.compiled_object.get_signatures()
|
||||
|
||||
def py__call__(self, arguments):
|
||||
return (to_stub(self._wrapped_context) or self._wrapped_context).py__call__(arguments)
|
||||
return (to_stub(self._wrapped_value) or self._wrapped_value).py__call__(arguments)
|
||||
|
||||
def get_safe_value(self, default=_sentinel):
|
||||
if default is _sentinel:
|
||||
@@ -83,11 +83,11 @@ class MixedName(compiled.CompiledName):
|
||||
"""
|
||||
@property
|
||||
def start_pos(self):
|
||||
contexts = list(self.infer())
|
||||
if not contexts:
|
||||
values = list(self.infer())
|
||||
if not values:
|
||||
# This means a start_pos that doesn't exist (compiled objects).
|
||||
return 0, 0
|
||||
return contexts[0].name.start_pos
|
||||
return values[0].name.start_pos
|
||||
|
||||
@start_pos.setter
|
||||
def start_pos(self, value):
|
||||
@@ -97,20 +97,20 @@ class MixedName(compiled.CompiledName):
|
||||
@underscore_memoization
|
||||
def infer(self):
|
||||
# TODO use logic from compiled.CompiledObjectFilter
|
||||
access_paths = self.parent_context.access_handle.getattr_paths(
|
||||
access_paths = self.parent_value.access_handle.getattr_paths(
|
||||
self.string_name,
|
||||
default=None
|
||||
)
|
||||
assert len(access_paths)
|
||||
contexts = [None]
|
||||
values = [None]
|
||||
for access in access_paths:
|
||||
contexts = ContextSet.from_sets(
|
||||
_create(self._infer_state, access, parent_context=c)
|
||||
values = ContextSet.from_sets(
|
||||
_create(self._infer_state, access, parent_value=c)
|
||||
if c is None or isinstance(c, MixedObject)
|
||||
else ContextSet({create_cached_compiled_object(c.infer_state, access, c)})
|
||||
for c in contexts
|
||||
for c in values
|
||||
)
|
||||
return contexts
|
||||
return values
|
||||
|
||||
@property
|
||||
def api_type(self):
|
||||
@@ -230,11 +230,11 @@ def _find_syntax_node_name(infer_state, python_object):
|
||||
|
||||
|
||||
@compiled_objects_cache('mixed_cache')
|
||||
def _create(infer_state, access_handle, parent_context, *args):
|
||||
def _create(infer_state, access_handle, parent_value, *args):
|
||||
compiled_object = create_cached_compiled_object(
|
||||
infer_state,
|
||||
access_handle,
|
||||
parent_context=parent_context and parent_context.compiled_object
|
||||
parent_value=parent_value and parent_value.compiled_object
|
||||
)
|
||||
|
||||
# TODO accessing this is bad, but it probably doesn't matter that much,
|
||||
@@ -246,17 +246,17 @@ def _create(infer_state, access_handle, parent_context, *args):
|
||||
if type(python_object) in (dict, list, tuple):
|
||||
return ContextSet({compiled_object})
|
||||
|
||||
tree_contexts = to_stub(compiled_object)
|
||||
if not tree_contexts:
|
||||
tree_values = to_stub(compiled_object)
|
||||
if not tree_values:
|
||||
return ContextSet({compiled_object})
|
||||
else:
|
||||
module_node, tree_node, file_io, code_lines = result
|
||||
|
||||
if parent_context is None:
|
||||
if parent_value is None:
|
||||
# TODO this __name__ is probably wrong.
|
||||
name = compiled_object.get_root_context().py__name__()
|
||||
name = compiled_object.get_root_value().py__name__()
|
||||
string_names = tuple(name.split('.'))
|
||||
module_context = ModuleContext(
|
||||
module_value = ModuleContext(
|
||||
infer_state, module_node,
|
||||
file_io=file_io,
|
||||
string_names=string_names,
|
||||
@@ -264,28 +264,28 @@ def _create(infer_state, access_handle, parent_context, *args):
|
||||
is_package=hasattr(compiled_object, 'py__path__'),
|
||||
)
|
||||
if name is not None:
|
||||
infer_state.module_cache.add(string_names, ContextSet([module_context]))
|
||||
infer_state.module_cache.add(string_names, ContextSet([module_value]))
|
||||
else:
|
||||
if parent_context.tree_node.get_root_node() != module_node:
|
||||
if parent_value.tree_node.get_root_node() != module_node:
|
||||
# This happens e.g. when __module__ is wrong, or when using
|
||||
# TypeVar('foo'), where Jedi uses 'foo' as the name and
|
||||
# Python's TypeVar('foo').__module__ will be typing.
|
||||
return ContextSet({compiled_object})
|
||||
module_context = parent_context.get_root_context()
|
||||
module_value = parent_value.get_root_value()
|
||||
|
||||
tree_contexts = ContextSet({
|
||||
module_context.create_context(
|
||||
tree_values = ContextSet({
|
||||
module_value.create_value(
|
||||
tree_node,
|
||||
node_is_context=True,
|
||||
node_is_value=True,
|
||||
node_is_object=True
|
||||
)
|
||||
})
|
||||
if tree_node.type == 'classdef':
|
||||
if not access_handle.is_class():
|
||||
# Is an instance, not a class.
|
||||
tree_contexts = tree_contexts.execute_with_values()
|
||||
tree_values = tree_values.execute_with_values()
|
||||
|
||||
return ContextSet(
|
||||
MixedObject(compiled_object, tree_context=tree_context)
|
||||
for tree_context in tree_contexts
|
||||
MixedObject(compiled_object, tree_value=tree_value)
|
||||
for tree_value in tree_values
|
||||
)
|
||||
|
||||
@@ -12,7 +12,7 @@ from jedi.inference.filters import AbstractFilter
|
||||
from jedi.inference.names import AbstractNameDefinition, ContextNameMixin, \
|
||||
ParamNameInterface
|
||||
from jedi.inference.base_value import Context, ContextSet, NO_CONTEXTS
|
||||
from jedi.inference.lazy_context import LazyKnownContext
|
||||
from jedi.inference.lazy_value import LazyKnownContext
|
||||
from jedi.inference.compiled.access import _sentinel
|
||||
from jedi.inference.cache import infer_state_function_cache
|
||||
from jedi.inference.helpers import reraise_getitem_errors
|
||||
@@ -41,8 +41,8 @@ class CheckAttribute(object):
|
||||
|
||||
|
||||
class CompiledObject(Context):
|
||||
def __init__(self, infer_state, access_handle, parent_context=None):
|
||||
super(CompiledObject, self).__init__(infer_state, parent_context)
|
||||
def __init__(self, infer_state, access_handle, parent_value=None):
|
||||
super(CompiledObject, self).__init__(infer_state, parent_value)
|
||||
self.access_handle = access_handle
|
||||
|
||||
def py__call__(self, arguments):
|
||||
@@ -57,9 +57,9 @@ class CompiledObject(Context):
|
||||
return super(CompiledObject, self).py__call__(arguments)
|
||||
else:
|
||||
if self.access_handle.is_class():
|
||||
from jedi.inference.context import CompiledInstance
|
||||
from jedi.inference.value import CompiledInstance
|
||||
return ContextSet([
|
||||
CompiledInstance(self.infer_state, self.parent_context, self, arguments)
|
||||
CompiledInstance(self.infer_state, self.parent_value, self, arguments)
|
||||
])
|
||||
else:
|
||||
return ContextSet(self._execute_function(arguments))
|
||||
@@ -189,24 +189,24 @@ class CompiledObject(Context):
|
||||
|
||||
return ContextSet([create_from_access_path(self.infer_state, access)])
|
||||
|
||||
def py__getitem__(self, index_context_set, contextualized_node):
|
||||
def py__getitem__(self, index_value_set, valueualized_node):
|
||||
all_access_paths = self.access_handle.py__getitem__all_values()
|
||||
if all_access_paths is None:
|
||||
# This means basically that no __getitem__ has been defined on this
|
||||
# object.
|
||||
return super(CompiledObject, self).py__getitem__(index_context_set, contextualized_node)
|
||||
return super(CompiledObject, self).py__getitem__(index_value_set, valueualized_node)
|
||||
return ContextSet(
|
||||
create_from_access_path(self.infer_state, access)
|
||||
for access in all_access_paths
|
||||
)
|
||||
|
||||
def py__iter__(self, contextualized_node=None):
|
||||
def py__iter__(self, valueualized_node=None):
|
||||
# Python iterators are a bit strange, because there's no need for
|
||||
# the __iter__ function as long as __getitem__ is defined (it will
|
||||
# just start with __getitem__(0). This is especially true for
|
||||
# Python 2 strings, where `str.__iter__` is not even defined.
|
||||
if not self.access_handle.has_iter():
|
||||
for x in super(CompiledObject, self).py__iter__(contextualized_node):
|
||||
for x in super(CompiledObject, self).py__iter__(valueualized_node):
|
||||
yield x
|
||||
|
||||
access_path_list = self.access_handle.py__iter__list()
|
||||
@@ -269,18 +269,18 @@ class CompiledObject(Context):
|
||||
|
||||
|
||||
class CompiledName(AbstractNameDefinition):
|
||||
def __init__(self, infer_state, parent_context, name):
|
||||
def __init__(self, infer_state, parent_value, name):
|
||||
self._infer_state = infer_state
|
||||
self.parent_context = parent_context
|
||||
self.parent_value = parent_value
|
||||
self.string_name = name
|
||||
|
||||
def _get_qualified_names(self):
|
||||
parent_qualified_names = self.parent_context.get_qualified_names()
|
||||
parent_qualified_names = self.parent_value.get_qualified_names()
|
||||
return parent_qualified_names + (self.string_name,)
|
||||
|
||||
def __repr__(self):
|
||||
try:
|
||||
name = self.parent_context.name # __name__ is not defined all the time
|
||||
name = self.parent_value.name # __name__ is not defined all the time
|
||||
except AttributeError:
|
||||
name = None
|
||||
return '<%s: (%s).%s>' % (self.__class__.__name__, name, self.string_name)
|
||||
@@ -296,13 +296,13 @@ class CompiledName(AbstractNameDefinition):
|
||||
@underscore_memoization
|
||||
def infer(self):
|
||||
return ContextSet([_create_from_name(
|
||||
self._infer_state, self.parent_context, self.string_name
|
||||
self._infer_state, self.parent_value, self.string_name
|
||||
)])
|
||||
|
||||
|
||||
class SignatureParamName(ParamNameInterface, AbstractNameDefinition):
|
||||
def __init__(self, compiled_obj, signature_param):
|
||||
self.parent_context = compiled_obj.parent_context
|
||||
self.parent_value = compiled_obj.parent_value
|
||||
self._signature_param = signature_param
|
||||
|
||||
@property
|
||||
@@ -322,19 +322,19 @@ class SignatureParamName(ParamNameInterface, AbstractNameDefinition):
|
||||
|
||||
def infer(self):
|
||||
p = self._signature_param
|
||||
infer_state = self.parent_context.infer_state
|
||||
contexts = NO_CONTEXTS
|
||||
infer_state = self.parent_value.infer_state
|
||||
values = NO_CONTEXTS
|
||||
if p.has_default:
|
||||
contexts = ContextSet([create_from_access_path(infer_state, p.default)])
|
||||
values = ContextSet([create_from_access_path(infer_state, p.default)])
|
||||
if p.has_annotation:
|
||||
annotation = create_from_access_path(infer_state, p.annotation)
|
||||
contexts |= annotation.execute_with_values()
|
||||
return contexts
|
||||
values |= annotation.execute_with_values()
|
||||
return values
|
||||
|
||||
|
||||
class UnresolvableParamName(ParamNameInterface, AbstractNameDefinition):
|
||||
def __init__(self, compiled_obj, name, default):
|
||||
self.parent_context = compiled_obj.parent_context
|
||||
self.parent_value = compiled_obj.parent_value
|
||||
self.string_name = name
|
||||
self._default = default
|
||||
|
||||
@@ -352,10 +352,10 @@ class UnresolvableParamName(ParamNameInterface, AbstractNameDefinition):
|
||||
|
||||
|
||||
class CompiledContextName(ContextNameMixin, AbstractNameDefinition):
|
||||
def __init__(self, context, name):
|
||||
def __init__(self, value, name):
|
||||
self.string_name = name
|
||||
self._context = context
|
||||
self.parent_context = context.parent_context
|
||||
self._value = value
|
||||
self.parent_value = value.parent_value
|
||||
|
||||
|
||||
class EmptyCompiledName(AbstractNameDefinition):
|
||||
@@ -365,7 +365,7 @@ class EmptyCompiledName(AbstractNameDefinition):
|
||||
nothing.
|
||||
"""
|
||||
def __init__(self, infer_state, name):
|
||||
self.parent_context = infer_state.builtins_module
|
||||
self.parent_value = infer_state.builtins_module
|
||||
self.string_name = name
|
||||
|
||||
def infer(self):
|
||||
@@ -509,33 +509,33 @@ def _parse_function_doc(doc):
|
||||
|
||||
def _create_from_name(infer_state, compiled_object, name):
|
||||
access_paths = compiled_object.access_handle.getattr_paths(name, default=None)
|
||||
parent_context = compiled_object
|
||||
if parent_context.is_class():
|
||||
parent_context = parent_context.parent_context
|
||||
parent_value = compiled_object
|
||||
if parent_value.is_class():
|
||||
parent_value = parent_value.parent_value
|
||||
|
||||
context = None
|
||||
value = None
|
||||
for access_path in access_paths:
|
||||
context = create_cached_compiled_object(
|
||||
infer_state, access_path, parent_context=context
|
||||
value = create_cached_compiled_object(
|
||||
infer_state, access_path, parent_value=value
|
||||
)
|
||||
return context
|
||||
return value
|
||||
|
||||
|
||||
def _normalize_create_args(func):
|
||||
"""The cache doesn't care about keyword vs. normal args."""
|
||||
def wrapper(infer_state, obj, parent_context=None):
|
||||
return func(infer_state, obj, parent_context)
|
||||
def wrapper(infer_state, obj, parent_value=None):
|
||||
return func(infer_state, obj, parent_value)
|
||||
return wrapper
|
||||
|
||||
|
||||
def create_from_access_path(infer_state, access_path):
|
||||
parent_context = None
|
||||
parent_value = None
|
||||
for name, access in access_path.accesses:
|
||||
parent_context = create_cached_compiled_object(infer_state, access, parent_context)
|
||||
return parent_context
|
||||
parent_value = create_cached_compiled_object(infer_state, access, parent_value)
|
||||
return parent_value
|
||||
|
||||
|
||||
@_normalize_create_args
|
||||
@infer_state_function_cache()
|
||||
def create_cached_compiled_object(infer_state, access_handle, parent_context):
|
||||
return CompiledObject(infer_state, access_handle, parent_context)
|
||||
def create_cached_compiled_object(infer_state, access_handle, parent_value):
|
||||
return CompiledObject(infer_state, access_handle, parent_value)
|
||||
Reference in New Issue
Block a user