diff --git a/crates/red_knot_python_semantic/src/types/infer.rs b/crates/red_knot_python_semantic/src/types/infer.rs index a00f9d4e8b..42b4297dfd 100644 --- a/crates/red_knot_python_semantic/src/types/infer.rs +++ b/crates/red_knot_python_semantic/src/types/infer.rs @@ -4568,10 +4568,14 @@ impl<'db> TypeInferenceBuilder<'db> { UnionType::from_elements(self.db, [param_type, Type::none(self.db)]) } KnownInstanceType::Union => match parameters { - ast::Expr::Tuple(t) => UnionType::from_elements( - self.db, - t.iter().map(|elt| self.infer_type_expression(elt)), - ), + ast::Expr::Tuple(t) => { + let union_ty = UnionType::from_elements( + 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), }, KnownInstanceType::TypeVar(_) => todo_type!(), diff --git a/crates/red_knot_workspace/resources/test/corpus/95_annotation_union.py b/crates/red_knot_workspace/resources/test/corpus/95_annotation_union.py new file mode 100644 index 0000000000..79be74b631 --- /dev/null +++ b/crates/red_knot_workspace/resources/test/corpus/95_annotation_union.py @@ -0,0 +1,3 @@ +from typing import Union + +x: Union[int, str] = 1