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)
a can be either
_ArrayLike when a is a scalar or object
- or
_ArrayLike[T] when a is a collection
- 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.