[syntax-errors] Check annotations in annotated assignments (#17283)

Summary
--

This PR extends the checks in #17101 and #17282 to annotated assignments
after Python 3.13.

Currently stacked on #17282 to include `await`.

Test Plan
--

New inline tests. These are simpler than the other cases because there's
no place to put generics.
This commit is contained in:
Brent Westbrook 2025-04-08 08:56:25 -04:00 committed by GitHub
parent 127a45622f
commit 0891689d2f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 433 additions and 0 deletions

View file

@ -119,6 +119,32 @@ impl SemanticSyntaxChecker {
fn check_annotation<Ctx: SemanticSyntaxContext>(stmt: &ast::Stmt, ctx: &Ctx) {
match stmt {
Stmt::AnnAssign(ast::StmtAnnAssign { annotation, .. }) => {
if ctx.python_version() > PythonVersion::PY313 {
// test_ok valid_annotation_py313
// # parse_options: {"target-version": "3.13"}
// a: (x := 1)
// def outer():
// b: (yield 1)
// c: (yield from 1)
// async def outer():
// d: (await 1)
// test_err invalid_annotation_py314
// # parse_options: {"target-version": "3.14"}
// a: (x := 1)
// def outer():
// b: (yield 1)
// c: (yield from 1)
// async def outer():
// d: (await 1)
let mut visitor = InvalidExpressionVisitor {
position: InvalidExpressionPosition::TypeAnnotation,
ctx,
};
visitor.visit_expr(annotation);
}
}
Stmt::FunctionDef(ast::StmtFunctionDef {
type_params,
parameters,