python / python/mypy

mypy infers `numpy.float64` instead of `Any` for `numpy.ndarray.__iter__` in numpy 2.2.2

Open
#18,566 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-overloads
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

It looks like mypy doesn't correctly handle the overloading of numpy.ndarray.__iter__. In the example below mypy infers numpy.float64 for ai while the correct type is Any.

See also: https://github.com/numpy/numpy/issues/28245

To Reproduce

import numpy as np
import numpy.typing as npt

a: npt.NDArray[np.float64] = np.ones([2, 2])

for _, ai in enumerate(a):
    reveal_type(ai)

Expected Behavior

Any

Actual Behavior

test.py:7: note: Revealed type is "numpy.float64"

Your Environment

Both pyright 1.1.392 and basedpyright 1.25.0 behave as expected.

  • Mypy version used: mypy 1.14.1 (compiled: yes)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12.8 (main, Jan 22 2025, 21:56:05) [GCC 11.4.0]
  • Numpy version used: 2.2.2

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

Start with the test.py reproduction and run it under mypy 1.14.1 using the versions listed in the report. Trace how mypy handles the overloaded numpy.ndarray.iter result, then confirm the fix by checking that reveal_type(ai) reports Any for the provided example.

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
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.