| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
from inspect import signature |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
from hypothesis.errors import InvalidArgument |
| 16 |
from hypothesis.extra import array_api |
| 17 |
from hypothesis.extra.array_api import make_strategies_namespace |
| 18 |
|
| 19 |
from tests.array_api.common import MIN_VER_FOR_COMPLEX |
| 20 |
|
| 21 |
|
| 22 |
@pytest.mark.parametrize( |
| 23 |
"name", |
| 24 |
[ |
| 25 |
"from_dtype", |
| 26 |
"arrays", |
| 27 |
"array_shapes", |
| 28 |
"scalar_dtypes", |
| 29 |
"boolean_dtypes", |
| 30 |
"numeric_dtypes", |
| 31 |
"integer_dtypes", |
| 32 |
"unsigned_integer_dtypes", |
| 33 |
"floating_dtypes", |
| 34 |
"real_dtypes", |
| 35 |
pytest.param( |
| 36 |
"complex_dtypes", marks=pytest.mark.xp_min_version(MIN_VER_FOR_COMPLEX) |
| 37 |
), |
| 38 |
"valid_tuple_axes", |
| 39 |
"broadcastable_shapes", |
| 40 |
"mutually_broadcastable_shapes", |
| 41 |
"indices", |
| 42 |
], |
| 43 |
) |
| 44 |
def test_namespaced_methods_meta(xp, xps, name): |
| 45 |
"""Namespaced method objects have good meta attributes.""" |
| 46 |
func = getattr(xps, name) |
| 47 |
assert func.__name__ == name |
| 48 |
assert func.__doc__ is not None |
| 49 |
# The (private) top-level strategy methods may expose a xp argument in their |
| 50 |
# function signatures. make_strategies_namespace() exists to wrap these |
| 51 |
# top-level methods by binding the passed xp argument, and so the namespace |
| 52 |
# it returns should not expose xp in any of its function signatures. |
| 53 |
assert "xp" not in signature(func).parameters |
| 54 |
|
| 55 |
|
| 56 |
@pytest.mark.parametrize( |
| 57 |
"name, valid_args", |
| 58 |
[ |
| 59 |
("from_dtype", ["int8"]), |
| 60 |
("arrays", ["int8", 5]), |
| 61 |
("array_shapes", []), |
| 62 |
("scalar_dtypes", []), |
| 63 |
("boolean_dtypes", []), |
| 64 |
("numeric_dtypes", []), |
| 65 |
("integer_dtypes", []), |
| 66 |
("unsigned_integer_dtypes", []), |
| 67 |
("floating_dtypes", []), |
| 68 |
("real_dtypes", []), |
| 69 |
pytest.param( |
| 70 |
"complex_dtypes", [], marks=pytest.mark.xp_min_version(MIN_VER_FOR_COMPLEX) |
| 71 |
), |
| 72 |
("valid_tuple_axes", [0]), |
| 73 |
("broadcastable_shapes", [()]), |
| 74 |
("mutually_broadcastable_shapes", [3]), |
| 75 |
("indices", [(5,)]), |
| 76 |
], |
| 77 |
) |
| 78 |
def test_namespaced_strategies_repr(xp, xps, name, valid_args): |
| 79 |
"""Namespaced strategies have good repr.""" |
| 80 |
func = getattr(xps, name) |
| 81 |
strat = func(*valid_args) |
| 82 |
assert repr(strat).startswith(name + "("), f"{name} not in strat repr {strat!r}" |
| 83 |
assert len(repr(strat)) < 100, "strat repr looks too long" |
| 84 |
assert xp.__name__ not in repr(strat), f"{xp.__name__} in strat repr" |
| 85 |
|
| 86 |
|
| 87 |
@pytest.mark.filterwarnings("ignore::hypothesis.errors.HypothesisWarning") |
| 88 |
def test_inferred_version_strategies_namespace_repr(xp): |
| 89 |
"""Strategies namespace has good repr when api_version=None.""" |
| 90 |
try: |
| 91 |
xps = make_strategies_namespace(xp) |
| 92 |
except InvalidArgument as e: |
| 93 |
pytest.skip(str(e)) |
| 94 |
expected = f"make_strategies_namespace({xp.__name__})" |
| 95 |
assert repr(xps) == expected |
| 96 |
assert str(xps) == expected |
| 97 |
|
| 98 |
|
| 99 |
@pytest.mark.filterwarnings("ignore::hypothesis.errors.HypothesisWarning") |
| 100 |
def test_specified_version_strategies_namespace_repr(xp, monkeypatch): |
| 101 |
"""Strategies namespace has good repr when api_version is specified.""" |
| 102 |
monkeypatch.setattr(array_api, "_args_to_xps", {}) # ignore cached versions |
| 103 |
xps = make_strategies_namespace(xp, api_version="2021.12") |
| 104 |
expected = f"make_strategies_namespace({xp.__name__}, api_version='2021.12')" |
| 105 |
assert repr(xps) == expected |
| 106 |
assert str(xps) == expected |