Skip to content
New issue

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

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

Already on GitHub? # to your account

Conditional expression with branches of type None and T should be Optional[T], not object #1820

Closed
gvanrossum opened this issue Jul 7, 2016 · 2 comments

Comments

@gvanrossum
Copy link
Member

Consider:

def f(flag; bool) -> Optional[int]:
  a = (None if flag else 0)
  return a

When run with --strict-optional I get the error

error: Incompatible return value type (got "object", expected "Optional[int]")

It looks like the unification code is not understanding what's going on and falling back on object.

@ddfisher
Copy link
Collaborator

Related to #1863. This is due to mypy's reluctance to make Union types.

@gnprice
Copy link
Collaborator

gnprice commented Aug 1, 2016

As discussed under #1863, we should go ahead and become willing to make Union types when one of the types to be combined is None. We might go back later and become less reluctant to make Union types more broadly, but there's more detailed discussion to be had there (see #1863.)

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

3 participants