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

Method check_annotation

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

Source from the content-addressed store, hash-verified

366 }
367
368 fn check_annotation<Ctx: SemanticSyntaxContext>(stmt: &ast::Stmt, ctx: &Ctx) {
369 match stmt {
370 Stmt::AnnAssign(ast::StmtAnnAssign {
371 target, annotation, ..
372 }) => {
373 if ctx.python_version() > PythonVersion::PY313 {
374 // test_ok valid_annotation_py313
375 // # parse_options: {"target-version": "3.13"}
376 // a: (x := 1)
377 // def outer():
378 // b: (yield 1)
379 // c: (yield from 1)
380 // async def outer():
381 // d: (await 1)
382
383 // test_err invalid_annotation_py314
384 // # parse_options: {"target-version": "3.14"}
385 // a: (x := 1)
386 // def outer():
387 // b: (yield 1)
388 // c: (yield from 1)
389 // async def outer():
390 // d: (await 1)
391 let mut visitor = InvalidExpressionVisitor {
392 position: InvalidExpressionPosition::TypeAnnotation,
393 ctx,
394 };
395 visitor.visit_expr(annotation);
396 }
397 if let Expr::Name(ast::ExprName { id, .. }) = target.as_ref() {
398 if let Some(global_stmt) = ctx.global(id.as_str()) {
399 let global_start = global_stmt.start();
400 if !ctx.in_module_scope() || target.start() < global_start {
401 Self::add_error(
402 ctx,
403 SemanticSyntaxErrorKind::AnnotatedGlobal(id.to_string()),
404 target.range(),
405 );
406 }
407 }
408 }
409 }
410 Stmt::FunctionDef(ast::StmtFunctionDef {
411 type_params,
412 parameters,
413 returns,
414 ..
415 }) => {
416 // test_ok valid_annotation_function_py313
417 // # parse_options: {"target-version": "3.13"}
418 // def f() -> (y := 3): ...
419 // def g(arg: (x := 1)): ...
420 // def outer():
421 // def i(x: (yield 1)): ...
422 // def k() -> (yield 1): ...
423 // def m(x: (yield from 1)): ...
424 // def o() -> (yield from 1): ...
425 // 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