Skip to content

Embed the dtype-like types in the scalar types #138

Description

@jorenham

Similar to the NEP 50 embeddings that are powering the _numtype.CastsWith* type alias, it should be possible to create an dynamically specializing dtype-like alias. This unifies all 24 individual _numtype.ToDType* as a single generic type, without loss of specificity.


The idea is to define a type-check only method in each scalar-type, e.g in int8:

@type_check_only
def __dtype_like__(self, like: int8, /) -> tuple[
    type[int8 | ct.c_int8] | HasDType[int8] | L["int8", "i1", ...],
    Self,
]: ...

The return-types of must not overlap, and overloads cannot be used, and don't annotate like with Self.


In _numtype:

@type_check_only
class CanDTypeLike[
    +DTypeLikeT,
    +OutT: np.generic,
    -LikeT: np.generic = np.generic,
](Protocol):
    def __dtype_like__(self, like: LikeT, /) -> tuple[DTypeLikeT, OutT]: ...

And I'm hoping it can be used like this

def scalar[
    DTypeLikeT, 
    OutT: (bool_, int8, uint8, int16, ...),
    LikeT: np.generic = np.generic,
](
    dtype: _DTypeLikeT,
    *,
    like: LikeT | None = None
    __out: CanDTypeLike[DTypeLikeT, OutT, LikeT] = ...,  # type-check only
) -> OutT: ...

The LikeT is optional, but can be used to dynamically constrain OutT. It can also be provided manually, which is less verbose than having to re-define the entire "typevar with constraints".

It's not certain whether this will work in practice, as it requires pretty powerful constraint solving. I guess there's only one way to find out 🤷🏻

Activity

  1. changed the title [-]Embed the dtype-likes in the dtype types[/-] [+]Embed the possible dtype-likes in the scalar types[/+] on Feb 14, 2025
  2. changed the title [-]Embed the possible dtype-likes in the scalar types[/-] [+]embed the possible dtype-likes in the scalar types[/+] on Mar 20, 2025
  3. changed the title [-]embed the possible dtype-likes in the scalar types[/-] [+]Embed the possible dtype-likes in the scalar types[/+] on Mar 21, 2025
  4. pinned this issue on Apr 16, 2025
  5. changed the title [-]Embed the possible dtype-likes in the scalar types[/-] [+]Embed the dtype-like types in the scalar types[/+] on Apr 16, 2025
  6. unpinned this issue on Apr 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions