Skip to content

BUG: Sum of ndarray subclassess incorrectly typed as np.ndarray #20072

Closed
@rdbisme

Description

@rdbisme

Describe the issue:

In my code I'm using a slightly customized numpy subclass. When I sum two numpy arrays viewed as this class, the result has a different type. I'd expect the sum to be of the same type as the two addends.

Reproduce the code example:

import numpy as np


class State(np.ndarray):
    pass


a: State = np.random.rand(10, 5).view(State)
b: State = np.random.rand(10, 5).view(State)

reveal_type(a)
reveal_type(b)
reveal_type(a + b)

Error message:

test-mypy-numpy.py:12: note: Revealed type is "test-mypy-numpy.State"
test-mypy-numpy.py:13: note: Revealed type is "test-mypy-numpy.State"
test-mypy-numpy.py:14: note: Revealed type is "numpy.ndarray[Any, numpy.dtype[numpy.bool_]]"

NumPy/Python version information:

Python 3.9.5
Numpy: 1.21.2
Mypy: 0.910

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