Mypy errors on code that's trivial syntactic transformation of code that it does not error on
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
Mypy reports a type error for the eta expansion of code that it does not error on. See below for an example.
To Reproduce
from __future__ import annotations
from collections.abc import Callable
from typing import Generic, NamedTuple, TypeVar, TypeVarTuple
DType = TypeVar("DType")
DType2 = TypeVar("DType2")
Shape = TypeVarTuple("Shape")
Shape2 = TypeVarTuple("Shape2")
Dim1 = TypeVar("Dim1")
SeqLen = TypeVar("SeqLen")
BatchLen = TypeVar("BatchLen")
class ndarray(Generic[*Shape, DType]): ... # noqa: N801
def vmap(
fun: Callable[[ndarray[*Shape, DType]], ndarray[*Shape2, DType2]],
) -> Callable[[ndarray[Dim1, *Shape, DType]], ndarray[Dim1, *Shape2, DType2]]:
raise NotImplementedError
def fn(tkns: ndarray[BatchLen, SeqLen, float]):
_ = vmap(call)(tkns)
_ = vmap(lambda x: call(x))(tkns)
def call(input_: ndarray[SeqLen, float]) -> ndarray[SeqLen, float]:
raise NotImplementedError
Expected Behavior
Eta expansion (going from something like vmap(fn) to vmap(lambda x: fn(x)) should always be behavior preserving and also type preserving.
Actual Behavior
Instead Mypy approves of the first call but not the second.
main.py:26: error: Argument 1 to "call" has incompatible type "ndarray[*Shape, DType]"; expected "ndarray[Never, float]" [arg-type]
Your Environment
This is using the default setting on https://mypy-play.net/?mypy=latest&python=3.12 with Mypy at 1.11.2.
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
Reproduce the example from main.py using the default mypy settings with Python 3.12, as demonstrated on mypy-play with mypy 1.11.2. Trace how mypy infers the direct call versus the eta-expanded lambda, and add or update coverage so both equivalent forms receive consistent type-checking results.
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
- 39/100