Skip to content

Conversation

@BvB93
Copy link
Member

@BvB93 BvB93 commented May 31, 2021

This PR adds dtype-support to the annotations of the following 3 generic/ndarray methods:

  • astype
  • view
  • getfield

* `astype`
* `getfield`
* `view`
Comment on lines +1864 to +1881
@overload
def astype(
self,
dtype: _DTypeLike[_ScalarType],
order: _OrderKACF = ...,
casting: _Casting = ...,
subok: bool = ...,
copy: bool = ...,
) -> NDArray[_ScalarType]: ...
@overload
def astype(
self,
dtype: DTypeLike,
order: _OrderKACF = ...,
casting: _Casting = ...,
subok: bool = ...,
copy: bool = ...,
) -> NDArray[Any]: ...
Copy link
Member Author

@BvB93 BvB93 May 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dtype-support is, for now, limited to dtype-likes that can be parametrized w.r.t. np.generic.
For all others the dtype will be set to Any.

Comment on lines +1891 to +1896
@overload
def view(
self,
dtype: DTypeLike,
type: Type[_NdArraySubClass],
) -> _NdArraySubClass: ...
Copy link
Member Author

@BvB93 BvB93 May 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately we can't express the return of an ndarray-subclass with a specific dtype
(e.g. ndarray[float64] -> matrix[int64]). This would require higher-kinded TypeVars, which we don't have.

@charris charris merged commit 21838b4 into numpy:main Jun 6, 2021
@charris
Copy link
Member

charris commented Jun 6, 2021

Thanks Bas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants