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:1067–1098  ·  view source on GitHub ↗

F704

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

Source from the content-addressed store, hash-verified

1065
1066 /// F704
1067 fn yield_outside_function<Ctx: SemanticSyntaxContext>(
1068 ctx: &Ctx,
1069 expr: &Expr,
1070 kind: YieldOutsideFunctionKind,
1071 ) {
1072 // We are intentionally not inspecting the async status of the scope for now to mimic F704.
1073 // await-outside-async is PLE1142 instead, so we'll end up emitting both syntax errors for
1074 // cases that trigger F704
1075
1076 if ctx.in_function_scope() {
1077 return;
1078 }
1079
1080 if kind.is_await() {
1081 // `await` is allowed at the top level of a Jupyter notebook.
1082 // See: https://ipython.readthedocs.io/en/stable/interactive/autoawait.html.
1083 if ctx.in_module_scope() && ctx.in_notebook() {
1084 return;
1085 }
1086 if ctx.in_await_allowed_context() {
1087 return;
1088 }
1089 } else if ctx.in_yield_allowed_context() {
1090 return;
1091 }
1092
1093 Self::add_error(
1094 ctx,
1095 SemanticSyntaxErrorKind::YieldOutsideFunction(kind),
1096 expr.range(),
1097 );
1098 }
1099
1100 /// Add a [`SemanticSyntaxErrorKind::ReboundComprehensionVariable`] if `expr` rebinds an
1101 /// 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