forked from VimPlug/jedi
Move the lazy contexts to a separate module.
This commit is contained in:
@@ -1 +1,3 @@
|
|||||||
from jedi.evaluate.context.base import *
|
from jedi.evaluate.context.base import *
|
||||||
|
from jedi.evaluate.context.lazy import AbstractLazyContext, LazyKnownContext, \
|
||||||
|
LazyKnownContexts, LazyTreeContext, LazyUnknownContext, get_merged_lazy_context
|
||||||
|
|||||||
@@ -189,67 +189,6 @@ class TreeContext(Context):
|
|||||||
return '<%s: %s>' % (self.__class__.__name__, self.tree_node)
|
return '<%s: %s>' % (self.__class__.__name__, self.tree_node)
|
||||||
|
|
||||||
|
|
||||||
class AbstractLazyContext(object):
|
|
||||||
def __init__(self, data):
|
|
||||||
self.data = data
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return '<%s: %s>' % (self.__class__.__name__, self.data)
|
|
||||||
|
|
||||||
def infer(self):
|
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
|
|
||||||
class LazyKnownContext(AbstractLazyContext):
|
|
||||||
"""data is a context."""
|
|
||||||
def infer(self):
|
|
||||||
return ContextSet(self.data)
|
|
||||||
|
|
||||||
|
|
||||||
class LazyKnownContexts(AbstractLazyContext):
|
|
||||||
"""data is a ContextSet."""
|
|
||||||
def infer(self):
|
|
||||||
return self.data
|
|
||||||
|
|
||||||
|
|
||||||
class LazyUnknownContext(AbstractLazyContext):
|
|
||||||
def __init__(self):
|
|
||||||
super(LazyUnknownContext, self).__init__(None)
|
|
||||||
|
|
||||||
def infer(self):
|
|
||||||
return NO_CONTEXTS
|
|
||||||
|
|
||||||
|
|
||||||
class LazyTreeContext(AbstractLazyContext):
|
|
||||||
def __init__(self, context, node):
|
|
||||||
super(LazyTreeContext, self).__init__(node)
|
|
||||||
self._context = context
|
|
||||||
# We need to save the predefined names. It's an unfortunate side effect
|
|
||||||
# that needs to be tracked otherwise results will be wrong.
|
|
||||||
self._predefined_names = dict(context.predefined_names)
|
|
||||||
|
|
||||||
def infer(self):
|
|
||||||
old, self._context.predefined_names = \
|
|
||||||
self._context.predefined_names, self._predefined_names
|
|
||||||
try:
|
|
||||||
return self._context.eval_node(self.data)
|
|
||||||
finally:
|
|
||||||
self._context.predefined_names = old
|
|
||||||
|
|
||||||
|
|
||||||
def get_merged_lazy_context(lazy_contexts):
|
|
||||||
if len(lazy_contexts) > 1:
|
|
||||||
return MergedLazyContexts(lazy_contexts)
|
|
||||||
else:
|
|
||||||
return lazy_contexts[0]
|
|
||||||
|
|
||||||
|
|
||||||
class MergedLazyContexts(AbstractLazyContext):
|
|
||||||
"""data is a list of lazy contexts."""
|
|
||||||
def infer(self):
|
|
||||||
return ContextSet.from_sets(l.infer() for l in self.data)
|
|
||||||
|
|
||||||
|
|
||||||
class ContextualizedNode(object):
|
class ContextualizedNode(object):
|
||||||
def __init__(self, context, node):
|
def __init__(self, context, node):
|
||||||
self.context = context
|
self.context = context
|
||||||
@@ -303,6 +242,7 @@ class ContextSet(BaseContextSet):
|
|||||||
return ContextSet.from_iterable(c.py__class__() for c in self._set)
|
return ContextSet.from_iterable(c.py__class__() for c in self._set)
|
||||||
|
|
||||||
def iterate(self, contextualized_node=None):
|
def iterate(self, contextualized_node=None):
|
||||||
|
from jedi.evaluate.context.lazy import get_merged_lazy_context
|
||||||
type_iters = [c.iterate(contextualized_node) for c in self._set]
|
type_iters = [c.iterate(contextualized_node) for c in self._set]
|
||||||
for lazy_contexts in zip_longest(*type_iters):
|
for lazy_contexts in zip_longest(*type_iters):
|
||||||
yield get_merged_lazy_context(
|
yield get_merged_lazy_context(
|
||||||
|
|||||||
61
jedi/evaluate/context/lazy.py
Normal file
61
jedi/evaluate/context/lazy.py
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
from jedi.evaluate.context import ContextSet, NO_CONTEXTS
|
||||||
|
|
||||||
|
class AbstractLazyContext(object):
|
||||||
|
def __init__(self, data):
|
||||||
|
self.data = data
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return '<%s: %s>' % (self.__class__.__name__, self.data)
|
||||||
|
|
||||||
|
def infer(self):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
|
class LazyKnownContext(AbstractLazyContext):
|
||||||
|
"""data is a context."""
|
||||||
|
def infer(self):
|
||||||
|
return ContextSet(self.data)
|
||||||
|
|
||||||
|
|
||||||
|
class LazyKnownContexts(AbstractLazyContext):
|
||||||
|
"""data is a ContextSet."""
|
||||||
|
def infer(self):
|
||||||
|
return self.data
|
||||||
|
|
||||||
|
|
||||||
|
class LazyUnknownContext(AbstractLazyContext):
|
||||||
|
def __init__(self):
|
||||||
|
super(LazyUnknownContext, self).__init__(None)
|
||||||
|
|
||||||
|
def infer(self):
|
||||||
|
return NO_CONTEXTS
|
||||||
|
|
||||||
|
|
||||||
|
class LazyTreeContext(AbstractLazyContext):
|
||||||
|
def __init__(self, context, node):
|
||||||
|
super(LazyTreeContext, self).__init__(node)
|
||||||
|
self._context = context
|
||||||
|
# We need to save the predefined names. It's an unfortunate side effect
|
||||||
|
# that needs to be tracked otherwise results will be wrong.
|
||||||
|
self._predefined_names = dict(context.predefined_names)
|
||||||
|
|
||||||
|
def infer(self):
|
||||||
|
old, self._context.predefined_names = \
|
||||||
|
self._context.predefined_names, self._predefined_names
|
||||||
|
try:
|
||||||
|
return self._context.eval_node(self.data)
|
||||||
|
finally:
|
||||||
|
self._context.predefined_names = old
|
||||||
|
|
||||||
|
|
||||||
|
def get_merged_lazy_context(lazy_contexts):
|
||||||
|
if len(lazy_contexts) > 1:
|
||||||
|
return MergedLazyContexts(lazy_contexts)
|
||||||
|
else:
|
||||||
|
return lazy_contexts[0]
|
||||||
|
|
||||||
|
|
||||||
|
class MergedLazyContexts(AbstractLazyContext):
|
||||||
|
"""data is a list of lazy contexts."""
|
||||||
|
def infer(self):
|
||||||
|
return ContextSet.from_sets(l.infer() for l in self.data)
|
||||||
Reference in New Issue
Block a user