From c8cae2140fae94fd02e55f0c56cff37f656b0645 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Fri, 29 Sep 2017 15:44:40 +0200 Subject: [PATCH] Move the lazy contexts to a separate module. --- jedi/evaluate/context/__init__.py | 2 + jedi/evaluate/context/base.py | 62 +------------------------------ jedi/evaluate/context/lazy.py | 61 ++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 61 deletions(-) create mode 100644 jedi/evaluate/context/lazy.py diff --git a/jedi/evaluate/context/__init__.py b/jedi/evaluate/context/__init__.py index 199aaf1d..b227a1d3 100644 --- a/jedi/evaluate/context/__init__.py +++ b/jedi/evaluate/context/__init__.py @@ -1 +1,3 @@ from jedi.evaluate.context.base import * +from jedi.evaluate.context.lazy import AbstractLazyContext, LazyKnownContext, \ + LazyKnownContexts, LazyTreeContext, LazyUnknownContext, get_merged_lazy_context diff --git a/jedi/evaluate/context/base.py b/jedi/evaluate/context/base.py index 8db0ff1c..f9574f7e 100644 --- a/jedi/evaluate/context/base.py +++ b/jedi/evaluate/context/base.py @@ -189,67 +189,6 @@ class TreeContext(Context): 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): def __init__(self, context, node): self.context = context @@ -303,6 +242,7 @@ class ContextSet(BaseContextSet): return ContextSet.from_iterable(c.py__class__() for c in self._set) 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] for lazy_contexts in zip_longest(*type_iters): yield get_merged_lazy_context( diff --git a/jedi/evaluate/context/lazy.py b/jedi/evaluate/context/lazy.py new file mode 100644 index 00000000..aeca6690 --- /dev/null +++ b/jedi/evaluate/context/lazy.py @@ -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)