1
0
forked from VimPlug/jedi

context -> value

This commit is contained in:
Dave Halter
2019-08-15 01:23:06 +02:00
parent 9e23f4d67b
commit ad4f546aca
68 changed files with 1931 additions and 1931 deletions

View File

@@ -1,6 +1,6 @@
"""
Contexts are the "values" that Python would return. However Contexts are at the
same time also the "contexts" that a user is currently sitting in.
same time also the "values" that a user is currently sitting in.
A ContextSet is typically used to specify the return of a function or any other
static analysis operation. In jedi there are always multiple returns and not
@@ -23,12 +23,12 @@ _sentinel = object()
class HelperContextMixin(object):
def get_root_context(self):
context = self
def get_root_value(self):
value = self
while True:
if context.parent_context is None:
return context
context = context.parent_context
if value.parent_value is None:
return value
value = value.parent_value
@classmethod
@infer_state_as_method_param_cache()
@@ -49,22 +49,22 @@ class HelperContextMixin(object):
def gather_annotation_classes(self):
return ContextSet([self])
def merge_types_of_iterate(self, contextualized_node=None, is_async=False):
def merge_types_of_iterate(self, valueualized_node=None, is_async=False):
return ContextSet.from_sets(
lazy_context.infer()
for lazy_context in self.iterate(contextualized_node, is_async)
lazy_value.infer()
for lazy_value in self.iterate(valueualized_node, is_async)
)
def py__getattribute__(self, name_or_str, name_context=None, position=None,
def py__getattribute__(self, name_or_str, name_value=None, position=None,
search_global=False, is_goto=False,
analysis_errors=True):
"""
:param position: Position of the last statement -> tuple of line, column
"""
if name_context is None:
name_context = self
if name_value is None:
name_value = self
from jedi.inference import finder
f = finder.NameFinder(self.infer_state, self, name_context, name_or_str,
f = finder.NameFinder(self.infer_state, self, name_value, name_or_str,
position, analysis_errors=analysis_errors)
filters = f.get_filters(search_global)
if is_goto:
@@ -72,22 +72,22 @@ class HelperContextMixin(object):
return f.find(filters, attribute_lookup=not search_global)
def py__await__(self):
await_context_set = self.py__getattribute__(u"__await__")
if not await_context_set:
debug.warning('Tried to run __await__ on context %s', self)
return await_context_set.execute_with_values()
await_value_set = self.py__getattribute__(u"__await__")
if not await_value_set:
debug.warning('Tried to run __await__ on value %s', self)
return await_value_set.execute_with_values()
def infer_node(self, node):
return self.infer_state.infer_element(self, node)
def create_context(self, node, node_is_context=False, node_is_object=False):
return self.infer_state.create_context(self, node, node_is_context, node_is_object)
def create_value(self, node, node_is_value=False, node_is_object=False):
return self.infer_state.create_value(self, node, node_is_value, node_is_object)
def iterate(self, contextualized_node=None, is_async=False):
def iterate(self, valueualized_node=None, is_async=False):
debug.dbg('iterate %s', self)
if is_async:
from jedi.inference.lazy_context import LazyKnownContexts
# TODO if no __aiter__ contexts are there, error should be:
from jedi.inference.lazy_value import LazyKnownContexts
# TODO if no __aiter__ values are there, error should be:
# TypeError: 'async for' requires an object with __aiter__ method, got int
return iter([
LazyKnownContexts(
@@ -97,11 +97,11 @@ class HelperContextMixin(object):
.py__stop_iteration_returns()
) # noqa
])
return self.py__iter__(contextualized_node)
return self.py__iter__(valueualized_node)
def is_sub_class_of(self, class_context):
def is_sub_class_of(self, class_value):
for cls in self.py__mro__():
if cls.is_same_class(class_context):
if cls.is_same_class(class_value):
return True
return False
@@ -128,24 +128,24 @@ class Context(HelperContextMixin, BaseContext):
# overwritten.
return self.__class__.__name__.lower()
def py__getitem__(self, index_context_set, contextualized_node):
def py__getitem__(self, index_value_set, valueualized_node):
from jedi.inference import analysis
# TODO this context is probably not right.
# TODO this value is probably not right.
analysis.add(
contextualized_node.context,
valueualized_node.value,
'type-error-not-subscriptable',
contextualized_node.node,
valueualized_node.node,
message="TypeError: '%s' object is not subscriptable" % self
)
return NO_CONTEXTS
def py__iter__(self, contextualized_node=None):
if contextualized_node is not None:
def py__iter__(self, valueualized_node=None):
if valueualized_node is not None:
from jedi.inference import analysis
analysis.add(
contextualized_node.context,
valueualized_node.value,
'type-error-not-iterable',
contextualized_node.node,
valueualized_node.node,
message="TypeError: '%s' object is not iterable" % self)
return iter([])
@@ -191,7 +191,7 @@ class Context(HelperContextMixin, BaseContext):
def get_safe_value(self, default=_sentinel):
if default is _sentinel:
raise ValueError("There exists no safe value for context %s" % self)
raise ValueError("There exists no safe value for value %s" % self)
return default
def py__call__(self, arguments):
@@ -207,18 +207,18 @@ class Context(HelperContextMixin, BaseContext):
return None
def is_stub(self):
# The root context knows if it's a stub or not.
return self.parent_context.is_stub()
# The root value knows if it's a stub or not.
return self.parent_value.is_stub()
def iterate_contexts(contexts, contextualized_node=None, is_async=False):
def iterate_values(values, valueualized_node=None, is_async=False):
"""
Calls `iterate`, on all contexts but ignores the ordering and just returns
all contexts that the iterate functions yield.
Calls `iterate`, on all values but ignores the ordering and just returns
all values that the iterate functions yield.
"""
return ContextSet.from_sets(
lazy_context.infer()
for lazy_context in contexts.iterate(contextualized_node, is_async=is_async)
lazy_value.infer()
for lazy_value in values.iterate(valueualized_node, is_async=is_async)
)
@@ -228,7 +228,7 @@ class _ContextWrapperBase(HelperContextMixin):
@safe_property
def name(self):
from jedi.inference.names import ContextName
wrapped_name = self._wrapped_context.name
wrapped_name = self._wrapped_value.name
if wrapped_name.tree_name is not None:
return ContextName(self, wrapped_name.tree_name)
else:
@@ -241,35 +241,35 @@ class _ContextWrapperBase(HelperContextMixin):
return cls(*args, **kwargs)
def __getattr__(self, name):
assert name != '_wrapped_context', 'Problem with _get_wrapped_context'
return getattr(self._wrapped_context, name)
assert name != '_wrapped_value', 'Problem with _get_wrapped_value'
return getattr(self._wrapped_value, name)
class LazyContextWrapper(_ContextWrapperBase):
@safe_property
@memoize_method
def _wrapped_context(self):
with debug.increase_indent_cm('Resolve lazy context wrapper'):
return self._get_wrapped_context()
def _wrapped_value(self):
with debug.increase_indent_cm('Resolve lazy value wrapper'):
return self._get_wrapped_value()
def __repr__(self):
return '<%s>' % (self.__class__.__name__)
def _get_wrapped_context(self):
def _get_wrapped_value(self):
raise NotImplementedError
class ContextWrapper(_ContextWrapperBase):
def __init__(self, wrapped_context):
self._wrapped_context = wrapped_context
def __init__(self, wrapped_value):
self._wrapped_value = wrapped_value
def __repr__(self):
return '%s(%s)' % (self.__class__.__name__, self._wrapped_context)
return '%s(%s)' % (self.__class__.__name__, self._wrapped_value)
class TreeContext(Context):
def __init__(self, infer_state, parent_context, tree_node):
super(TreeContext, self).__init__(infer_state, parent_context)
def __init__(self, infer_state, parent_value, tree_node):
super(TreeContext, self).__init__(infer_state, parent_value)
self.predefined_names = {}
self.tree_node = tree_node
@@ -278,18 +278,18 @@ class TreeContext(Context):
class ContextualizedNode(object):
def __init__(self, context, node):
self.context = context
def __init__(self, value, node):
self.value = value
self.node = node
def get_root_context(self):
return self.context.get_root_context()
def get_root_value(self):
return self.value.get_root_value()
def infer(self):
return self.context.infer_node(self.node)
return self.value.infer_node(self.node)
def __repr__(self):
return '<%s: %s in %s>' % (self.__class__.__name__, self.node, self.context)
return '<%s: %s in %s>' % (self.__class__.__name__, self.node, self.value)
class ContextualizedName(ContextualizedNode):
@@ -340,18 +340,18 @@ class ContextualizedName(ContextualizedNode):
return indexes
def _getitem(context, index_contexts, contextualized_node):
from jedi.inference.context.iterable import Slice
def _getitem(value, index_values, valueualized_node):
from jedi.inference.value.iterable import Slice
# The actual getitem call.
simple_getitem = getattr(context, 'py__simple_getitem__', None)
simple_getitem = getattr(value, 'py__simple_getitem__', None)
result = NO_CONTEXTS
unused_contexts = set()
for index_context in index_contexts:
unused_values = set()
for index_value in index_values:
if simple_getitem is not None:
index = index_context
if isinstance(index_context, Slice):
index = index_value
if isinstance(index_value, Slice):
index = index.obj
try:
@@ -368,15 +368,15 @@ def _getitem(context, index_contexts, contextualized_node):
except SimpleGetItemNotFound:
pass
unused_contexts.add(index_context)
unused_values.add(index_value)
# The index was somehow not good enough or simply a wrong type.
# Therefore we now iterate through all the contexts and just take
# Therefore we now iterate through all the values and just take
# all results.
if unused_contexts or not index_contexts:
result |= context.py__getitem__(
ContextSet(unused_contexts),
contextualized_node
if unused_values or not index_values:
result |= value.py__getitem__(
ContextSet(unused_values),
valueualized_node
)
debug.dbg('py__getitem__ result: %s', result)
return result
@@ -386,12 +386,12 @@ class ContextSet(BaseContextSet):
def py__class__(self):
return ContextSet(c.py__class__() for c in self._set)
def iterate(self, contextualized_node=None, is_async=False):
from jedi.inference.lazy_context import get_merged_lazy_context
type_iters = [c.iterate(contextualized_node, is_async=is_async) for c in self._set]
for lazy_contexts in zip_longest(*type_iters):
yield get_merged_lazy_context(
[l for l in lazy_contexts if l is not None]
def iterate(self, valueualized_node=None, is_async=False):
from jedi.inference.lazy_value import get_merged_lazy_value
type_iters = [c.iterate(valueualized_node, is_async=is_async) for c in self._set]
for lazy_values in zip_longest(*type_iters):
yield get_merged_lazy_value(
[l for l in lazy_values if l is not None]
)
def execute(self, arguments):
@@ -409,15 +409,15 @@ class ContextSet(BaseContextSet):
return ContextSet.from_sets(_getitem(c, *args, **kwargs) for c in self._set)
def try_merge(self, function_name):
context_set = self.__class__([])
value_set = self.__class__([])
for c in self._set:
try:
method = getattr(c, function_name)
except AttributeError:
pass
else:
context_set |= method()
return context_set
value_set |= method()
return value_set
def gather_annotation_classes(self):
return ContextSet.from_sets([c.gather_annotation_classes() for c in self._set])
@@ -429,7 +429,7 @@ class ContextSet(BaseContextSet):
NO_CONTEXTS = ContextSet([])
def iterator_to_context_set(func):
def iterator_to_value_set(func):
def wrapper(*args, **kwargs):
return ContextSet(func(*args, **kwargs))