Skip to content

ENH: Add an ndarray typing protocol #19752

Closed
@anuppari

Description

@anuppari

I'd like to define a protocol that extends the numpy N-D Array. I can't create an abstract base class for unrelated reasons. I'd like to do something like the following:

from typing import Protocol
import numpy.typing import NDArrayProtocol  # Currently doesn't exist

class NdArrayWithMethod(NDArrayProtocol, Protocol):
    def method(self, input) -> None: pass

class ImpementsProtocol(np.ndarray):
    def method(self, input):
        ...

class AlsoImpementsProtocol(np.ndarray):
    def method(self, input):
        ...

def operates on protocol(in: NdArrayWithMethod):
   ...

The protocol cannot subclass ndarray directly, so I need to use something like NDArrayProtocol. Is there an ndarray protocol exposed somewhere?

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