bad error message when attempting to assign to generic type
Nobody has claimed this yet.
- 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'.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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