-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Closed
Description
The type of expression such as 1 if foo else 'x' is object (join of int and str). This is surprising and if such an expression is used in a union type context, the result is often not what's expected. Example:
from typing import Union
def f(x: Union[int, str]) -> None: ...
c = 1
f(1 if c else 'x') # Incompatible argument type "object"This is also arguably inconsistent, as the or and and operators produce union types. For example, the type of (1+2) or 'x' is Union[int, str].
Maybe conditional expressions should also produce unions? An alternative idea is to only produce union types if the type context is a union type, but this would be somewhat ad-hoc.
yan12125 and christianbundy