Skip to content
This repository was archived by the owner on Jun 10, 2020. It is now read-only.
This repository was archived by the owner on Jun 10, 2020. It is now read-only.

What to do with array_like variables #37

@johanvergeer

Description

@johanvergeer

I'm currently working on core.numeric and I'm running into an interesting issue.

A lot of methods have an array_like input parameter and an ndarray as the output parameter.

For now I created a TypeVar like the following:

_ArrayLike = TypeVar("_ArrayLike", str, int, float, bool, object, ByteString, Iterable, Container, ndarray)

As an example I will use the zeros_like function

def zeros_like(
        a: _ArrayLike,
        dtype: Optional[dtype] = None,
        order: str = 'K',
        subok: bool = True,
        shape: Optional[Union[int, Sequence[int]]] = None) -> ndarray[T]: ...

This function returns an instance of ndarray containing the same type as a, or it can be overridden by dtype.

This brought me to the following conclusions (please correct me if I'm wrong)

  1. a can be either
    • _ArrayLike when a is a scalar or object
    • or _ArrayLike[T] when a is a collection
  2. The return value type can be
    • ndarray[_ArrayLike] when a is a scalar or object and dtype is None
    • ndarray[T] when a is _ArrayLike[T] and dtype is None
    • ndarray[dtype] when dtype is not None

I'm wondering what the type annotations of a and the return value should look like.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions