You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The type of the conditional expression 1 if foo else 'x' is object, which is totally unexpected.
To be consistent with the rest of the language, the type should be one of these:
Union[int, str] (obvious)
Error, unless the type context is a supertype of both int and str. If one of the operands would have type None, the type will always be Optional[...] once mypy supports strict type checking of None values (None would be the only special case).
The rationale for case 2 is that mypy usually tries not to infer non-trivial union types, as these can result in confusing error messages far away from the source of the error. The error would be given if the only common non-union, non-Any supertype of the operands would be object. This is a somewhat arbitrary heuristic, but it would probably work well in most cases.
(These are also sometimes called ternary expressions. -- SEO Guido)
The text was updated successfully, but these errors were encountered:
The type of the conditional expression
1 if foo else 'x'
isobject
, which is totally unexpected.To be consistent with the rest of the language, the type should be one of these:
Union[int, str]
(obvious)int
andstr
. If one of the operands would have typeNone
, the type will always beOptional[...]
once mypy supports strict type checking ofNone
values (None
would be the only special case).The rationale for case 2 is that mypy usually tries not to infer non-trivial union types, as these can result in confusing error messages far away from the source of the error. The error would be given if the only common non-union, non-Any supertype of the operands would be
object
. This is a somewhat arbitrary heuristic, but it would probably work well in most cases.(These are also sometimes called ternary expressions. -- SEO Guido)
The text was updated successfully, but these errors were encountered: