Try to use yield from instead of yield, if possible

This commit is contained in:
Dave Halter
2020-07-19 13:34:58 +02:00
parent 5bc174bf8d
commit 9957565b37
21 changed files with 49 additions and 97 deletions

View File

@@ -198,8 +198,7 @@ def filter_follow_imports(names, follow_builtin_imports=False):
if found_builtin:
yield name
else:
for new_name in new_names:
yield new_name
yield from new_names
else:
yield name

View File

@@ -71,5 +71,4 @@ class MixedModuleContext(ModuleContext):
)
for mixed_object in self.mixed_values:
for filter in mixed_object.get_filters(until_position, origin_scope):
yield filter
yield from mixed_object.get_filters(until_position, origin_scope)

View File

@@ -192,8 +192,7 @@ class TreeArguments(AbstractArguments):
elif star_count == 2:
arrays = self.context.infer_node(el)
for dct in arrays:
for key, values in _star_star_dict(self.context, dct, el, funcdef):
yield key, values
yield from _star_star_dict(self.context, dct, el, funcdef)
else:
if el.type == 'argument':
c = el.children
@@ -216,8 +215,7 @@ class TreeArguments(AbstractArguments):
# Reordering arguments is necessary, because star args sometimes appear
# after named argument, but in the actual order it's prepended.
for named_arg in named_args:
yield named_arg
yield from named_args
def _as_tree_tuple_objects(self):
for star_count, argument in unpack_arglist(self.argument_node):
@@ -318,8 +316,7 @@ def _iterate_star_args(context, array, input_node, funcdef=None):
except AttributeError:
pass
else:
for lazy_value in iter_():
yield lazy_value
yield from iter_()
def _star_star_dict(context, array, input_node, funcdef):

View File

@@ -55,14 +55,12 @@ class HelperValueMixin(object):
def _get_value_filters(self, name_or_str):
origin_scope = name_or_str if isinstance(name_or_str, Name) else None
for f in self.get_filters(origin_scope=origin_scope):
yield f
yield from self.get_filters(origin_scope=origin_scope)
# This covers the case where a stub files are incomplete.
if self.is_stub():
from jedi.inference.gradual.conversion import convert_values
for c in convert_values(ValueSet({self})):
for f in c.get_filters():
yield f
yield from c.get_filters()
def goto(self, name_or_str, name_context=None, analysis_errors=True):
from jedi.inference import finder
@@ -439,8 +437,7 @@ class ValueSet(object):
return self._from_frozen_set(self._set & other._set)
def __iter__(self):
for element in self._set:
yield element
return iter(self._set)
def __bool__(self):
return bool(self._set)

View File

@@ -184,8 +184,7 @@ class CompiledValue(Value):
def py__iter__(self, contextualized_node=None):
if not self.access_handle.has_iter():
for x in super().py__iter__(contextualized_node):
yield x
yield from super().py__iter__(contextualized_node)
access_path_list = self.access_handle.py__iter__list()
if access_path_list is None:
@@ -220,10 +219,8 @@ class CompiledValue(Value):
continue
else:
bltn_obj = builtin_from_name(self.inference_state, name)
for result in self.inference_state.execute(bltn_obj, params):
yield result
for type_ in docstrings.infer_return_types(self):
yield type_
yield from self.inference_state.execute(bltn_obj, params)
yield from docstrings.infer_return_types(self)
def get_safe_value(self, default=_sentinel):
try:

View File

@@ -482,10 +482,10 @@ def get_global_filters(context, until_position, origin_scope):
from jedi.inference.value.function import BaseFunctionExecutionContext
while context is not None:
# Names in methods cannot be resolved within the class.
for filter in context.get_filters(
until_position=until_position,
origin_scope=origin_scope):
yield filter
yield from context.get_filters(
until_position=until_position,
origin_scope=origin_scope
)
if isinstance(context, (BaseFunctionExecutionContext, ModuleContext)):
# The position should be reset if the current scope is a function.
until_position = None

View File

@@ -95,8 +95,7 @@ def _search_return_in_numpydocstr(docstr):
# Return names are optional and if so the type is in the name
if not r_type:
r_type = r_name
for type_ in _expand_typestr(r_type):
yield type_
yield from _expand_typestr(r_type)
def _expand_typestr(type_str):
@@ -295,9 +294,7 @@ def infer_return_types(function_value):
if match:
yield _strip_rst_role(match.group(1))
# Check for numpy style return hint
for type_ in _search_return_in_numpydocstr(code):
yield type_
yield from _search_return_in_numpydocstr(code)
for type_str in search_return_in_docstr(function_value.py__doc__()):
for value in _infer_for_statement_string(function_value.get_root_context(), type_str):
yield value
yield from _infer_for_statement_string(function_value.get_root_context(), type_str)

View File

@@ -215,12 +215,10 @@ def _check_name_for_execution(inference_state, context, compare_node, name, trai
for name, trailer in potential_nodes:
if value_node.start_pos < name.start_pos < value_node.end_pos:
random_context = execution_context.create_context(name)
iterator = _check_name_for_execution(
yield from _check_name_for_execution(
inference_state,
random_context,
compare_node,
name,
trailer
)
for arguments in iterator:
yield arguments

View File

@@ -323,9 +323,7 @@ class _OverwriteMeta(type):
class _AttributeOverwriteMixin(object):
def get_filters(self, *args, **kwargs):
yield SpecialMethodFilter(self, self.overwritten_methods, self._wrapped_value)
for filter in self._wrapped_value.get_filters(*args, **kwargs):
yield filter
yield from self._wrapped_value.get_filters(*args, **kwargs)
class LazyAttributeOverwrite(_AttributeOverwriteMixin, LazyValueWrapper,

View File

@@ -25,8 +25,7 @@ class _BoundTypeVarName(AbstractNameDefinition):
# Replace any with the constraints if they are there.
from jedi.inference.gradual.typing import AnyClass
if isinstance(value, AnyClass):
for constraint in self._type_var.constraints:
yield constraint
yield from self._type_var.constraints
else:
yield value
return ValueSet(iter_())
@@ -73,8 +72,7 @@ class _AnnotatedClassContext(ClassContext):
filters = super().get_filters(
*args, **kwargs
)
for f in filters:
yield f
yield from filters
# The type vars can only be looked up if it's a global search and
# not a direct lookup on the class.

View File

@@ -135,8 +135,7 @@ def _python_to_stub_names(names, fallback_to_python=False):
if converted:
converted_names = converted.goto(name.get_public_name())
if converted_names:
for n in converted_names:
yield n
yield from converted_names
continue
if fallback_to_python:
# This is the part where if we haven't found anything, just return

View File

@@ -43,11 +43,8 @@ class StubModuleValue(ModuleValue):
filters = super().get_filters(origin_scope)
next(filters, None) # Ignore the first filter and replace it with our own
stub_filters = self._get_stub_filters(origin_scope=origin_scope)
for f in stub_filters:
yield f
for f in filters:
yield f
yield from stub_filters
yield from filters
def _as_context(self):
return StubModuleContext(self)
@@ -66,8 +63,7 @@ class TypingModuleWrapper(StubModuleValue):
f = next(filters, None)
assert f is not None
yield TypingModuleFilterWrapper(f)
for f in filters:
yield f
yield from filters
def _as_context(self):
return TypingModuleContext(self)
@@ -77,8 +73,7 @@ class TypingModuleContext(ModuleContext):
def get_filters(self, *args, **kwargs):
filters = super().get_filters(*args, **kwargs)
yield TypingModuleFilterWrapper(next(filters, None))
for f in filters:
yield f
yield from filters
class StubFilter(ParserTreeFilter):

View File

@@ -38,8 +38,7 @@ def _resolve_names(definition_names, avoid_names=()):
yield name
if name.api_type == 'module':
for n in _resolve_names(name.goto(), definition_names):
yield n
yield from _resolve_names(name.goto(), definition_names)
def _dictionarize(names):
@@ -90,8 +89,7 @@ def _add_names_in_same_context(context, string_name):
names = set(filter_.get(string_name))
if not names:
break
for name in names:
yield name
yield from names
ordered = sorted(names, key=lambda x: x.start_pos)
until_position = ordered[0].start_pos
@@ -109,8 +107,7 @@ def _find_global_variables(names, search_name):
for global_name in method().get(search_name):
yield global_name
c = module_context.create_context(global_name.tree_name)
for n in _add_names_in_same_context(c, global_name.string_name):
yield n
yield from _add_names_in_same_context(c, global_name.string_name)
def find_references(module_context, tree_name, only_in_module=False):

View File

@@ -96,8 +96,7 @@ def process_params(param_names, star_count=3): # default means both * and **
if is_big_annoying_library(param_names[0].parent_context):
# At first this feature can look innocent, but it does a lot of
# type inference in some cases, so we just ditch it.
for p in param_names:
yield p
yield from param_names
return
used_names = set()

View File

@@ -166,8 +166,7 @@ def _get_paths_from_buildout_script(inference_state, buildout_script_path):
string_names=None,
code_lines=get_cached_code_lines(inference_state.grammar, str(buildout_script_path)),
).as_context()
for path in check_sys_path_modifications(module_context):
yield path
yield from check_sys_path_modifications(module_context)
def _get_parent_dir_with_file(path: Path, filename):

View File

@@ -155,14 +155,12 @@ class _DynamicArrayAdditions(HelperValueMixin):
except StopIteration:
pass
else:
for lazy in lazy_value.infer().iterate():
yield lazy
yield from lazy_value.infer().iterate()
from jedi.inference.arguments import TreeArguments
if isinstance(arguments, TreeArguments):
additions = _internal_check_array_additions(arguments.context, self._instance)
for addition in additions:
yield addition
yield from additions
def iterate(self, contextualized_node=None, is_async=False):
return self.py__iter__(contextualized_node)
@@ -189,8 +187,7 @@ class _Modification(ValueWrapper):
class DictModification(_Modification):
def py__iter__(self, contextualized_node=None):
for lazy_context in self._wrapped_value.py__iter__(contextualized_node):
yield lazy_context
yield from self._wrapped_value.py__iter__(contextualized_node)
yield self._contextualized_key
def get_key_values(self):
@@ -199,6 +196,5 @@ class DictModification(_Modification):
class ListModification(_Modification):
def py__iter__(self, contextualized_node=None):
for lazy_context in self._wrapped_value.py__iter__(contextualized_node):
yield lazy_context
yield from self._wrapped_value.py__iter__(contextualized_node)
yield LazyKnownValues(self._assigned_values)

View File

@@ -59,8 +59,7 @@ class FunctionMixin(object):
def get_filters(self, origin_scope=None):
cls = self.py__class__()
for instance in cls.execute_with_values():
for filter in instance.get_filters(origin_scope=origin_scope):
yield filter
yield from instance.get_filters(origin_scope=origin_scope)
def py__get__(self, instance, class_value):
from jedi.inference.value.instance import BoundMethod
@@ -256,8 +255,7 @@ class BaseFunctionExecutionContext(ValueContext, TreeContextMixin):
node = yield_expr.children[1]
if node.type == 'yield_arg': # It must be a yield from.
cn = ContextualizedNode(self, node.children[1])
for lazy_value in cn.infer().iterate(cn):
yield lazy_value
yield from cn.infer().iterate(cn)
else:
yield LazyTreeValue(self, node)
@@ -296,8 +294,7 @@ class BaseFunctionExecutionContext(ValueContext, TreeContextMixin):
if for_stmt is None:
# No for_stmt, just normal yields.
for yield_ in yields:
for result in self._get_yield_lazy_value(yield_):
yield result
yield from self._get_yield_lazy_value(yield_)
else:
input_node = for_stmt.get_testlist()
cn = ContextualizedNode(self, input_node)
@@ -307,8 +304,7 @@ class BaseFunctionExecutionContext(ValueContext, TreeContextMixin):
dct = {str(for_stmt.children[1].value): lazy_value.infer()}
with self.predefine_names(for_stmt, dct):
for yield_in_same_for_stmt in yields:
for result in self._get_yield_lazy_value(yield_in_same_for_stmt):
yield result
yield from self._get_yield_lazy_value(yield_in_same_for_stmt)
def merge_yield_values(self, is_async=False):
return ValueSet.from_sets(

View File

@@ -255,8 +255,7 @@ class _BaseTreeInstance(AbstractInstanceValue):
def iterate():
for generator in self.execute_function_slots(iter_slot_names):
for lazy_value in generator.py__next__(contextualized_node):
yield lazy_value
yield from generator.py__next__(contextualized_node)
return iterate()
def py__next__(self, contextualized_node=None):
@@ -526,8 +525,7 @@ class LazyInstanceClassName(NameWrapper):
@iterator_to_value_set
def infer(self):
for result_value in self._wrapped_name.infer():
for c in result_value.py__get__(self._instance, self._instance.py__class__()):
yield c
yield from result_value.py__get__(self._instance, self._instance.py__class__())
def get_signatures(self):
return self.infer().get_signatures()
@@ -616,5 +614,4 @@ class InstanceArguments(TreeArgumentsWrapper):
def unpack(self, func=None):
yield None, LazyKnownValue(self.instance)
for values in self._wrapped_arguments.unpack(func):
yield values
yield from self._wrapped_arguments.unpack(func)

View File

@@ -153,8 +153,7 @@ class ComprehensionMixin(object):
)
with context.predefine_names(comp_for, dct):
try:
for result in self._nested(comp_fors[1:], context):
yield result
yield from self._nested(comp_fors[1:], context)
except IndexError:
iterated = context.infer_node(self._entry_node)
if self.array_type == 'dict':
@@ -166,8 +165,7 @@ class ComprehensionMixin(object):
@to_list
def _iterate(self):
comp_fors = tuple(get_sync_comp_fors(self._sync_comp_for_node))
for result in self._nested(comp_fors):
yield result
yield from self._nested(comp_fors)
def py__iter__(self, contextualized_node=None):
for set_ in self._iterate():
@@ -358,8 +356,7 @@ class SequenceLiteralValue(Sequence):
yield LazyKnownValue(Slice(self._defining_context, None, None, None))
else:
yield LazyTreeValue(self._defining_context, node)
for addition in check_array_additions(self._defining_context, self):
yield addition
yield from check_array_additions(self._defining_context, self)
def py__len__(self):
# This function is not really used often. It's more of a try.
@@ -566,8 +563,7 @@ class MergedArray(Sequence):
def py__iter__(self, contextualized_node=None):
for array in self._arrays:
for lazy_value in array.py__iter__():
yield lazy_value
yield from array.py__iter__()
def py__simple_getitem__(self, index):
return ValueSet.from_sets(lazy_value.infer() for lazy_value in self.py__iter__())

View File

@@ -68,8 +68,7 @@ class ClassName(TreeNameDefinition):
for result_value in inferred:
if self._apply_decorators:
for c in result_value.py__get__(instance=None, class_value=self._class_value):
yield c
yield from result_value.py__get__(instance=None, class_value=self._class_value)
else:
yield result_value

View File

@@ -67,8 +67,7 @@ class ModuleMixin(SubModuleDictMixin):
)
yield DictFilter(self.sub_modules_dict())
yield DictFilter(self._module_attributes_dict())
for star_filter in self.iter_star_filters():
yield star_filter
yield from self.iter_star_filters()
def py__class__(self):
c, = values_from_qualified_names(self.inference_state, 'types', 'ModuleType')