Type variables in Callables don't unify
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Issue type: Bug
Python version: 3.8
mypy version: 0.760
mypy flags: whatever the defaults are on https://mypy-play.net/
I discovered this issue when playing around with a monad-y Either thing (and here's it working in Typescript)
Here is (I think) a minimal repro:
from typing import Callable, TypeVar, Generic
def foo() -> int:
return 1
T = TypeVar('T')
bar: Callable[[], T] = foo
main.py:7: error: Incompatible types in assignment (expression has type "Callable[[], int]", variable has type "Callable[[], T]")
I would expect that in the type declaration of bar, T would unify with int, but clearly it does not.
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 with the minimal reproduction in main.py and run it using the mypy version and default flags described in the issue. Compare the Callable assignment result with the expected TypeVar unification, then trace the relevant type-inference behavior in mypy. Done means the reproduction is accepted with the intended inferred type and regression coverage is added.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100