Repository: scikit-learn/scikit-learn
File: sklearn/utils/_array_api.py (Python)

Code before:

    is_array_api_compliant : bool
        True if the arrays are containers that implement the array API spec (see
        https://data-apis.org/array-api/latest/index.html).
        Always False when array_api_dispatch=False.
    """
    array_api_dispatch = get_config()["array_api_dispatch"]
    if not array_api_dispatch:
        if xp is not None:
            return xp, False
        else:
            return np_compat, False

    if xp is not None:
        return xp, True

    arrays = _remove_non_arrays(
        *arrays,
        remove_none=remove_none,
        remove_types=remove_types,
    )

    # get_namespace can be called by helper functions that are used both in
    # array API compatible code and non-array API Cython related code. To
    # support the latter on NumPy inputs without raising a TypeError, we
    # unwrap potential Cython memoryview slices here.j
    arrays = _unwrap_memoryviewslices(*arrays)

    if not arrays:
        return np_compat, False

    _check_array_api_dispatch(array_api_dispatch)

    namespace = array_api_compat.get_namespace(*arrays)
    is_array_api_compliant = True

    if namespace.__name__ == "array_api_strict" and hasattr(
        namespace, "set_array_api_strict_flags"
    ):
        namespace.set_array_api_strict_flags(api_version="2024.12")

    return namespace, is_array_api_compliant


def get_namespace_and_device(
    *array_list, remove_none=True, remove_types=REMOVE_TYPES_DEFAULT, xp=None
):
    """Combination into one single function of `get_namespace` and `device`.

    Parameters
    ----------

Review comment (suggestion):
```suggestion
    # unwrap potential Cython memoryview slices here.
```

Code after:
    namespace : module
        Namespace shared by array objects. If any of the `arrays` are not arrays,
        the namespace defaults to the NumPy namespace.

    is_array_api_compliant : bool
        True if the arrays are containers that implement the array API spec (see
        https://data-apis.org/array-api/latest/index.html).
        Always False when array_api_dispatch=False.
    """
    array_api_dispatch = get_config()["array_api_dispatch"]
    if not array_api_dispatch:
        if xp is not None:
            return xp, False
        else:
            return np_compat, False

    if xp is not None:
        return xp, True

    arrays = _remove_non_arrays(
        *arrays,
        remove_none=remove_none,
        remove_types=remove_types,
    )

    # get_namespace can be called by helper functions that are used both in
    # array API compatible code and non-array API Cython related code. To
    # support the latter on NumPy inputs without raising a TypeError, we
    # unwrap potential Cython memoryview slices here.
    arrays = _unwrap_memoryviewslices(*arrays)

    if not arrays:
        return np_compat, False

    _check_array_api_dispatch(array_api_dispatch)

    namespace = array_api_compat.get_namespace(*arrays)
    is_array_api_compliant = True

    if namespace.__name__ == "array_api_strict" and hasattr(
        namespace, "set_array_api_strict_flags"
    ):
        namespace.set_array_api_strict_flags(api_version="2024.12")

    return namespace, is_array_api_compliant


def get_namespace_and_device(
    *array_list, remove_none=True, remove_types=REMOVE_TYPES_DEFAULT, xp=None
):
    """Combination into one single function of `get_namespace` and `device`.