Skip to content

False error in conditional expression returning union #6898

Closed

Description

  • Are you reporting a bug, or opening a feature request?

Bug

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.
from typing import TypeVar, Union


T = TypeVar("T")


def get_or_str(value: T) -> Union[str, T]:
    return value


def f(b: bool) -> Union[str, bool]:
    return "" if b else get_or_str(True)
  • What is the actual behavior/output?

a.py:12: error: Argument 1 to "get_or_str" has incompatible type "bool"; expected "str"

  • What is the behavior/output you expect?

No error.

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?

0.710+dev.5f08ccf029aa3046b15e1afc60743ba692e4758d (from master)

  • What are the mypy flags you are using? (For example --strict-optional)

None

Activity

  1. ilevkivskyi commented on May 26, 2019

    @ilevkivskyi
    Member

    The tuple example is a separate (relatively simple, but old) issue. The original example is just another case (among dozens) where single-bin inference would help.

    I think we should really do this.

  2. utkarshgupta137 commented on Jun 8, 2021

    @utkarshgupta137

    I'm getting `Argument 1 to "func" has incompatible type "Union[str, float]"; expected "str" [arg-type]'. Is this related as well? Am I doing something wrong?

  3. added a commit that references this issue on Jun 13, 2025
    2ba79cb
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