Skip to content

TYP: shape field is typed Any instead of `tuple[int, ...] #29830

@thierry-martinez

Description

@thierry-martinez

Describe the issue:

With numpy >=2.3, given a numpy-array m, m.shape field is typed tuple[Any, ...], whereas, with numpy <2.3, it was typed tuple[int, ...].

Reproduce the code example:

# The following code fails to type-check with `mypy --strict`:
# error: Returning Any from function declared to return "int"  [no-any-return]

import numpy as np
import numpy.typing as npt
def get_bitlength(m: npt.NDArray[np.float64]) -> int:
     return m.shape[0].bit_length()

Error message:

`mypy --strict`:
error: Returning Any from function declared to return "int"  [no-any-return]

Python and NumPy Versions:

Numpy 2.3.3
Python 3.13.0 | packaged by conda-forge | (main, Nov 27 2024, 19:18:26) [Clang 18.1.8 ]
mypy 1.18.2

Runtime Environment:

[{'numpy_version': '2.3.3',
'python': '3.13.0 | packaged by conda-forge | (main, Nov 27 2024, 19:18:26) '
'[Clang 18.1.8 ]',
'uname': uname_result(system='Darwin', node='MAC-02008476', release='24.6.0', version='Darwin Kernel Version 24.6.0: Mon Aug 11 21:16:52 PDT 2025; root:xnu-11417.140.69.701.11~1/RELEASE_ARM64_T8112', machine='arm64')},
{'simd_extensions': {'baseline': ['NEON', 'NEON_FP16', 'NEON_VFPV4', 'ASIMD'],
'found': ['ASIMDHP'],
'not_found': ['ASIMDFHM']}}]

Context for the issue:

We circumvent the bug by using a local variable:

def get_bitlength(m: npt.NDArray[np.float64]) -> int:
     bitlength: int = m.shape[0].bit_length()
     return bitlength

We prefer using a local variable over a type cast, since mypy will be able to catch a type error if any.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions