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

Method check_annotation

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

Source from the content-addressed store, hash-verified

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