From 5c8300e62a7576e022d30f55fde515f5d473b13f Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Mon, 19 Feb 2018 09:43:50 +0100 Subject: [PATCH] Move all the asynchronous contexts to a separate module --- jedi/evaluate/context/asynchronous.py | 77 +++++++++++++++++++++++++++ jedi/evaluate/context/function.py | 5 +- jedi/evaluate/context/iterable.py | 73 ------------------------- 3 files changed, 80 insertions(+), 75 deletions(-) create mode 100644 jedi/evaluate/context/asynchronous.py diff --git a/jedi/evaluate/context/asynchronous.py b/jedi/evaluate/context/asynchronous.py new file mode 100644 index 00000000..7cb6228f --- /dev/null +++ b/jedi/evaluate/context/asynchronous.py @@ -0,0 +1,77 @@ +from jedi.evaluate import compiled +from jedi.evaluate.filters import has_builtin_methods, \ + register_builtin_method, SpecialMethodFilter +from jedi.evaluate.base_context import ContextSet, Context + + +@has_builtin_methods +class CoroutineMixin(object): + array_type = None + + def get_filters(self, search_global, until_position=None, origin_scope=None): + gen_obj = compiled.get_special_object(self.evaluator, 'COROUTINE_TYPE') + yield SpecialMethodFilter(self, self.builtin_methods, gen_obj) + for filter in gen_obj.get_filters(search_global): + yield filter + + def py__bool__(self): + return True + + def py__class__(self): + gen_obj = compiled.get_special_object(self.evaluator, 'COROUTINE_TYPE') + return gen_obj.py__class__() + + @property + def name(self): + return compiled.CompiledContextName(self, 'coroutine') + + +class Coroutine(CoroutineMixin, Context): + def __init__(self, evaluator, func_execution_context): + super(Coroutine, self).__init__(evaluator, parent_context=evaluator.builtins_module) + self._func_execution_context = func_execution_context + + def execute_await(self): + return self._func_execution_context.get_return_values() + + def __repr__(self): + return "<%s of %s>" % (type(self).__name__, self._func_execution_context) + + +@has_builtin_methods +class AsyncGeneratorMixin(object): + array_type = None + + @register_builtin_method('__anext__') + def py__anext__(self): + return ContextSet.from_sets(lazy_context.infer() for lazy_context in self.py__aiter__()) + + def get_filters(self, search_global, until_position=None, origin_scope=None): + gen_obj = compiled.get_special_object(self.evaluator, 'ASYNC_GENERATOR_TYPE') + yield SpecialMethodFilter(self, self.builtin_methods, gen_obj) + for filter in gen_obj.get_filters(search_global): + yield filter + + def py__bool__(self): + return True + + def py__class__(self): + gen_obj = compiled.get_special_object(self.evaluator, 'ASYNC_GENERATOR_TYPE') + return gen_obj.py__class__() + + @property + def name(self): + return compiled.CompiledContextName(self, 'asyncgenerator') + + +class AsyncGenerator(AsyncGeneratorMixin, Context): + """Handling of `yield` functions.""" + def __init__(self, evaluator, func_execution_context): + super(AsyncGenerator, self).__init__(evaluator, parent_context=evaluator.builtins_module) + self._func_execution_context = func_execution_context + + def py__aiter__(self): + return self._func_execution_context.get_yield_lazy_contexts(is_async=True) + + def __repr__(self): + return "<%s of %s>" % (type(self).__name__, self._func_execution_context) diff --git a/jedi/evaluate/context/function.py b/jedi/evaluate/context/function.py index 1b7356e8..05259714 100644 --- a/jedi/evaluate/context/function.py +++ b/jedi/evaluate/context/function.py @@ -17,6 +17,7 @@ from jedi.evaluate.base_context import ContextualizedNode, NO_CONTEXTS, \ from jedi.evaluate.lazy_context import LazyKnownContexts, LazyKnownContext, \ LazyTreeContext from jedi.evaluate.context import iterable +from jedi.evaluate.context import asynchronous from jedi import parser_utils from jedi.evaluate.parser_cache import get_yield_exprs @@ -70,11 +71,11 @@ class FunctionContext(use_metaclass(CachedMetaClass, TreeContext)): if is_generator: if self.evaluator.environment.version_info < (3, 6): return NO_CONTEXTS - return ContextSet(iterable.AsyncGenerator(self.evaluator, function_execution)) + return ContextSet(asynchronous.AsyncGenerator(self.evaluator, function_execution)) else: if self.evaluator.environment.version_info < (3, 5): return NO_CONTEXTS - return ContextSet(iterable.Coroutine(self.evaluator, function_execution)) + return ContextSet(asynchronous.Coroutine(self.evaluator, function_execution)) else: if is_generator: return ContextSet(iterable.Generator(self.evaluator, function_execution)) diff --git a/jedi/evaluate/context/iterable.py b/jedi/evaluate/context/iterable.py index ff4027b2..04a12e2f 100644 --- a/jedi/evaluate/context/iterable.py +++ b/jedi/evaluate/context/iterable.py @@ -55,79 +55,6 @@ class AbstractIterable(Context): return compiled.CompiledContextName(self, self.array_type) -@has_builtin_methods -class CoroutineMixin(object): - array_type = None - - def get_filters(self, search_global, until_position=None, origin_scope=None): - gen_obj = compiled.get_special_object(self.evaluator, 'COROUTINE_TYPE') - yield SpecialMethodFilter(self, self.builtin_methods, gen_obj) - for filter in gen_obj.get_filters(search_global): - yield filter - - def py__bool__(self): - return True - - def py__class__(self): - gen_obj = compiled.get_special_object(self.evaluator, 'COROUTINE_TYPE') - return gen_obj.py__class__() - - @property - def name(self): - return compiled.CompiledContextName(self, 'coroutine') - - -class Coroutine(CoroutineMixin, Context): - def __init__(self, evaluator, func_execution_context): - super(Coroutine, self).__init__(evaluator, parent_context=evaluator.builtins_module) - self._func_execution_context = func_execution_context - - def execute_await(self): - return self._func_execution_context.get_return_values() - - def __repr__(self): - return "<%s of %s>" % (type(self).__name__, self._func_execution_context) - - -@has_builtin_methods -class AsyncGeneratorMixin(object): - array_type = None - - @register_builtin_method('__anext__') - def py__anext__(self): - return ContextSet.from_sets(lazy_context.infer() for lazy_context in self.py__aiter__()) - - def get_filters(self, search_global, until_position=None, origin_scope=None): - gen_obj = compiled.get_special_object(self.evaluator, 'ASYNC_GENERATOR_TYPE') - yield SpecialMethodFilter(self, self.builtin_methods, gen_obj) - for filter in gen_obj.get_filters(search_global): - yield filter - - def py__bool__(self): - return True - - def py__class__(self): - gen_obj = compiled.get_special_object(self.evaluator, 'ASYNC_GENERATOR_TYPE') - return gen_obj.py__class__() - - @property - def name(self): - return compiled.CompiledContextName(self, 'asyncgenerator') - - -class AsyncGenerator(AsyncGeneratorMixin, Context): - """Handling of `yield` functions.""" - def __init__(self, evaluator, func_execution_context): - super(AsyncGenerator, self).__init__(evaluator, parent_context=evaluator.builtins_module) - self._func_execution_context = func_execution_context - - def py__aiter__(self): - return self._func_execution_context.get_yield_lazy_contexts(is_async=True) - - def __repr__(self): - return "<%s of %s>" % (type(self).__name__, self._func_execution_context) - - @has_builtin_methods class GeneratorMixin(object): array_type = None