|  | 
| 1 |  | -from typing import Protocol | 
|  | 1 | +from typing import Protocol, TypeVar | 
| 2 | 2 | 
 | 
| 3 |  | -__all__ = ("ArrayNamespace",) | 
|  | 3 | +from ._array import HasDType | 
|  | 4 | + | 
|  | 5 | +__all__ = ( | 
|  | 6 | +    "ArrayNamespace", | 
|  | 7 | +    # Data Type Functions | 
|  | 8 | +    "DoesAsType", | 
|  | 9 | +    "HasAsType", | 
|  | 10 | +) | 
|  | 11 | + | 
|  | 12 | +DTypeT = TypeVar("DTypeT") | 
|  | 13 | +ToDTypeT = TypeVar("ToDTypeT") | 
| 4 | 14 | 
 | 
| 5 | 15 | # =================================================================== | 
| 6 | 16 | # Creation Functions | 
|  | 
| 9 | 19 | 
 | 
| 10 | 20 | # =================================================================== | 
| 11 | 21 | # Data Type Functions | 
| 12 |  | -# TODO: astype, broadcast_arrays, broadcast_to, can_cast, finfo, iinfo, | 
|  | 22 | +# TODO: broadcast_arrays, broadcast_to, can_cast, finfo, iinfo, | 
| 13 | 23 | # result_type | 
| 14 | 24 | 
 | 
|  | 25 | + | 
|  | 26 | +class DoesAsType(Protocol): | 
|  | 27 | +    """Copies an array to a specified data type irrespective of Type Promotion Rules rules. | 
|  | 28 | +
 | 
|  | 29 | +    Note: | 
|  | 30 | +        Casting floating-point ``NaN`` and ``infinity`` values to integral data | 
|  | 31 | +        types is not specified and is implementation-dependent. | 
|  | 32 | +
 | 
|  | 33 | +    Note: | 
|  | 34 | +        When casting a boolean input array to a numeric data type, a value of | 
|  | 35 | +        `True` must cast to a numeric value equal to ``1``, and a value of | 
|  | 36 | +        `False` must cast to a numeric value equal to ``0``. | 
|  | 37 | +
 | 
|  | 38 | +        When casting a numeric input array to bool, a value of ``0`` must cast | 
|  | 39 | +        to `False`, and a non-zero value must cast to `True`. | 
|  | 40 | +
 | 
|  | 41 | +    Args: | 
|  | 42 | +        x: The array to cast. | 
|  | 43 | +        dtype: desired data type. | 
|  | 44 | +        copy: specifies whether to copy an array when the specified `dtype` | 
|  | 45 | +            matches the data type of the input array `x`. If `True`, a newly | 
|  | 46 | +            allocated array must always be returned. If `False` and the | 
|  | 47 | +            specified `dtype` matches the data type of the input array, the | 
|  | 48 | +            input array must be returned; otherwise, a newly allocated must be | 
|  | 49 | +            returned. Default: `True`. | 
|  | 50 | +
 | 
|  | 51 | +    """  # noqa: E501 | 
|  | 52 | + | 
|  | 53 | +    def __call__( | 
|  | 54 | +        self, x: HasDType[DTypeT], dtype: ToDTypeT, /, *, copy: bool = True | 
|  | 55 | +    ) -> HasDType[ToDTypeT]: ... | 
|  | 56 | + | 
|  | 57 | + | 
|  | 58 | +class HasAsType(Protocol): | 
|  | 59 | +    """Protocol for namespaces that have an ``astype`` function.""" | 
|  | 60 | + | 
|  | 61 | +    astype: DoesAsType | 
|  | 62 | + | 
|  | 63 | + | 
| 15 | 64 | # =================================================================== | 
| 16 | 65 | # Element-wise Functions | 
| 17 | 66 | # TODO: abs, acos, acosh, add, asin, asinh, atan, atan2, atanh, bitwise_and, | 
|  | 
| 55 | 104 | # Full Namespace | 
| 56 | 105 | 
 | 
| 57 | 106 | 
 | 
| 58 |  | -class ArrayNamespace(Protocol): | 
|  | 107 | +class ArrayNamespace( | 
|  | 108 | +    # Data Type Functions | 
|  | 109 | +    HasAsType, | 
|  | 110 | +    Protocol, | 
|  | 111 | +): | 
| 59 | 112 |     """Protocol for an Array API-compatible namespace.""" | 
0 commit comments