1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Copyright the Hypothesis Authors.
# Individual contributors are listed in AUTHORS.rst and the git log.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.
import functools
import warnings
from copy import copy
from types import SimpleNamespace
import pytest
from hypothesis import given, strategies as st
from hypothesis.errors import HypothesisWarning, InvalidArgument
from hypothesis.extra.array_api import (
COMPLEX_NAMES,
DTYPE_NAMES,
FLOAT_NAMES,
INT_NAMES,
UINT_NAMES,
make_strategies_namespace,
mock_xp,
)
from tests.common.debug import check_can_generate_examples
MOCK_WARN_MSG = f"determine.*{mock_xp.__name__}.*Array API"
class MockedArray:
def __init__(self, wrapped, *, exclude=()):
self.wrapped = wrapped
self.exclude = exclude
def __getattr__(self, name):
if name in self.exclude:
raise AttributeError(f"removed on the mock: {name}")
return object.__getattr__(self, name)
def wrap_array(func: callable, exclude: tuple[str, ...] = ()) -> callable:
@functools.wraps(func)
def wrapped(*args, **kwargs):
result = func(*args, **kwargs)
if isinstance(result, tuple):
return tuple(MockedArray(arr, exclude=exclude) for arr in result)
return MockedArray(result, exclude=exclude)
return wrapped
def make_mock_xp(
*, exclude: tuple[str, ...] = (), exclude_methods: tuple[str, ...] = ()
) -> SimpleNamespace:
xp = copy(mock_xp)
assert isinstance(exclude, tuple) # sanity check
assert isinstance(exclude_methods, tuple) # sanity check
for attr in exclude:
delattr(xp, attr)
array_returning_funcs = (
"astype",
"broadcast_arrays",
"arange",
"asarray",
"empty",
"zeros",
"ones",
"reshape",
"isnan",
"isfinite",
"logical_or",
"sum",
"nonzero",
"sort",
"unique_values",
"any",
"all",
)
for name in array_returning_funcs:
func = getattr(xp, name, None)
if func is None:
# removed in the step before
continue
setattr(xp, name, wrap_array(func, exclude=exclude_methods))
return xp
def test_warning_on_noncompliant_xp():
"""Using non-compliant array modules raises helpful warning"""
xp = make_mock_xp(exclude_methods=("__array_namespace__",))
with pytest.warns(HypothesisWarning, match=MOCK_WARN_MSG):
make_strategies_namespace(xp, api_version="draft")
@pytest.mark.filterwarnings(f"ignore:.*{MOCK_WARN_MSG}.*")
@pytest.mark.parametrize(
"stratname, args, attr",
[("from_dtype", ["int8"], "iinfo"), ("arrays", ["int8", 5], "asarray")],
)
def test_error_on_missing_attr(stratname, args, attr):
"""Strategies raise helpful error when using array modules that lack
required attributes."""
xp = make_mock_xp(exclude=(attr,))
xps = make_strategies_namespace(xp, api_version="draft")
func = getattr(xps, stratname)
with pytest.raises(InvalidArgument, match=f"{mock_xp.__name__}.*required.*{attr}"):
check_can_generate_examples(func(*args))
dtypeless_xp = make_mock_xp(exclude=tuple(DTYPE_NAMES))
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=HypothesisWarning)
dtypeless_xps = make_strategies_namespace(dtypeless_xp, api_version="draft")
@pytest.mark.parametrize(
"stratname",
[
"scalar_dtypes",
"boolean_dtypes",
"numeric_dtypes",
"integer_dtypes",
"unsigned_integer_dtypes",
"floating_dtypes",
"real_dtypes",
"complex_dtypes",
],
)
def test_error_on_missing_dtypes(stratname):
"""Strategies raise helpful error when using array modules that lack
required dtypes."""
func = getattr(dtypeless_xps, stratname)
with pytest.raises(InvalidArgument, match=f"{mock_xp.__name__}.*dtype.*namespace"):
check_can_generate_examples(func())
@pytest.mark.filterwarnings(f"ignore:.*{MOCK_WARN_MSG}.*")
@pytest.mark.parametrize(
"stratname, keep_anys",
[
("scalar_dtypes", [INT_NAMES, UINT_NAMES, FLOAT_NAMES]),
("numeric_dtypes", [INT_NAMES, UINT_NAMES, FLOAT_NAMES, COMPLEX_NAMES]),
("integer_dtypes", [INT_NAMES]),
("unsigned_integer_dtypes", [UINT_NAMES]),
("floating_dtypes", [FLOAT_NAMES]),
("real_dtypes", [INT_NAMES, UINT_NAMES, FLOAT_NAMES]),
("complex_dtypes", [COMPLEX_NAMES]),
],
)
@given(st.data())
def test_warning_on_partial_dtypes(stratname, keep_anys, data):
"""Strategies using array modules with at least one of a dtype in the
necessary category/categories execute with a warning."""
exclude = []
for keep_any in keep_anys:
exclude.extend(
data.draw(
st.lists(
st.sampled_from(keep_any),
min_size=1,
max_size=len(keep_any) - 1,
unique=True,
)
)
)
xp = make_mock_xp(exclude=tuple(exclude))
xps = make_strategies_namespace(xp, api_version="draft")
func = getattr(xps, stratname)
with pytest.warns(HypothesisWarning, match=f"{mock_xp.__name__}.*dtype.*namespace"):
data.draw(func())
def test_raises_on_inferring_with_no_dunder_version():
"""When xp has no __array_api_version__, inferring api_version raises
helpful error."""
xp = make_mock_xp(exclude=("__array_api_version__",))
with pytest.raises(InvalidArgument, match="has no attribute"):
make_strategies_namespace(xp)
def test_raises_on_invalid_dunder_version():
"""When xp has invalid __array_api_version__, inferring api_version raises
helpful error."""
xp = make_mock_xp()
xp.__array_api_version__ = None
with pytest.raises(InvalidArgument):
make_strategies_namespace(xp)
|