Skip to content

Commit f684b6f

Browse files
authored
[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](#14499 (comment)) on #14499. ## Test Plan New corpus test
1 parent 47f39ed commit f684b6f

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

crates/red_knot_python_semantic/src/types/infer.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -4568,10 +4568,14 @@ impl<'db> TypeInferenceBuilder<'db> {
45684568
UnionType::from_elements(self.db, [param_type, Type::none(self.db)])
45694569
}
45704570
KnownInstanceType::Union => match parameters {
4571-
ast::Expr::Tuple(t) => UnionType::from_elements(
4572-
self.db,
4573-
t.iter().map(|elt| self.infer_type_expression(elt)),
4574-
),
4571+
ast::Expr::Tuple(t) => {
4572+
let union_ty = UnionType::from_elements(
4573+
self.db,
4574+
t.iter().map(|elt| self.infer_type_expression(elt)),
4575+
);
4576+
self.store_expression_type(parameters, union_ty);
4577+
union_ty
4578+
}
45754579
_ => self.infer_type_expression(parameters),
45764580
},
45774581
KnownInstanceType::TypeVar(_) => todo_type!(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from typing import Union
2+
3+
x: Union[int, str] = 1

0 commit comments

Comments
 (0)