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

Lines changed: 8 additions & 4 deletions
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!(),
Lines changed: 3 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy