[syntax-errors] Type parameter defaults before Python 3.13 (#16447)

Summary
--

Detects the presence of a [PEP 696] type parameter default before Python
3.13.

Test Plan
--

New inline parser tests for type aliases, generic functions and generic
classes.

[PEP 696]: https://peps.python.org/pep-0696/#grammar-changes
This commit is contained in:
Brent Westbrook 2025-03-04 11:53:38 -05:00 committed by GitHub
parent c8a06a9be8
commit e7b93f93ef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 514 additions and 4 deletions

View file

@ -3243,6 +3243,7 @@ impl<'src> Parser<'src> {
None
};
let equal_token_start = self.node_start();
let default = if self.eat(TokenKind::Equal) {
if self.at_expr() {
// test_err type_param_type_var_invalid_default_expr
@ -3268,6 +3269,26 @@ impl<'src> Parser<'src> {
None
};
// test_ok type_param_default_py313
// # parse_options: {"target-version": "3.13"}
// type X[T = int] = int
// def f[T = int](): ...
// class C[T = int](): ...
// test_err type_param_default_py312
// # parse_options: {"target-version": "3.12"}
// type X[T = int] = int
// def f[T = int](): ...
// class C[T = int](): ...
// class D[S, T = int, U = uint](): ...
if default.is_some() {
self.add_unsupported_syntax_error(
UnsupportedSyntaxErrorKind::TypeParamDefault,
self.node_range(equal_token_start),
);
}
ast::TypeParam::TypeVar(ast::TypeParamTypeVar {
range: self.node_range(start),
name,