Skip to content

ENH: flatiter is missing _SupportsArray in type definition #17114

@bashtage

Description

@bashtage

Reproducing code example:

flat fails typing when used with a ufunc because flatiter is not array-like enough

Run mypy on

import numpy as np

a = np.empty((2,2))
np.any(a.flat)

I think the solution is to have flatiter inherit _SupportsArray since it does expose __array__.

Error message:

demo.py:4: error: No overload variant of "any" matches argument type "flatiter[ndarray]"
demo.py:4: note: Possible overload variants:
demo.py:4: note:     def any(a: Union[bool, int, float, complex, _SupportsArray, Sequence[Any]], axis: None = ..., out: Optional[ndarray] = ..., keepdims: Literal[False] = ...) -> bool_
demo.py:4: note:     def any(a: Union[bool, int, float, complex, _SupportsArray, Sequence[Any]], axis: Union[int, Sequence[int], None] = ..., out: Optional[ndarray] = ..., keepdims: bool = ...) -> Union[bool_, ndarray]

Numpy/Python version information:

1.20.0.dev0+986e533 3.8.3 (default, Jul  2 2020, 16:21:59)
[GCC 7.3.0]

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