From c47f5ca68cf0bf84af4735620b36f1acb1647da6 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Fri, 1 Sep 2017 18:38:19 +0200 Subject: [PATCH] Fix issues with yield. --- jedi/evaluate/context.py | 11 ++++++++++- jedi/evaluate/iterable.py | 1 - jedi/evaluate/representation.py | 5 +++++ test/completion/generators.py | 19 +++++++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/jedi/evaluate/context.py b/jedi/evaluate/context.py index 0c91af2e..a232be6b 100644 --- a/jedi/evaluate/context.py +++ b/jedi/evaluate/context.py @@ -5,7 +5,10 @@ from jedi.parser_utils import clean_scope_docstring, get_doc_with_call_signature class Context(object): - api_type = None + """ + Should be defined, otherwise the API returns empty types. + """ + """ To be defined by subclasses. """ @@ -16,6 +19,12 @@ class Context(object): self.evaluator = evaluator self.parent_context = parent_context + @property + def api_type(self): + # By default just lower name of the class. Can and should be + # overwritten. + return self.__class__.__name__.lower() + def get_root_context(self): context = self while True: diff --git a/jedi/evaluate/iterable.py b/jedi/evaluate/iterable.py index 6030d6e7..2b0c11fc 100644 --- a/jedi/evaluate/iterable.py +++ b/jedi/evaluate/iterable.py @@ -165,7 +165,6 @@ class GeneratorMixin(object): class Generator(GeneratorMixin, context.Context): """Handling of `yield` functions.""" - def __init__(self, evaluator, func_execution_context): super(Generator, self).__init__(evaluator, parent_context=evaluator.BUILTINS) self._func_execution_context = func_execution_context diff --git a/jedi/evaluate/representation.py b/jedi/evaluate/representation.py index b49d3536..55ae7751 100644 --- a/jedi/evaluate/representation.py +++ b/jedi/evaluate/representation.py @@ -342,6 +342,11 @@ class FunctionExecutionContext(context.TreeContext): return types def _eval_yield(self, yield_expr): + if yield_expr.type == 'keyword': + # `yield` just yields None. + yield context.LazyKnownContext(compiled.create(self.evaluator, None)) + return + node = yield_expr.children[1] if node.type == 'yield_arg': # It must be a yield from. cn = ContextualizedNode(self, node.children[1]) diff --git a/test/completion/generators.py b/test/completion/generators.py index 5e08a350..2327b185 100644 --- a/test/completion/generators.py +++ b/test/completion/generators.py @@ -179,6 +179,25 @@ gen().send() #? gen()() +# ----------------- +# empty yield +# ----------------- + +def x(): + yield + +#? None +next(x()) +#? gen() +x() + +def x(): + for i in range(3): + yield + +#? None +next(x()) + # ----------------- # yield in expression # -----------------