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) func = getattr(xps, stratname) with pytest.raises(InvalidArgument, match=f"{mock_xp.__name__}.*required.*{attr}"): func(*args).example()
def setup(app): if os.path.isfile( os.path.join(os.path.dirname(__file__), "..", "RELEASE.rst")): app.tags.add("has_release_file") # patch in mock array_api namespace so we can autodoc it from hypothesis.extra.array_api import make_strategies_namespace, mock_xp mod = types.ModuleType("xps") mod.__dict__.update(make_strategies_namespace(mock_xp).__dict__) assert "xps" not in sys.modules sys.modules["xps"] = mod
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) func = getattr(xps, stratname) with pytest.warns(HypothesisWarning, match=f"{mock_xp.__name__}.*dtype.*namespace"): data.draw(func())
# 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/. # # END HEADER import pytest from hypothesis.errors import HypothesisWarning from hypothesis.extra.array_api import make_strategies_namespace, mock_xp __all__ = [ "xp", "xps", "COMPLIANT_XP", ] # We try importing the Array API namespace from NumPy first, which modern # versions should include. If not available we default to our own mocked module, # which should allow our test suite to still work. A constant is set accordingly # to inform our test suite of whether the array module here is a mock or not. try: with pytest.warns(UserWarning): from numpy import array_api as xp # type: ignore xps = make_strategies_namespace(xp) COMPLIANT_XP = True except ImportError: xp = mock_xp with pytest.warns(HypothesisWarning): xps = make_strategies_namespace(xp) COMPLIANT_XP = False
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)
[("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) 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) @pytest.mark.parametrize( "stratname", [ "scalar_dtypes", "boolean_dtypes", "numeric_dtypes", "integer_dtypes", "unsigned_integer_dtypes", "floating_dtypes", ], ) def test_error_on_missing_dtypes(stratname): """Strategies raise helpful error when using array modules that lack