From c88cf08b9fe8d6639dfbdddaaae7d5b0f2dd7669 Mon Sep 17 00:00:00 2001 From: Christian Bourjau Date: Wed, 12 Jul 2023 11:02:51 +0200 Subject: [PATCH 1/2] Allow lazy implementations to raise from intrinsically eager functions --- src/array_api_stubs/_draft/array_object.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/array_api_stubs/_draft/array_object.py b/src/array_api_stubs/_draft/array_object.py index cf6adcf3c..1349fca7f 100644 --- a/src/array_api_stubs/_draft/array_object.py +++ b/src/array_api_stubs/_draft/array_object.py @@ -239,6 +239,10 @@ def __bool__(self: array, /) -> bool: For complex floating-point operands, special cases must be handled as if the operation is implemented as the logical AND of ``bool(real(self))`` and ``bool(imag(self))``. + **Lazy implemenations** + + Lazy implementations of the API standard must raise a ``ValueError`` if they are unable eagerly compute the requested value. + .. versionchanged:: 2022.12 Added boolean and complex data type support. """ @@ -422,6 +426,10 @@ def __float__(self: array, /) -> float: - If ``self`` is ``True``, the result is ``1``. - If ``self`` is ``False``, the result is ``0``. + **Lazy implementations** + + Lazy implementations of the API standard must raise a ``ValueError`` if they are unable eagerly compute the requested value. + .. versionchanged:: 2022.12 Added boolean and complex data type support. """ @@ -537,6 +545,13 @@ def __index__(self: array, /) -> int: ------- out: int a Python ``int`` object representing the single element of the array instance. + + Notes + ----- + + **Lazy implementations** + + Lazy implementations of the API standard must raise a ``ValueError`` if they are unable eagerly compute the requested value. """ def __int__(self: array, /) -> int: @@ -575,6 +590,13 @@ def __int__(self: array, /) -> int: - If ``self`` is either ``+infinity`` or ``-infinity``, raise ``OverflowError``. - If ``self`` is ``NaN``, raise ``ValueError``. + Notes + ----- + + **Lazy implementations** + + Lazy implementations of the API standard must raise a ``ValueError`` if they are unable eagerly compute the requested value. + .. versionchanged:: 2022.12 Added boolean and complex data type support. """ From b68979f8041635eb8ac985bbc9598664e2e7299c Mon Sep 17 00:00:00 2001 From: Christian Bourjau Date: Mon, 17 Jul 2023 11:26:32 +0200 Subject: [PATCH 2/2] Implement comments from code review --- src/array_api_stubs/_draft/array_object.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/array_api_stubs/_draft/array_object.py b/src/array_api_stubs/_draft/array_object.py index 1349fca7f..32a71d31c 100644 --- a/src/array_api_stubs/_draft/array_object.py +++ b/src/array_api_stubs/_draft/array_object.py @@ -239,9 +239,9 @@ def __bool__(self: array, /) -> bool: For complex floating-point operands, special cases must be handled as if the operation is implemented as the logical AND of ``bool(real(self))`` and ``bool(imag(self))``. - **Lazy implemenations** + **Lazy implementations** - Lazy implementations of the API standard must raise a ``ValueError`` if they are unable eagerly compute the requested value. + The Python language requires the return value to be of type ``bool``. Lazy implementations are therefore not able to return any kind of lazy/delayed object here and should raise a ``ValueError`` instead. .. versionchanged:: 2022.12 Added boolean and complex data type support. @@ -278,6 +278,10 @@ def __complex__(self: array, /) -> complex: - If ``self`` is ``-infinity``, the result is ``-infinity + 0j``. - If ``self`` is a finite number, the result is ``self + 0j``. + **Lazy implementations** + + The Python language requires the return value to be of type ``complex``. Lazy implementations are therefore not able to return any kind of lazy/delayed object here and should raise a ``ValueError`` instead. + .. versionadded:: 2022.12 """ @@ -428,7 +432,7 @@ def __float__(self: array, /) -> float: **Lazy implementations** - Lazy implementations of the API standard must raise a ``ValueError`` if they are unable eagerly compute the requested value. + The Python language requires the return value to be of type ``float``. Lazy implementations are therefore not able to return any kind of lazy/delayed object here and should raise a ``ValueError`` instead. .. versionchanged:: 2022.12 Added boolean and complex data type support. @@ -551,7 +555,7 @@ def __index__(self: array, /) -> int: **Lazy implementations** - Lazy implementations of the API standard must raise a ``ValueError`` if they are unable eagerly compute the requested value. + The Python language requires the return value to be of type ``int``. Lazy implementations are therefore not able to return any kind of lazy/delayed object here and should raise a ``ValueError`` instead. """ def __int__(self: array, /) -> int: @@ -595,7 +599,7 @@ def __int__(self: array, /) -> int: **Lazy implementations** - Lazy implementations of the API standard must raise a ``ValueError`` if they are unable eagerly compute the requested value. + The Python language requires the return value to be of type ``int``. Lazy implementations are therefore not able to return any kind of lazy/delayed object here and should raise a ``ValueError`` instead. .. versionchanged:: 2022.12 Added boolean and complex data type support.