[red-knot] Avoid panic for generic type aliases (#14312)

## Summary

This avoids a panic inside `TypeInferenceBuilder::infer_type_parameters`
when encountering generic type aliases:
```py
type ListOrSet[T] = list[T] | set[T]
```

To fix this properly, we would have to treat type aliases as being their own
annotation scope [1]. The left hand side is a definition for the type parameter
`T` which is being used in the special annotation scope on the right hand side.
Similar to how it works for generic functions and classes.

[1] https://docs.python.org/3/reference/compound_stmts.html#generic-type-aliases


closes #14307

## Test Plan

Added new example to the corpus.
This commit is contained in:
David Peter 2024-11-13 16:01:15 +01:00 committed by GitHub
parent 5fcf0afff4
commit 0eb36e4345
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 4 additions and 4 deletions

View file

@ -1796,14 +1796,13 @@ impl<'db> TypeInferenceBuilder<'db> {
let ast::StmtTypeAlias {
range: _,
name,
type_params,
type_params: _,
value,
} = type_alias_statement;
self.infer_expression(value);
self.infer_expression(name);
if let Some(type_params) = type_params {
self.infer_type_parameters(type_params);
}
// TODO: properly handle generic type aliases, which need their own annotation scope
}
fn infer_for_statement(&mut self, for_statement: &ast::StmtFor) {