Skip to content

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

Description

@gvanrossum

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.

Activity

  1. ddfisher commented on Jul 21, 2016

    @ddfisher
    Collaborator

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

  2. added this to the milestone on Jul 21, 2016
  3. gnprice commented on Aug 1, 2016

    @gnprice
    Collaborator

    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.)

  4. added a commit that references this issue on Aug 8, 2016
    d83aece
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions