[numpy] mypy does not raise `type-var` when `NDArray` with incompatible dtype is used as a generic.
Open
Nobody has claimed this yet.
bug
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
To Reproduce
import numpy as np
from numpy.typing import NDArray
class A: ...
def foo() -> NDArray[A]: # ✅ raises type-var (true positive)
return np.array([A()])
def bar() -> list[NDArray[A]]: # ❌ does not raise type-var (false negative)
return [np.array([A()])]
Expected Behavior
mypy should raise error: Type argument "A" of "NDArray" must be a subtype of "generic" [type-var] in both instances.
Your Environment
- Mypy version used: 1.10
- numpy version: 1.26.4
- Mypy configuration options:
plugins = ["numpy.typing.mypy_plugin"] - Python version used: 3.11.7
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 issue with the foo and bar examples using mypy 1.10, NumPy 1.26.4, Python 3.11.7, and numpy.typing.mypy_plugin enabled. Compare the diagnostics for direct and nested NDArray[A] usage; done means mypy raises the specified type-var error for both cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100