| 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 |
import warnings |
| 12 |
from importlib import import_module |
| 13 |
from os import getenv |
| 14 |
from types import ModuleType, SimpleNamespace |
| 15 |
|
| 16 |
import pytest |
| 17 |
|
| 18 |
from hypothesis.errors import HypothesisWarning, InvalidArgument |
| 19 |
from hypothesis.extra.array_api import ( |
| 20 |
NOMINAL_VERSIONS, |
| 21 |
NominalVersion, |
| 22 |
make_strategies_namespace, |
| 23 |
mock_xp, |
| 24 |
) |
| 25 |
|
| 26 |
from tests.array_api.common import installed_array_modules |
| 27 |
|
| 28 |
# See README.md in regards to the env variables |
| 29 |
test_xp_option = getenv("HYPOTHESIS_TEST_ARRAY_API", "default") |
| 30 |
|
| 31 |
test_version_option = getenv("HYPOTHESIS_TEST_ARRAY_API_VERSION", "default") |
| 32 |
if test_version_option != "default" and test_version_option not in NOMINAL_VERSIONS: |
| 33 |
raise ValueError( |
| 34 |
f"HYPOTHESIS_TEST_ARRAY_API_VERSION='{test_version_option}' is not " |
| 35 |
f"'default' or a valid api_version {NOMINAL_VERSIONS}." |
| 36 |
) |
| 37 |
with warnings.catch_warnings(): |
| 38 |
warnings.filterwarnings("ignore", category=HypothesisWarning) |
| 39 |
mock_version = "draft" if test_version_option == "default" else test_version_option |
| 40 |
mock_xps = make_strategies_namespace(mock_xp, api_version=mock_version) |
| 41 |
api_version = None if test_version_option == "default" else test_version_option |
| 42 |
|
| 43 |
|
| 44 |
class InvalidArgumentWarning(UserWarning): |
| 45 |
"""Custom warning so we can bypass our global capturing""" |
| 46 |
|
| 47 |
|
| 48 |
name_to_entry_point = installed_array_modules() |
| 49 |
xp_and_xps_pairs: list[tuple[ModuleType, SimpleNamespace]] = [] |
| 50 |
with warnings.catch_warnings(): |
| 51 |
# We ignore all warnings here as many array modules warn on import. Ideally |
| 52 |
# we would just ignore ImportWarning, but no one seems to use it! |
| 53 |
warnings.simplefilter("ignore") |
| 54 |
warnings.simplefilter("default", category=InvalidArgumentWarning) |
| 55 |
# We go through the steps described in README.md to define `xp_xps_pairs`, |
| 56 |
# which contains the array module(s) to be run against the test suite, along |
| 57 |
# with their respective strategy namespaces. |
| 58 |
if test_xp_option == "default": |
| 59 |
xp_and_xps_pairs = [(mock_xp, mock_xps)] |
| 60 |
elif test_xp_option == "all": |
| 61 |
if len(name_to_entry_point) == 0: |
| 62 |
raise ValueError( |
| 63 |
"HYPOTHESIS_TEST_ARRAY_API='all', but no entry points where found" |
| 64 |
) |
| 65 |
xp_and_xps_pairs = [(mock_xp, mock_xps)] |
| 66 |
for ep in name_to_entry_point.values(): |
| 67 |
xp = ep.load() |
| 68 |
try: |
| 69 |
xps = make_strategies_namespace(xp, api_version=api_version) |
| 70 |
except InvalidArgument as e: |
| 71 |
warnings.warn(str(e), InvalidArgumentWarning, stacklevel=1) |
| 72 |
else: |
| 73 |
xp_and_xps_pairs.append((xp, xps)) |
| 74 |
elif test_xp_option in name_to_entry_point: |
| 75 |
ep = name_to_entry_point[test_xp_option] |
| 76 |
xp = ep.load() |
| 77 |
xps = make_strategies_namespace(xp, api_version=api_version) |
| 78 |
xp_and_xps_pairs = [(xp, xps)] |
| 79 |
else: |
| 80 |
try: |
| 81 |
xp = import_module(test_xp_option) |
| 82 |
except ImportError as e: |
| 83 |
raise ValueError( |
| 84 |
f"HYPOTHESIS_TEST_ARRAY_API='{test_xp_option}' is not a valid " |
| 85 |
"option ('default' or 'all'), name of an available entry point, " |
| 86 |
"or a valid import path." |
| 87 |
) from e |
| 88 |
else: |
| 89 |
xps = make_strategies_namespace(xp, api_version=api_version) |
| 90 |
xp_and_xps_pairs = [(xp, xps)] |
| 91 |
|
| 92 |
|
| 93 |
def pytest_generate_tests(metafunc): |
| 94 |
xp_params = [] |
| 95 |
xp_and_xps_params = [] |
| 96 |
for xp, xps in xp_and_xps_pairs: |
| 97 |
xp_params.append(pytest.param(xp, id=xp.__name__)) |
| 98 |
xp_and_xps_params.append( |
| 99 |
pytest.param(xp, xps, id=f"{xp.__name__}-{xps.api_version}") |
| 100 |
) |
| 101 |
if "xp" in metafunc.fixturenames: |
| 102 |
if "xps" in metafunc.fixturenames: |
| 103 |
metafunc.parametrize("xp, xps", xp_and_xps_params) |
| 104 |
else: |
| 105 |
metafunc.parametrize("xp", xp_params) |
| 106 |
|
| 107 |
|
| 108 |
def pytest_collection_modifyitems(config, items): |
| 109 |
for item in items: |
| 110 |
if "xps" in item.fixturenames: |
| 111 |
markers = [m for m in item.own_markers if m.name == "xp_min_version"] |
| 112 |
if markers: |
| 113 |
assert len(markers) == 1 # sanity check |
| 114 |
min_version: NominalVersion = markers[0].args[0] |
| 115 |
xps_version: NominalVersion = item.callspec.params["xps"].api_version |
| 116 |
if xps_version < min_version: |
| 117 |
item.add_marker( |
| 118 |
pytest.mark.skip(reason=f"requires api_version=>{min_version}") |
| 119 |
) |