From f436248fa9d316ce3a9b81d4fc66fcc3b8be632f Mon Sep 17 00:00:00 2001 From: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> Date: Thu, 30 Jul 2026 14:27:38 +0530 Subject: [PATCH 1/2] Ruff safe-fixes Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> --- array_api_strict/__init__.py | 90 ++++++++++++------- array_api_strict/_array_object.py | 8 +- array_api_strict/_creation_functions.py | 9 +- array_api_strict/_devices.py | 22 +++-- array_api_strict/_dtypes.py | 2 +- array_api_strict/_fft.py | 20 ++--- array_api_strict/_flags.py | 4 +- array_api_strict/_linalg.py | 22 +++-- array_api_strict/_searching_functions.py | 8 +- array_api_strict/_set_functions.py | 4 +- array_api_strict/tests/conftest.py | 5 +- array_api_strict/tests/test_array_object.py | 30 +++---- .../tests/test_creation_functions.py | 15 ++-- .../tests/test_elementwise_functions.py | 17 ++-- array_api_strict/tests/test_flags.py | 70 ++++++++++++--- array_api_strict/tests/test_linalg.py | 4 +- .../tests/test_manipulation_functions.py | 8 +- .../tests/test_searching_functions.py | 2 +- .../tests/test_statistical_functions.py | 11 +-- docs/conf.py | 3 +- 20 files changed, 224 insertions(+), 130 deletions(-) diff --git a/array_api_strict/__init__.py b/array_api_strict/__init__.py index a4b578e7..d1763a4e 100644 --- a/array_api_strict/__init__.py +++ b/array_api_strict/__init__.py @@ -28,13 +28,13 @@ __all__ += ["__array_api_version__"] -from ._constants import e, inf, nan, pi, newaxis +from ._constants import e, inf, nan, newaxis, pi -__all__ += ["e", "inf", "nan", "pi", "newaxis"] +__all__ += ["e", "inf", "nan", "newaxis", "pi"] from ._creation_functions import ( - asarray, arange, + asarray, empty, empty_like, eye, @@ -52,8 +52,8 @@ ) __all__ += [ - "asarray", "arange", + "asarray", "empty", "empty_like", "eye", @@ -77,8 +77,8 @@ broadcast_to, can_cast, finfo, - isdtype, iinfo, + isdtype, result_type, ) @@ -89,12 +89,17 @@ "broadcast_to", "can_cast", "finfo", - "isdtype", "iinfo", + "isdtype", "result_type", ] from ._dtypes import ( + bool, + complex64, + complex128, + float32, + float64, int8, int16, int32, @@ -103,14 +108,14 @@ uint16, uint32, uint64, - float32, - float64, - complex64, - complex128, - bool, ) __all__ += [ + "bool", + "complex64", + "complex128", + "float32", + "float64", "int8", "int16", "int32", @@ -119,11 +124,6 @@ "uint16", "uint32", "uint64", - "float32", - "float64", - "complex64", - "complex128", - "bool", ] from ._elementwise_functions import ( @@ -137,8 +137,8 @@ atan2, atanh, bitwise_and, - bitwise_left_shift, bitwise_invert, + bitwise_left_shift, bitwise_or, bitwise_right_shift, bitwise_xor, @@ -188,8 +188,8 @@ signbit, sin, sinh, - square, sqrt, + square, subtract, tan, tanh, @@ -207,8 +207,8 @@ "atan2", "atanh", "bitwise_and", - "bitwise_left_shift", "bitwise_invert", + "bitwise_left_shift", "bitwise_or", "bitwise_right_shift", "bitwise_xor", @@ -258,8 +258,8 @@ "signbit", "sin", "sinh", - "square", "sqrt", + "square", "subtract", "tan", "tanh", @@ -276,9 +276,9 @@ "__array_namespace_info__", ] -from ._linear_algebra_functions import matmul, tensordot, matrix_transpose, vecdot +from ._linear_algebra_functions import matmul, matrix_transpose, tensordot, vecdot -__all__ += ["matmul", "tensordot", "matrix_transpose", "vecdot"] +__all__ += ["matmul", "matrix_transpose", "tensordot", "vecdot"] from ._manipulation_functions import ( concat, @@ -297,44 +297,68 @@ __all__ += ["concat", "expand_dims", "flip", "moveaxis", "permute_dims", "repeat", "reshape", "roll", "squeeze", "stack", "tile", "unstack"] -from ._searching_functions import argmax, argmin, nonzero, count_nonzero, searchsorted, where +from ._searching_functions import ( + argmax, + argmin, + count_nonzero, + nonzero, + searchsorted, + where, +) -__all__ += ["argmax", "argmin", "nonzero", "count_nonzero", "searchsorted", "where"] +__all__ += ["argmax", "argmin", "count_nonzero", "nonzero", "searchsorted", "where"] -from ._set_functions import unique_all, unique_counts, unique_inverse, unique_values, isin +from ._set_functions import ( + isin, + unique_all, + unique_counts, + unique_inverse, + unique_values, +) -__all__ += ["unique_all", "unique_counts", "unique_inverse", "unique_values", "isin"] +__all__ += ["isin", "unique_all", "unique_counts", "unique_inverse", "unique_values"] from ._sorting_functions import argsort, sort __all__ += ["argsort", "sort"] -from ._statistical_functions import cumulative_sum, cumulative_prod, max, mean, min, prod, std, sum, var +from ._statistical_functions import ( + cumulative_prod, + cumulative_sum, + max, + mean, + min, + prod, + std, + sum, + var, +) -__all__ += ["cumulative_sum", "cumulative_prod", "max", "mean", "min", "prod", "std", "sum", "var"] +__all__ += ["cumulative_prod", "cumulative_sum", "max", "mean", "min", "prod", "std", "sum", "var"] from ._utility_functions import all, any, diff __all__ += ["all", "any", "diff"] from ._array_object import Device + __all__ += ["Device"] # Helper functions that are not part of the standard from ._flags import ( - set_array_api_strict_flags, + ArrayAPIStrictFlags, get_array_api_strict_flags, reset_array_api_strict_flags, - ArrayAPIStrictFlags, + set_array_api_strict_flags, ) __all__ += [ - 'set_array_api_strict_flags', - 'get_array_api_strict_flags', - 'reset_array_api_strict_flags', 'ArrayAPIStrictFlags', '__version__', + 'get_array_api_strict_flags', + 'reset_array_api_strict_flags', + 'set_array_api_strict_flags', ] try: diff --git a/array_api_strict/_array_object.py b/array_api_strict/_array_object.py index 25e6595a..2d9defab 100644 --- a/array_api_strict/_array_object.py +++ b/array_api_strict/_array_object.py @@ -17,15 +17,16 @@ import operator import sys -from collections.abc import Iterator +from collections.abc import Callable, Iterator from enum import IntEnum from types import EllipsisType, ModuleType -from typing import Any, Literal, SupportsIndex, Callable +from typing import Any, Literal, SupportsIndex import numpy as np import numpy.typing as npt from ._creation_functions import Undef, _undef, asarray +from ._devices import CPU_DEVICE, Device, device_supports_dtype from ._dtypes import ( DType, _all_dtypes, @@ -40,7 +41,6 @@ _real_to_complex_map, _result_type, ) -from ._devices import CPU_DEVICE, Device, device_supports_dtype from ._flags import get_array_api_strict_flags, set_array_api_strict_flags from ._typing import PyCapsule @@ -64,7 +64,7 @@ class Array: _array: npt.NDArray[Any] _dtype: DType _device: Device - __slots__ = ("_array", "_dtype", "_device", "__weakref__") + __slots__ = ("__weakref__", "_array", "_device", "_dtype") # Use a custom constructor instead of __init__, as manually initializing # this class is not supported API. diff --git a/array_api_strict/_creation_functions.py b/array_api_strict/_creation_functions.py index 685044df..90012a8a 100644 --- a/array_api_strict/_creation_functions.py +++ b/array_api_strict/_creation_functions.py @@ -5,11 +5,10 @@ import numpy as np -from ._dtypes import DType, _all_dtypes, _np_dtype, bool as xp_bool -from ._devices import ( - Device, device_supports_dtype, get_default_dtypes, - check_device as _check_device -) +from ._devices import Device, device_supports_dtype, get_default_dtypes +from ._devices import check_device as _check_device +from ._dtypes import DType, _all_dtypes, _np_dtype +from ._dtypes import bool as xp_bool from ._flags import get_array_api_strict_flags from ._typing import NestedSequence, SupportsBufferProtocol, SupportsDLPack diff --git a/array_api_strict/_devices.py b/array_api_strict/_devices.py index 18831991..f4ddab10 100644 --- a/array_api_strict/_devices.py +++ b/array_api_strict/_devices.py @@ -1,17 +1,29 @@ from typing import Final from ._dtypes import ( - DType, float32, float64, complex64, complex128, int64, uint64, int32, - _all_dtypes, _boolean_dtypes, _signed_integer_dtypes, - _unsigned_integer_dtypes, _integer_dtypes, _real_floating_dtypes, - _complex_floating_dtypes, _numeric_dtypes + DType, + _all_dtypes, + _boolean_dtypes, + _complex_floating_dtypes, + _integer_dtypes, + _numeric_dtypes, + _real_floating_dtypes, + _signed_integer_dtypes, + _unsigned_integer_dtypes, + complex64, + complex128, + float32, + float64, + int32, + int64, + uint64, ) _ALL_DEVICE_NAMES = ("CPU_DEVICE", "device1", "device2", "no_float64", "no_x64") class Device: _device: Final[str] - __slots__ = ("_device", "__weakref__") + __slots__ = ("__weakref__", "_device") def __init__(self, device: str = "CPU_DEVICE"): if device not in _ALL_DEVICE_NAMES: diff --git a/array_api_strict/_dtypes.py b/array_api_strict/_dtypes.py index 158d8d96..874b8623 100644 --- a/array_api_strict/_dtypes.py +++ b/array_api_strict/_dtypes.py @@ -12,7 +12,7 @@ class DType: _np_dtype: Final[np.dtype[Any]] _canonical_name: Final[Any] - __slots__ = ("_np_dtype", "_canonical_name", "__weakref__") + __slots__ = ("__weakref__", "_canonical_name", "_np_dtype") def __init__(self, np_dtype: npt.DTypeLike): self._canonical_name = np_dtype diff --git a/array_api_strict/_fft.py b/array_api_strict/_fft.py index e069435f..aecaa7a9 100644 --- a/array_api_strict/_fft.py +++ b/array_api_strict/_fft.py @@ -4,8 +4,8 @@ import numpy as np from ._array_object import Array -from ._devices import ALL_DEVICES, Device, device_supports_dtype from ._data_type_functions import astype +from ._devices import ALL_DEVICES, Device, device_supports_dtype from ._dtypes import ( DType, _complex_floating_dtypes, @@ -14,8 +14,8 @@ complex64, float32, ) -from ._info import __array_namespace_info__ from ._flags import requires_extension +from ._info import __array_namespace_info__ @requires_extension('fft') @@ -338,17 +338,17 @@ def ifftshift(x: Array, /, *, axes: int | Sequence[int] | None = None) -> Array: __all__ = [ "fft", - "ifft", + "fftfreq", "fftn", + "fftshift", + "hfft", + "ifft", "ifftn", - "rfft", + "ifftshift", + "ihfft", "irfft", - "rfftn", "irfftn", - "hfft", - "ihfft", - "fftfreq", + "rfft", "rfftfreq", - "fftshift", - "ifftshift", + "rfftn", ] diff --git a/array_api_strict/_flags.py b/array_api_strict/_flags.py index 450c91e2..caa0d16c 100644 --- a/array_api_strict/_flags.py +++ b/array_api_strict/_flags.py @@ -14,9 +14,9 @@ import functools import os import warnings -from collections.abc import Callable +from collections.abc import Callable, Collection from types import TracebackType -from typing import Any, Collection, ParamSpec, TypeVar, cast +from typing import Any, ParamSpec, TypeVar, cast import array_api_strict diff --git a/array_api_strict/_linalg.py b/array_api_strict/_linalg.py index c512963a..aa68894c 100644 --- a/array_api_strict/_linalg.py +++ b/array_api_strict/_linalg.py @@ -9,7 +9,7 @@ from ._data_type_functions import finfo from ._dtypes import DType, _floating_dtypes, _numeric_dtypes, complex64, complex128 from ._elementwise_functions import conj -from ._flags import get_array_api_strict_flags, requires_extension, requires_api_version +from ._flags import get_array_api_strict_flags, requires_api_version, requires_extension from ._manipulation_functions import reshape from ._statistical_functions import _np_dtype_sumprod @@ -232,7 +232,7 @@ def matrix_norm( *, keepdims: bool = False, ord: float | Literal["fro", "nuc"] | None = "fro", -) -> Array: # noqa: F821 +) -> Array: """ Array API compatible wrapper for :py:func:`np.linalg.norm `. @@ -330,7 +330,7 @@ def pinv(x: Array, /, *, rtol: float | Array | None = None) -> Array: return Array._new(np.linalg.pinv(x._array, rcond=rtol_np), device=x.device) @requires_extension('linalg') -def qr(x: Array, /, *, mode: Literal['reduced', 'complete'] = 'reduced') -> QRResult: # noqa: F821 +def qr(x: Array, /, *, mode: Literal['reduced', 'complete'] = 'reduced') -> QRResult: """ Array API compatible wrapper for :py:func:`np.linalg.qr `. @@ -373,13 +373,21 @@ def slogdet(x: Array, /) -> SlogdetResult: def _solve(a: np.ndarray, b: np.ndarray) -> np.ndarray: try: from numpy.linalg._linalg import ( # type: ignore[attr-defined] - _makearray, _assert_stacked_2d, _assert_stacked_square, - _commonType, isComplexType, _raise_linalgerror_singular + _assert_stacked_2d, + _assert_stacked_square, + _commonType, + _makearray, + _raise_linalgerror_singular, + isComplexType, ) except ImportError: from numpy.linalg.linalg import ( # type: ignore[attr-defined] - _makearray, _assert_stacked_2d, _assert_stacked_square, - _commonType, isComplexType, _raise_linalgerror_singular + _assert_stacked_2d, + _assert_stacked_square, + _commonType, + _makearray, + _raise_linalgerror_singular, + isComplexType, ) from numpy.linalg import _umath_linalg diff --git a/array_api_strict/_searching_functions.py b/array_api_strict/_searching_functions.py index b435856b..207f7468 100644 --- a/array_api_strict/_searching_functions.py +++ b/array_api_strict/_searching_functions.py @@ -5,7 +5,11 @@ from ._array_object import Array from ._dtypes import _real_numeric_dtypes, _result_type from ._dtypes import bool as _bool -from ._flags import requires_api_version, requires_data_dependent_shapes, get_array_api_strict_flags +from ._flags import ( + get_array_api_strict_flags, + requires_api_version, + requires_data_dependent_shapes, +) from ._helpers import _maybe_normalize_py_scalars @@ -64,7 +68,7 @@ def count_nonzero( @requires_api_version('2023.12') def searchsorted( x1: Array, - x2: Array | int | float, + x2: Array | float, /, *, side: Literal["left", "right"] = "left", diff --git a/array_api_strict/_set_functions.py b/array_api_strict/_set_functions.py index 1661045f..fe40b0df 100644 --- a/array_api_strict/_set_functions.py +++ b/array_api_strict/_set_functions.py @@ -3,9 +3,9 @@ import numpy as np from ._array_object import Array -from ._flags import requires_data_dependent_shapes, requires_api_version -from ._helpers import _maybe_normalize_py_scalars from ._dtypes import _result_type +from ._flags import requires_api_version, requires_data_dependent_shapes +from ._helpers import _maybe_normalize_py_scalars # Note: np.unique() is split into four functions in the array API: # unique_all, unique_counts, unique_inverse, and unique_values (this is done diff --git a/array_api_strict/tests/conftest.py b/array_api_strict/tests/conftest.py index 1a9d507e..e7d668b4 100644 --- a/array_api_strict/tests/conftest.py +++ b/array_api_strict/tests/conftest.py @@ -1,9 +1,10 @@ import os -from .._flags import reset_array_api_strict_flags, ENVIRONMENT_VARIABLES - import pytest +from .._flags import ENVIRONMENT_VARIABLES, reset_array_api_strict_flags + + def pytest_configure(config): for env_var in ENVIRONMENT_VARIABLES: if env_var in os.environ: diff --git a/array_api_strict/tests/test_array_object.py b/array_api_strict/tests/test_array_object.py index 445ee80b..03c5950e 100644 --- a/array_api_strict/tests/test_array_object.py +++ b/array_api_strict/tests/test_array_object.py @@ -1,39 +1,42 @@ -import sys -import warnings import operator import pickle +import sys +import warnings from builtins import all as all_ -from numpy.testing import assert_raises import numpy as np import pytest +from numpy.testing import assert_raises -from .. import ones, arange, reshape, asarray, result_type, all, equal, stack +import array_api_strict + +from .. import all, arange, asarray, equal, ones, reshape, result_type, stack from .._array_object import Array from .._devices import CPU_DEVICE, Device from .._dtypes import ( _all_dtypes, _boolean_dtypes, - _real_floating_dtypes, - _floating_dtypes, _complex_floating_dtypes, + _floating_dtypes, _integer_dtypes, _integer_or_boolean_dtypes, - _real_numeric_dtypes, _numeric_dtypes, - uint8, + _real_floating_dtypes, + _real_numeric_dtypes, + complex128, + float64, int8, int16, int32, int64, + uint8, uint64, - float64, - complex128, +) +from .._dtypes import ( bool as bool_, ) from .._flags import set_array_api_strict_flags -import array_api_strict def test_validate_index(): # The indexing tests in the official array API test suite test that the @@ -360,10 +363,7 @@ def _array_vals(): or y.dtype in _boolean_dtypes and x.dtype not in _boolean_dtypes or x.dtype in _floating_dtypes and y.dtype not in _floating_dtypes or y.dtype in _floating_dtypes and x.dtype not in _floating_dtypes - ): - assert_raises(TypeError, lambda: getattr(x, _op)(y)) - # Ensure in-place operators only promote to the same dtype as the left operand. - elif ( + ) or ( _op.startswith("__i") and result_type(x.dtype, y.dtype) != x.dtype ): diff --git a/array_api_strict/tests/test_creation_functions.py b/array_api_strict/tests/test_creation_functions.py index a28be649..c0b0b649 100644 --- a/array_api_strict/tests/test_creation_functions.py +++ b/array_api_strict/tests/test_creation_functions.py @@ -1,14 +1,14 @@ import warnings -from numpy.testing import assert_raises import numpy as np - import pytest +from numpy.testing import assert_raises from .. import all +from .._array_object import Array from .._creation_functions import ( - asarray, arange, + asarray, empty, empty_like, eye, @@ -22,11 +22,12 @@ zeros, zeros_like, ) -from .._dtypes import float32, float64, complex64, int32, int64, bool as xp_bool -from .._array_object import Array -from .._devices import CPU_DEVICE, ALL_DEVICES, Device -from .._info import __array_namespace_info__ +from .._devices import ALL_DEVICES, CPU_DEVICE, Device +from .._dtypes import bool as xp_bool +from .._dtypes import complex64, float32, float64, int32, int64 from .._flags import set_array_api_strict_flags +from .._info import __array_namespace_info__ + def test_asarray_errors(): # Test various protections against incorrect usage diff --git a/array_api_strict/tests/test_elementwise_functions.py b/array_api_strict/tests/test_elementwise_functions.py index 7fb6e339..c24e7d04 100644 --- a/array_api_strict/tests/test_elementwise_functions.py +++ b/array_api_strict/tests/test_elementwise_functions.py @@ -1,19 +1,18 @@ import warnings -from inspect import signature, getmodule +from inspect import getmodule, signature import numpy as np import pytest +import array_api_strict -from .. import asarray, _elementwise_functions +from .. import _elementwise_functions, asarray from .._devices import ALL_DEVICES, CPU_DEVICE, Device -from .._elementwise_functions import bitwise_left_shift, bitwise_right_shift from .._dtypes import ( - _dtype_categories, _boolean_dtypes, + _dtype_categories, _floating_dtypes, _integer_dtypes, - bool as xp_bool, float64, int8, int16, @@ -21,10 +20,12 @@ int64, uint64, ) +from .._dtypes import ( + bool as xp_bool, +) +from .._elementwise_functions import bitwise_left_shift, bitwise_right_shift from .._info import __array_namespace_info__ -from .test_array_object import _check_op_array_scalar, BIG_INT - -import array_api_strict +from .test_array_object import BIG_INT, _check_op_array_scalar def nargs(func): diff --git a/array_api_strict/tests/test_flags.py b/array_api_strict/tests/test_flags.py index 8edf5d66..52710dd5 100644 --- a/array_api_strict/tests/test_flags.py +++ b/array_api_strict/tests/test_flags.py @@ -1,20 +1,66 @@ -import sys import subprocess +import sys -from .._flags import (set_array_api_strict_flags, get_array_api_strict_flags, - reset_array_api_strict_flags) -from .._fft import (fft, ifft, fftn, ifftn, rfft, irfft, rfftn, irfftn, hfft, - ihfft, fftfreq, rfftfreq, fftshift, ifftshift) -from .._linalg import (cholesky, cross, det, diagonal, eigh, eigvalsh, inv, - matmul, matrix_norm, matrix_power, matrix_rank, matrix_transpose, outer, pinv, - qr, slogdet, solve, svd, svdvals, tensordot, trace, vecdot, vector_norm) - -from .. import (asarray, unique_all, unique_counts, unique_inverse, - unique_values, nonzero, repeat) +import pytest import array_api_strict as xp -import pytest +from .. import ( + asarray, + nonzero, + repeat, + unique_all, + unique_counts, + unique_inverse, + unique_values, +) +from .._fft import ( + fft, + fftfreq, + fftn, + fftshift, + hfft, + ifft, + ifftn, + ifftshift, + ihfft, + irfft, + irfftn, + rfft, + rfftfreq, + rfftn, +) +from .._flags import ( + get_array_api_strict_flags, + reset_array_api_strict_flags, + set_array_api_strict_flags, +) +from .._linalg import ( + cholesky, + cross, + det, + diagonal, + eigh, + eigvalsh, + inv, + matmul, + matrix_norm, + matrix_power, + matrix_rank, + matrix_transpose, + outer, + pinv, + qr, + slogdet, + solve, + svd, + svdvals, + tensordot, + trace, + vecdot, + vector_norm, +) + def test_flag_defaults(): flags = get_array_api_strict_flags() diff --git a/array_api_strict/tests/test_linalg.py b/array_api_strict/tests/test_linalg.py index 04023bc2..7e6c05f2 100644 --- a/array_api_strict/tests/test_linalg.py +++ b/array_api_strict/tests/test_linalg.py @@ -1,9 +1,9 @@ import pytest -from .._flags import set_array_api_strict_flags - import array_api_strict as xp +from .._flags import set_array_api_strict_flags + # TODO: Maybe all of these exceptions should be IndexError? # Technically this is linear_algebra, not linalg, but it's simpler to keep diff --git a/array_api_strict/tests/test_manipulation_functions.py b/array_api_strict/tests/test_manipulation_functions.py index 53b8fcf5..0fe66da9 100644 --- a/array_api_strict/tests/test_manipulation_functions.py +++ b/array_api_strict/tests/test_manipulation_functions.py @@ -1,13 +1,9 @@ -from numpy.testing import assert_raises import numpy as np +from numpy.testing import assert_raises from .._creation_functions import asarray from .._dtypes import float64, int8 -from .._manipulation_functions import ( - concat, - reshape, - stack -) +from .._manipulation_functions import concat, reshape, stack def test_concat_errors(): diff --git a/array_api_strict/tests/test_searching_functions.py b/array_api_strict/tests/test_searching_functions.py index 409deb5b..2be514b0 100644 --- a/array_api_strict/tests/test_searching_functions.py +++ b/array_api_strict/tests/test_searching_functions.py @@ -1,8 +1,8 @@ import pytest import array_api_strict as xp - from array_api_strict import ArrayAPIStrictFlags + from .._devices import ALL_DEVICES, CPU_DEVICE, Device from .._dtypes import _all_dtypes diff --git a/array_api_strict/tests/test_statistical_functions.py b/array_api_strict/tests/test_statistical_functions.py index 52ad53a0..4903ae4d 100644 --- a/array_api_strict/tests/test_statistical_functions.py +++ b/array_api_strict/tests/test_statistical_functions.py @@ -1,10 +1,12 @@ import cmath -import pytest -from .._flags import set_array_api_strict_flags, ArrayAPIStrictFlags +import pytest import array_api_strict as xp +from .._flags import ArrayAPIStrictFlags, set_array_api_strict_flags + + # sum, prod, and trace were changed in 2023.12 to not upcast floating-point dtypes # with dtype=None @pytest.mark.parametrize('func_name', ['sum', 'prod', 'trace']) @@ -44,9 +46,8 @@ def test_sum_prod_trace_2023_12(func_name): def test_mean_complex(): a = xp.asarray([1j, 2j, 3j]) - with ArrayAPIStrictFlags(api_version='2023.12'): - with pytest.raises(TypeError): - xp.mean(a) + with ArrayAPIStrictFlags(api_version='2023.12'), pytest.raises(TypeError): + xp.mean(a) m = xp.mean(a) assert cmath.isclose(complex(m), 2j) diff --git a/docs/conf.py b/docs/conf.py index 14d7503b..778d7893 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -6,8 +6,9 @@ # -- Project information ----------------------------------------------------- # https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information -import sys import os +import sys + sys.path.insert(0, os.path.abspath('..')) project = 'array-api-strict' From 443af84315cfe01bb0a7464fdfb87cec5cef7e24 Mon Sep 17 00:00:00 2001 From: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> Date: Thu, 30 Jul 2026 15:49:57 +0530 Subject: [PATCH 2/2] Suggestions Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> --- array_api_strict/_creation_functions.py | 11 +++++++---- array_api_strict/_searching_functions.py | 3 +-- array_api_strict/tests/test_array_object.py | 4 +--- array_api_strict/tests/test_creation_functions.py | 3 +-- array_api_strict/tests/test_elementwise_functions.py | 4 +--- array_api_strict/tests/test_statistical_functions.py | 5 +++-- ruff.toml | 3 +++ 7 files changed, 17 insertions(+), 16 deletions(-) diff --git a/array_api_strict/_creation_functions.py b/array_api_strict/_creation_functions.py index 90012a8a..6d69cb15 100644 --- a/array_api_strict/_creation_functions.py +++ b/array_api_strict/_creation_functions.py @@ -5,10 +5,13 @@ import numpy as np -from ._devices import Device, device_supports_dtype, get_default_dtypes -from ._devices import check_device as _check_device -from ._dtypes import DType, _all_dtypes, _np_dtype -from ._dtypes import bool as xp_bool +from ._devices import ( + Device, + check_device as _check_device, + device_supports_dtype, + get_default_dtypes, +) +from ._dtypes import DType, _all_dtypes, _np_dtype, bool as xp_bool from ._flags import get_array_api_strict_flags from ._typing import NestedSequence, SupportsBufferProtocol, SupportsDLPack diff --git a/array_api_strict/_searching_functions.py b/array_api_strict/_searching_functions.py index 207f7468..6b8659b2 100644 --- a/array_api_strict/_searching_functions.py +++ b/array_api_strict/_searching_functions.py @@ -3,8 +3,7 @@ import numpy as np from ._array_object import Array -from ._dtypes import _real_numeric_dtypes, _result_type -from ._dtypes import bool as _bool +from ._dtypes import _real_numeric_dtypes, _result_type, bool as _bool from ._flags import ( get_array_api_strict_flags, requires_api_version, diff --git a/array_api_strict/tests/test_array_object.py b/array_api_strict/tests/test_array_object.py index 03c5950e..94cd87b8 100644 --- a/array_api_strict/tests/test_array_object.py +++ b/array_api_strict/tests/test_array_object.py @@ -23,6 +23,7 @@ _numeric_dtypes, _real_floating_dtypes, _real_numeric_dtypes, + bool as bool_, complex128, float64, int8, @@ -32,9 +33,6 @@ uint8, uint64, ) -from .._dtypes import ( - bool as bool_, -) from .._flags import set_array_api_strict_flags diff --git a/array_api_strict/tests/test_creation_functions.py b/array_api_strict/tests/test_creation_functions.py index c0b0b649..2ea44456 100644 --- a/array_api_strict/tests/test_creation_functions.py +++ b/array_api_strict/tests/test_creation_functions.py @@ -23,8 +23,7 @@ zeros_like, ) from .._devices import ALL_DEVICES, CPU_DEVICE, Device -from .._dtypes import bool as xp_bool -from .._dtypes import complex64, float32, float64, int32, int64 +from .._dtypes import bool as xp_bool, complex64, float32, float64, int32, int64 from .._flags import set_array_api_strict_flags from .._info import __array_namespace_info__ diff --git a/array_api_strict/tests/test_elementwise_functions.py b/array_api_strict/tests/test_elementwise_functions.py index c24e7d04..6f7ef755 100644 --- a/array_api_strict/tests/test_elementwise_functions.py +++ b/array_api_strict/tests/test_elementwise_functions.py @@ -13,6 +13,7 @@ _dtype_categories, _floating_dtypes, _integer_dtypes, + bool as xp_bool, float64, int8, int16, @@ -20,9 +21,6 @@ int64, uint64, ) -from .._dtypes import ( - bool as xp_bool, -) from .._elementwise_functions import bitwise_left_shift, bitwise_right_shift from .._info import __array_namespace_info__ from .test_array_object import BIG_INT, _check_op_array_scalar diff --git a/array_api_strict/tests/test_statistical_functions.py b/array_api_strict/tests/test_statistical_functions.py index 4903ae4d..68b85f82 100644 --- a/array_api_strict/tests/test_statistical_functions.py +++ b/array_api_strict/tests/test_statistical_functions.py @@ -46,8 +46,9 @@ def test_sum_prod_trace_2023_12(func_name): def test_mean_complex(): a = xp.asarray([1j, 2j, 3j]) - with ArrayAPIStrictFlags(api_version='2023.12'), pytest.raises(TypeError): - xp.mean(a) + with ArrayAPIStrictFlags(api_version='2023.12'): + with pytest.raises(TypeError): + xp.mean(a) m = xp.mean(a) assert cmath.isclose(complex(m), 2j) diff --git a/ruff.toml b/ruff.toml index 43830f2b..15d7ebe7 100644 --- a/ruff.toml +++ b/ruff.toml @@ -4,4 +4,7 @@ ignore = [ "E402", # Annoying style checks "E7", + "SIM117", ] +[lint.isort] +combine-as-imports = true