mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 06:11:43 +00:00
[red-knot] Fix: Infer type for typing.Union[..] tuple expression (#14510)
## Summary Fixes a panic related to sub-expressions of `typing.Union` where we fail to store a type for the `int, str` tuple-expression in code like this: ``` x: Union[int, str] = 1 ``` relates to [my comment](https://github.com/astral-sh/ruff/pull/14499#discussion_r1851794467) on #14499. ## Test Plan New corpus test
This commit is contained in:
parent
47f39ed1a0
commit
f684b6fff4
2 changed files with 11 additions and 4 deletions
|
@ -4568,10 +4568,14 @@ impl<'db> TypeInferenceBuilder<'db> {
|
||||||
UnionType::from_elements(self.db, [param_type, Type::none(self.db)])
|
UnionType::from_elements(self.db, [param_type, Type::none(self.db)])
|
||||||
}
|
}
|
||||||
KnownInstanceType::Union => match parameters {
|
KnownInstanceType::Union => match parameters {
|
||||||
ast::Expr::Tuple(t) => UnionType::from_elements(
|
ast::Expr::Tuple(t) => {
|
||||||
self.db,
|
let union_ty = UnionType::from_elements(
|
||||||
t.iter().map(|elt| self.infer_type_expression(elt)),
|
self.db,
|
||||||
),
|
t.iter().map(|elt| self.infer_type_expression(elt)),
|
||||||
|
);
|
||||||
|
self.store_expression_type(parameters, union_ty);
|
||||||
|
union_ty
|
||||||
|
}
|
||||||
_ => self.infer_type_expression(parameters),
|
_ => self.infer_type_expression(parameters),
|
||||||
},
|
},
|
||||||
KnownInstanceType::TypeVar(_) => todo_type!(),
|
KnownInstanceType::TypeVar(_) => todo_type!(),
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
x: Union[int, str] = 1
|
Loading…
Add table
Add a link
Reference in a new issue