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
|
# 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 sys
from copy import copy
from types import SimpleNamespace
from typing import Tuple
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,
)
MOCK_WARN_MSG = f"determine.*{mock_xp.__name__}.*Array API"
def make_mock_xp(*, exclude: Tuple[str, ...] = ()) -> SimpleNamespace:
xp = copy(mock_xp)
assert isinstance(exclude, tuple) # sanity check
for attr in exclude:
delattr(xp, attr)
return xp
def test_warning_on_noncompliant_xp():
"""Using non-compliant array modules raises helpful warning"""
xp = make_mock_xp()
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], "full")],
)
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}"):
func(*args).example()
dtypeless_xp = make_mock_xp(exclude=tuple(DTYPE_NAMES))
with pytest.warns(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"):
func().example()
@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)
@pytest.mark.filterwarnings(f"ignore:.*{MOCK_WARN_MSG}.*")
def test_patch_torch_full(monkeypatch):
"""When xp is torch, full() is patched so xps.arrays() can work.
See https://github.com/HypothesisWorks/hypothesis/pull/3542
"""
xp = make_mock_xp()
old_full = copy(xp.full)
def bad_full(shape, *a, **kw):
bad_full.__counter += 1
if isinstance(shape, int):
raise AttributeError("xp.full() has not been patched correctly")
return old_full(shape, *a, **kw)
bad_full.__counter = 0
xp.full = bad_full
with monkeypatch.context() as m:
m.setitem(sys.modules, "torch", xp)
xps = make_strategies_namespace(xp)
xps.arrays(xp.int8, 5).example()
assert bad_full.__counter > 0 # sanity check
|