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

bad error message when attempting to assign to generic type #11536

Open
DetachHead opened this issue Nov 13, 2021 · 2 comments
Open

bad error message when attempting to assign to generic type #11536

DetachHead opened this issue Nov 13, 2021 · 2 comments
Labels
bug mypy got something wrong

Comments

@DetachHead
Copy link
Contributor

from typing import TypeVar

T = TypeVar("T", int, str)


def foo() -> T:
    return 1 # error: Incompatible return value type (got "int", expected "str")  [return-value]

def bar() -> T:
    return "" # error: Incompatible return value type (got "str", expected "int")  [return-value]

https://mypy-play.net/?mypy=latest&python=3.10&gist=442fc0f6332aa02d4dc509460b91536a

i think the error message should be something like:

Incompatible return value type (got "int", expected "T")

or maybe something more descriptive, like how typescript puts it:

'T' could be instantiated with an arbitrary type which could be unrelated to 'number'.

https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABMOcA8AVAfACgJQBciGiA3gLABQiNiATgKZQh1ICMVAvkA

@DetachHead DetachHead added the bug mypy got something wrong label Nov 13, 2021
@erictraut
Copy link

I think this is a side effect of the way mypy analyzes generic functions that are parameterized by one or more constrained TypeVars. It analyzes the function multiple times, once for each combination of possible constraints.

For example, you'll receive two error messages here:

T = TypeVar("T", int, str)

def foo() -> T:
    return 1.0
# error: Incompatible return value type (got "float", expected "int")
# error: Incompatible return value type (got "float", expected "str")

And four error messages here:

S = TypeVar("S", int, str)
T = TypeVar("T", int, str)

def foo() -> Union[T, S]:
    return 1.0
# error: Incompatible return value type (got "float", expected "int")
# error: Incompatible return value type (got "float", expected "Union[int, str]")
# error: Incompatible return value type (got "float", expected "Union[str, int]")
# error: Incompatible return value type (got "float", expected "str")

I don't disagree with your point that the error messages are confusing. I'm just saying that improving the errors would likely require a significant change to mypy's semantic analyzer logic.

@JelleZijlstra
Copy link
Member

We could probably improve the error messages without totally changing how we check this code, just by setting some state that the error message generation code reads.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants