python / python/mypy

bad error message when attempting to assign to generic type

Open
#11,536 2 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the Python example in the linked mypy-play case and compare the current diagnostics for foo() and bar(). Trace the type-checking path that produces the return-value errors. Done means the diagnostic communicates the generic type issue more accurately, with coverage for both examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.