MCPcopy Create free account
hub / github.com/astral-sh/ruff / yield_outside_function

Method yield_outside_function

crates/ruff_python_parser/src/semantic_errors.rs:1160–1191  ·  view source on GitHub ↗

F704

(
        ctx: &Ctx,
        expr: &Expr,
        kind: YieldOutsideFunctionKind,
    )

Source from the content-addressed store, hash-verified

1158
1159 /// F704
1160 fn yield_outside_function<Ctx: SemanticSyntaxContext>(
1161 ctx: &Ctx,
1162 expr: &Expr,
1163 kind: YieldOutsideFunctionKind,
1164 ) {
1165 // We are intentionally not inspecting the async status of the scope for now to mimic F704.
1166 // await-outside-async is PLE1142 instead, so we'll end up emitting both syntax errors for
1167 // cases that trigger F704
1168
1169 if ctx.in_function_scope() {
1170 return;
1171 }
1172
1173 if kind.is_await() {
1174 // `await` is allowed at the top level of a Jupyter notebook.
1175 // See: https://ipython.readthedocs.io/en/stable/interactive/autoawait.html.
1176 if ctx.in_module_scope() && ctx.in_notebook() {
1177 return;
1178 }
1179 if ctx.in_await_allowed_context() {
1180 return;
1181 }
1182 } else if ctx.in_yield_allowed_context() {
1183 return;
1184 }
1185
1186 Self::add_error(
1187 ctx,
1188 SemanticSyntaxErrorKind::YieldOutsideFunction(kind),
1189 expr.range(),
1190 );
1191 }
1192
1193 /// Add a [`SemanticSyntaxErrorKind::ReboundComprehensionVariable`] if `expr` rebinds an
1194 /// iteration variable in `generators`.

Callers

nothing calls this directly

Calls 8

is_awaitMethod · 0.80
in_function_scopeMethod · 0.45
in_module_scopeMethod · 0.45
in_notebookMethod · 0.45
rangeMethod · 0.45

Tested by

no test coverage detected