Skip to content

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

Open
@DetachHead

Description

@DetachHead
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions