From d1851c369c07cb7b0e54312a7c321e7685ab3be3 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Fri, 17 Jul 2020 12:05:59 +0200 Subject: [PATCH] Introduce py__next__ to have more clear way to use __next__ --- jedi/inference/base_value.py | 3 +++ jedi/inference/value/instance.py | 32 ++++++++++++++++---------------- jedi/inference/value/iterable.py | 5 ++++- jedi/plugins/stdlib.py | 2 +- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/jedi/inference/base_value.py b/jedi/inference/base_value.py index f8a8ff09..c516d463 100644 --- a/jedi/inference/base_value.py +++ b/jedi/inference/base_value.py @@ -175,6 +175,9 @@ class Value(HelperValueMixin): message="TypeError: '%s' object is not iterable" % self) return iter([]) + def py__next__(self, contextualized_node=None): + return self.py__iter__(contextualized_node) + def get_signatures(self): return [] diff --git a/jedi/inference/value/instance.py b/jedi/inference/value/instance.py index 5435c791..dab389d5 100644 --- a/jedi/inference/value/instance.py +++ b/jedi/inference/value/instance.py @@ -256,24 +256,24 @@ class _BaseTreeInstance(AbstractInstanceValue): def iterate(): for generator in self.execute_function_slots(iter_slot_names): - if generator.is_instance() and not generator.is_compiled(): - # `__next__` logic. - if self.inference_state.environment.version_info.major == 2: - name = u'next' - else: - name = u'__next__' - next_slot_names = generator.get_function_slot_names(name) - if next_slot_names: - yield LazyKnownValues( - generator.execute_function_slots(next_slot_names) - ) - else: - debug.warning('Instance has no __next__ function in %s.', generator) - else: - for lazy_value in generator.py__iter__(): - yield lazy_value + for lazy_value in generator.py__next__(contextualized_node): + yield lazy_value return iterate() + def py__next__(self, contextualized_node=None): + # `__next__` logic. + if self.inference_state.environment.version_info.major == 2: + name = u'next' + else: + name = u'__next__' + next_slot_names = self.get_function_slot_names(name) + if next_slot_names: + yield LazyKnownValues( + self.execute_function_slots(next_slot_names) + ) + else: + debug.warning('Instance has no __next__ function in %s.', self) + def py__call__(self, arguments): names = self.get_function_slot_names(u'__call__') if not names: diff --git a/jedi/inference/value/iterable.py b/jedi/inference/value/iterable.py index 43692c57..8a60d31d 100644 --- a/jedi/inference/value/iterable.py +++ b/jedi/inference/value/iterable.py @@ -23,6 +23,9 @@ from jedi.inference.value.dynamic_arrays import check_array_additions class IterableMixin(object): + def py__next__(self, contextualized_node=None): + return self.py__iter__(contextualized_node) + def py__stop_iteration_returns(self): return ValueSet([compiled.builtin_from_name(self.inference_state, u'None')]) @@ -64,7 +67,7 @@ class GeneratorBase(LazyAttributeOverwrite, IterableMixin): @publish_method('send') @publish_method('next', python_version_match=2) @publish_method('__next__', python_version_match=3) - def py__next__(self, arguments): + def _next(self, arguments): return ValueSet.from_sets(lazy_value.infer() for lazy_value in self.py__iter__()) def py__stop_iteration_returns(self): diff --git a/jedi/plugins/stdlib.py b/jedi/plugins/stdlib.py index 5e093e72..f24cadc9 100644 --- a/jedi/plugins/stdlib.py +++ b/jedi/plugins/stdlib.py @@ -267,7 +267,7 @@ class ReversedObject(AttributeOverwrite): @publish_method('next', python_version_match=2) @publish_method('__next__', python_version_match=3) - def py__next__(self, arguments): + def _next(self, arguments): return ValueSet.from_sets( lazy_value.infer() for lazy_value in self._iter_list )