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

Method check_annotation

crates/ruff_python_parser/src/semantic_errors.rs:350–524  ·  view source on GitHub ↗
(stmt: &ast::Stmt, ctx: &Ctx)

Source from the content-addressed store, hash-verified

348 }
349
350 fn check_annotation<Ctx: SemanticSyntaxContext>(stmt: &ast::Stmt, ctx: &Ctx) {
351 match stmt {
352 Stmt::AnnAssign(ast::StmtAnnAssign {
353 target, annotation, ..
354 }) => {
355 if ctx.python_version() > PythonVersion::PY313 {
356 // test_ok valid_annotation_py313
357 // # parse_options: {"target-version": "3.13"}
358 // a: (x := 1)
359 // def outer():
360 // b: (yield 1)
361 // c: (yield from 1)
362 // async def outer():
363 // d: (await 1)
364
365 // test_err invalid_annotation_py314
366 // # parse_options: {"target-version": "3.14"}
367 // a: (x := 1)
368 // def outer():
369 // b: (yield 1)
370 // c: (yield from 1)
371 // async def outer():
372 // d: (await 1)
373 let mut visitor = InvalidExpressionVisitor {
374 position: InvalidExpressionPosition::TypeAnnotation,
375 ctx,
376 };
377 visitor.visit_expr(annotation);
378 }
379 if let Expr::Name(ast::ExprName { id, .. }) = target.as_ref() {
380 if let Some(global_stmt) = ctx.global(id.as_str()) {
381 let global_start = global_stmt.start();
382 if !ctx.in_module_scope() || target.start() < global_start {
383 Self::add_error(
384 ctx,
385 SemanticSyntaxErrorKind::AnnotatedGlobal(id.to_string()),
386 target.range(),
387 );
388 }
389 }
390 }
391 }
392 Stmt::FunctionDef(ast::StmtFunctionDef {
393 type_params,
394 parameters,
395 returns,
396 ..
397 }) => {
398 // test_ok valid_annotation_function_py313
399 // # parse_options: {"target-version": "3.13"}
400 // def f() -> (y := 3): ...
401 // def g(arg: (x := 1)): ...
402 // def outer():
403 // def i(x: (yield 1)): ...
404 // def k() -> (yield 1): ...
405 // def m(x: (yield from 1)): ...
406 // def o() -> (yield from 1): ...
407 // async def outer():

Callers

nothing calls this directly

Calls 13

python_versionMethod · 0.45
visit_exprMethod · 0.45
as_refMethod · 0.45
globalMethod · 0.45
as_strMethod · 0.45
startMethod · 0.45
in_module_scopeMethod · 0.45
to_stringMethod · 0.45
rangeMethod · 0.45
visit_type_paramsMethod · 0.45
iterMethod · 0.45

Tested by

no test coverage detected