Skip to content

TYP: typing issue with np.object_ dtype iteration #30103

@acabal33uab

Description

@acabal33uab

Describe the issue:

np.ndarray.__iter__ type does not correctly type the return value when dtype is a union containing np.object_

Reproduce the code example:

## The iterated value is correctly typed with unions.

import numpy as np
from typing import Any, reveal_type


def str_union(a: np.ndarray[tuple[Any], np.dtype[np.int8 | np.str_]]) -> str:
    for i in a:
        reveal_type(i)
        if isinstance(i, str):
            return i
    return "a"


kk.py:7: note: Revealed type is "numpy.signedinteger[numpy._typing._nbit_base._8Bit] | numpy.str_"



## The iterated value is correctly typed to `Any` with np.object_.

import numpy as np
from typing import Any, reveal_type

def object(a: np.ndarray[tuple[Any], np.dtype[np.object_]]) -> np.ndarray:
    for i in a:
        reveal_type(i)
        if isinstance(i, np.ndarray):
            return i
    return a


kk.py:15: note: Revealed type is "Any"


## The iterated value is incorrectly typed to `int8 | np.object_` with `np.object_` union.
It should be typed to `Any` or `int8 | Any`.

def object_union(a: np.ndarray[tuple[Any], np.dtype[np.int8 | np.object_]]) -> np.ndarray:
    for i in a:
        reveal_type(i)
        if isinstance(i, np.ndarray):
            return i
    return a


kk.py:23: note: Revealed type is "numpy.signedinteger[numpy._typing._nbit_base._8Bit] | numpy.object_"

Error message:

kk.py:24: error: Subclass of "object_" and "ndarray[Any, Any]" cannot exist: "object_" is final  [unreachable]
            if isinstance(i, np.ndarray):
                          ^
kk.py:24: error: Subclass of "signedinteger[_8Bit]" and "ndarray[Any, Any]" cannot exist: would have incompatible method signatures 
[unreachable]
            if isinstance(i, np.ndarray):
                          ^
kk.py:25: error: Statement is unreachable  [unreachable]
                return i
                ^~~~~~~~

Python and NumPy Versions:

2.3.4
3.14.0 (main, Oct 27 2025, 12:54:43) [GCC 14.2.0]

Runtime Environment:

[{'numpy_version': '2.3.4',
'python': '3.14.0 (main, Oct 27 2025, 12:54:43) [GCC 14.2.0]',
'uname': uname_result(system='Linux', node='AV-COGNITION-14', release='6.14.0-15-generic', version='#15-Ubuntu SMP PREEMPT_DYNAMIC Sun Apr 6 15:05:05 UTC 2025', machine='x86_64')},
{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
'found': ['SSSE3',
'SSE41',
'POPCNT',
'SSE42',
'AVX',
'F16C',
'FMA3',
'AVX2'],
'not_found': ['AVX512F',
'AVX512CD',
'AVX512_KNL',
'AVX512_KNM',
'AVX512_SKX',
'AVX512_CLX',
'AVX512_CNL',
'AVX512_ICL',
'AVX512_SPR']}},
{'architecture': 'Haswell',
'filepath': '/home/dani/.virtualenvs/sc-api-DipHMMiG/lib/python3.14/site-packages/numpy.libs/libscipy_openblas64_-8fb3d286.so',
'internal_api': 'openblas',
'num_threads': 32,
'prefix': 'libscipy_openblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.30'}]

Context for the issue:

No response

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