Skip to content
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

Having a function that takes Optional[T] as an input and returns T (A type var) works incorrectly if T is an union #17383

Open
superosku opened this issue Jun 14, 2024 · 1 comment
Labels
bug mypy got something wrong topic-join-v-union Using join vs. using unions

Comments

@superosku
Copy link
Contributor

Bug Report

Having a function that takes Optional[T] as an input and returns T (A type var) works incorrectly if T is an union.

To Reproduce

from typing import TypeVar


class A:
    pass


class B:
    pass


T = TypeVar("T")


def make_non_optional(value: T | None) -> T:
    if value is None:
        raise ValueError("Value is None")
    return value


def make_non_optimal_simple_example() -> A:
    my_value: A | None = None
    return make_non_optional(my_value)  # This works as expected


def make_non_optimal_complex_example_broken() -> A | B:
    my_value: A | B | None = None
    return make_non_optional(my_value)  # error: Incompatible return value type (got "object", expected "A | B")  [return-value]

Expected Behavior

When calling make_non_optional(my_value) with A | B | None the returned type should be A | B

Actual Behavior

It has object type as the return type

Your Environment

Mypy version used:

 $ mypy --version
mypy 1.10.0 (compiled: yes)

Python version used:

 $ python --version
Python 3.12.2

Mypy command-line flags: none

Mypy configuration options from mypy.ini (and other config files): none

@superosku superosku added the bug mypy got something wrong label Jun 14, 2024
@erictraut
Copy link

This behavior stems from the fact that mypy's constraint solver uses a join operator rather than unions. The join of A and B is object, which explains the error message you're seeing here.

@JelleZijlstra JelleZijlstra added the topic-join-v-union Using join vs. using unions label Jun 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-join-v-union Using join vs. using unions
Projects
None yet
Development

No branches or pull requests

3 participants