python / python/mypy

[numpy] mypy does not raise `type-var` when `NDArray` with incompatible dtype is used as a generic.

Open
#17,228 0 comments 0 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

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

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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.