Skip to content

False error in conditional expression returning union #6898

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
ghost opened this issue May 24, 2019 · 5 comments · Fixed by #19249
Closed

False error in conditional expression returning union #6898

ghost opened this issue May 24, 2019 · 5 comments · Fixed by #19249
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-ternary-expression a if b else c topic-type-variables

Comments

@ghost
Copy link

ghost commented May 24, 2019

  • 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

@ghost
Copy link
Author

ghost commented May 24, 2019

There is a similar error when trying to return a Sequence using tuples of different lengths:

def f(b: bool) -> Sequence[int]:
    return (1,) if b else (2, 3)

a.py:4: error: Incompatible return value type (got "object", expected "Sequence[int]")

@ilevkivskyi ilevkivskyi added bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-type-variables labels May 26, 2019
@ilevkivskyi
Copy link
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.

@ghost
Copy link
Author

ghost commented Jul 12, 2019

A similar problem:

from typing import Union

def f(a: Union[str, int], b: Union[str, int]) -> Union[str, float]:
    return a if isinstance(a, str) else b if isinstance(b, str) else a / b

a.py:4: error: Incompatible return value type (got "object", expected "Union[str, float]")

@ghost
Copy link
Author

ghost commented Aug 14, 2019

Likely the same issue:

from typing import Callable, Optional, TypeVar

T = TypeVar("T")

def f(
    a: Callable[[], T],
    b: Callable[[], T]
) -> T:
    return a()

op: Optional[int] = f(lambda: 1, lambda: None)

a.py:11: error: Incompatible return value type (got "None", expected "int")

Works bare T instead of callables.

@utkarshgupta137
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-ternary-expression a if b else c topic-type-variables
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants