mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 22:31:47 +00:00
[red-knot] Understanding type[Union[A, B]]
(#14858)
Co-authored-by: Alex Waygood <alex.waygood@gmail.com>
This commit is contained in:
parent
cf260aef2b
commit
3865fb6641
2 changed files with 63 additions and 1 deletions
|
@ -4684,6 +4684,33 @@ impl<'db> TypeInferenceBuilder<'db> {
|
|||
);
|
||||
Type::Unknown
|
||||
}
|
||||
ast::Expr::Subscript(ast::ExprSubscript {
|
||||
value,
|
||||
slice: parameters,
|
||||
..
|
||||
}) => {
|
||||
let parameters_ty = match self.infer_expression(value) {
|
||||
Type::KnownInstance(KnownInstanceType::Union) => match &**parameters {
|
||||
ast::Expr::Tuple(tuple) => {
|
||||
let ty = UnionType::from_elements(
|
||||
self.db,
|
||||
tuple
|
||||
.iter()
|
||||
.map(|element| self.infer_subclass_of_type_expression(element)),
|
||||
);
|
||||
self.store_expression_type(parameters, ty);
|
||||
ty
|
||||
}
|
||||
_ => self.infer_subclass_of_type_expression(parameters),
|
||||
},
|
||||
_ => {
|
||||
self.infer_type_expression(parameters);
|
||||
todo_type!("unsupported nested subscript in type[X]")
|
||||
}
|
||||
};
|
||||
self.store_expression_type(slice, parameters_ty);
|
||||
parameters_ty
|
||||
}
|
||||
// TODO: subscripts, etc.
|
||||
_ => {
|
||||
self.infer_type_expression(slice);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue