Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Add aliases for commonly used dtype-like objects #18236

Merged
merged 3 commits into from
Jan 29, 2021

Conversation

BvB93
Copy link
Member

@BvB93 BvB93 commented Jan 26, 2021

This PR adds (invariant) aliases for commonly used dtype-like objects, similar to what #18050 has done for array-likes.

Note that the aliases introduced herein are currently unused (beyond their introduction).
This will change once we start adding dtype-support to the ufuncs, something that's on
the to-do list for after the ufunc-based ndarray magic methods have been wrapped up (xref #18128 and #18228).

Secondly, it performs a small follow-up on #13578, i.e. objects with the .dtype attribute are
now only considered dtype-like if aforementioned attribute returns a np.dtype instance.

Examples

Example usage of the new _DTypeLike<x> aliases in annotating a simplified version of np.bitwise_or.

from __future__ import annotations
from typing import Any, overload

import numpy as np
import numpy.typing as npt

@overload
def bitwise_or(
    __x1: npt._ArrayLikeBool_co, 
    __x2: npt._ArrayLikeBool_co,
    *, dtype: npt._DTypeLikeBool | None = ...,
) -> np.ndarray[Any, np.dtype[np.bool_]]: ...
@overload
def bitwise_or(
    __x1: npt._ArrayLikeUInt_co, 
    __x2: npt._ArrayLikeUInt_co, 
    *, dtype: npt._DTypeLikeUInt | None = ...,
) -> np.ndarray[Any, np.dtype[np.unsignedinteger[Any]]: ...
@overload
def bitwise_or(
    __x1: npt._ArrayLikeInt_co, 
    __x2: npt._ArrayLikeInt_co, 
    *, dtype: npt._DTypeLikeInt | None = ...,
) -> np.ndarray[Any, np.dtype[np.signedinteger[Any]]: ...

@BvB93 BvB93 changed the title ENH: Added aliases for commonly used dtype-like objects ENH: Add aliases for commonly used dtype-like objects Jan 26, 2021
_HalfCodes,
_SingleCodes,
_DoubleCodes,
_LongDoubleCodes,
Copy link
Member Author

@BvB93 BvB93 Jan 26, 2021

Choose a reason for hiding this comment

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

A typical example of the newly introduced _DTypeLike<X> unions and their components:

  • A np.generic subclass.
  • A builtin counterpart to aforementioned np.generic subclass.
  • A np.dtype instance.
  • An object with the dtype attribute.
  • Various string literals with relevant character codes ("f", "float", "f4", etc.).

Copy link
Member

@rgommers rgommers left a comment

Choose a reason for hiding this comment

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

Thanks @BvB93, this looks good to me modulo the one typo.

numpy/typing/_dtype_like.py Outdated Show resolved Hide resolved
@rgommers rgommers added this to the 1.21.0 release milestone Jan 29, 2021
@rgommers rgommers merged commit 9772bea into numpy:master Jan 29, 2021
@rgommers
Copy link
Member

Merged, thanks!

@BvB93 BvB93 deleted the dtype-like branch January 29, 2021 12:07
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.

None yet

2 participants