我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用ast.Yield()。
def _is_for_yield(self, node: ast.For) -> bool: if node.orelse: return False if not isinstance(node.target, ast.Name): return False body = node.body if len(body) != 1: return False expr = body[0] if not isinstance(expr, ast.Expr): return False yield_ = expr.value if not isinstance(yield_, ast.Yield): return False name = yield_.value if not isinstance(name, ast.Name): return False if name.id != node.target.id: return False return True
def check_for_b901(self, node): xs = list(node.body) has_yield = False return_node = None while xs: x = xs.pop() if isinstance(x, (ast.AsyncFunctionDef, ast.FunctionDef)): continue elif isinstance(x, (ast.Yield, ast.YieldFrom)): has_yield = True elif isinstance(x, ast.Return) and x.value is not None: return_node = x if has_yield and return_node is not None: self.errors.append( B901(return_node.lineno, return_node.col_offset) ) break xs.extend(ast.iter_child_nodes(x))
def name(self) -> str: return 'Yield to "yield from"'
def test_yield(self): self.expr(ast.Yield(ast.Name("x", ast.Store())), "must have Load") self.expr(ast.YieldFrom(ast.Name("x", ast.Store())), "must have Load")
def _isyield(el): return isinstance(el, (ast.Yield, ast.YieldFrom))
def is_a_generator(function): return has_nodes(function.tree, (ast.Yield, ast.YieldFrom))
def infer(node, context, solver, from_call=False): """Infer the type of a given AST node""" if isinstance(node, ast.Num): return infer_numeric(node, solver) elif isinstance(node, ast.Str): return solver.z3_types.string elif (sys.version_info[0] >= 3 and sys.version_info[1] >= 6 and (isinstance(node, ast.FormattedValue) or isinstance(node, ast.JoinedStr))): # Formatted strings were introduced in Python 3.6 return solver.z3_types.string elif isinstance(node, ast.Bytes): return solver.z3_types.bytes elif isinstance(node, ast.List): return infer_list(node, context, solver) elif isinstance(node, ast.Dict): return infer_dict(node, context, solver) elif isinstance(node, ast.Tuple): return infer_tuple(node, context, solver) elif isinstance(node, ast.NameConstant): return infer_name_constant(node, solver) elif isinstance(node, ast.Set): return infer_set(node, context, solver) elif isinstance(node, ast.BinOp): return infer_binary_operation(node, context, solver) elif isinstance(node, ast.BoolOp): return infer_boolean_operation(node, context, solver) elif isinstance(node, ast.UnaryOp): return infer_unary_operation(node, context, solver) elif isinstance(node, ast.IfExp): return infer_if_expression(node, context, solver) elif isinstance(node, ast.Subscript): return infer_subscript(node, context, solver) elif sys.version_info[0] >= 3 and sys.version_info[1] >= 5 and isinstance(node, ast.Await): # Await and Async were introduced in Python 3.5 return infer(node.value, context, solver) elif isinstance(node, ast.Yield): return infer(node.value, context, solver) elif isinstance(node, ast.Compare): return infer_compare(node, context, solver) elif isinstance(node, ast.Name): return infer_name(node, context) elif isinstance(node, ast.ListComp): return infer_sequence_comprehension(node, solver.z3_types.list, context, solver) elif isinstance(node, ast.SetComp): return infer_sequence_comprehension(node, solver.z3_types.set, context, solver) elif isinstance(node, ast.DictComp): return infer_dict_comprehension(node, context, solver) elif isinstance(node, ast.Call): return infer_func_call(node, context, solver) elif isinstance(node, ast.Attribute): return infer_attribute(node, context, from_call, solver) elif isinstance(node, ast.Lambda): return _infer_lambda(node, context, solver) raise NotImplementedError("Inference for expression {} is not implemented yet.".format(type(node).__name__))