Skip to content

Infer Union for ternary expressions #1511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
smallnamespace opened this issue May 9, 2016 · 2 comments
Closed

Infer Union for ternary expressions #1511

smallnamespace opened this issue May 9, 2016 · 2 comments

Comments

@smallnamespace
Copy link

smallnamespace commented May 9, 2016

Ternary expressions currently infer to type object, when they should at least infer a Union of both branch's types (if not more, in the case where the predicate is known to be constant).

from typing import Union

u = 0 if True else ''  # type: Union[int, str]

currently results in

error: Incompatible types in assignment (expression has type "object", variable has type "Union[int, str]")

@gvanrossum
Copy link
Member

Indeed! This is a duplicate of #622.

@smallnamespace
Copy link
Author

Thanks! I'd searched for existing issues, but wasn't aware that Python calls them conditional expressions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants