Debian Codegraph @

hypothesis-python/tests/array_api/test_strategies_namespace.py

raw
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 types import SimpleNamespace
12 from weakref import WeakValueDictionary
13
14 import pytest
15
16 from hypothesis.control import settings
17 from hypothesis.extra import array_api
18 from hypothesis.extra.array_api import (
19 NOMINAL_VERSIONS,
20 make_strategies_namespace,
21 mock_xp,
22 )
23 from hypothesis.strategies import SearchStrategy
24
25 pytestmark = [
26 pytest.mark.filterwarnings("ignore::hypothesis.errors.HypothesisWarning"),
27 pytest.mark.skipif(
28 settings.get_current_profile_name() == "threading",
29 reason="make_strategies_namespace is not thread safe",
30 ),
31 ]
32
33
34 class HashableArrayModuleFactory:
35 """
36 mock_xp cannot be hashed and thus cannot be used in our cache. So just for
37 the purposes of testing the cache, we wrap it with an unsafe hash method.
38 """
39
40 def __getattr__(self, name):
41 return getattr(mock_xp, name)
42
43 def __hash__(self):
44 return hash(tuple(sorted(mock_xp.__dict__)))
45
46
47 @pytest.mark.parametrize("api_version", ["2021.12", None])
48 def test_caching(api_version, monkeypatch):
49 """Caches namespaces respective to arguments."""
50 xp = HashableArrayModuleFactory()
51 assert isinstance(array_api._args_to_xps, WeakValueDictionary) # sanity check
52 monkeypatch.setattr(array_api, "_args_to_xps", WeakValueDictionary())
53 assert len(array_api._args_to_xps) == 0 # sanity check
54 xps1 = array_api.make_strategies_namespace(xp, api_version=api_version)
55 assert len(array_api._args_to_xps) == 1
56 xps2 = array_api.make_strategies_namespace(xp, api_version=api_version)
57 assert len(array_api._args_to_xps) == 1
58 assert isinstance(xps2, SimpleNamespace)
59 assert xps2 is xps1
60 del xps1
61 del xps2
62 assert len(array_api._args_to_xps) == 0
63
64
65 @pytest.mark.parametrize(
66 "api_version1, api_version2", [(None, "2021.12"), ("2021.12", None)]
67 )
68 def test_inferred_namespace_shares_cache(api_version1, api_version2, monkeypatch):
69 """Results from inferred versions share the same cache key as results
70 from specified versions."""
71 xp = HashableArrayModuleFactory()
72 xp.__array_api_version__ = "2021.12"
73 assert isinstance(array_api._args_to_xps, WeakValueDictionary) # sanity check
74 monkeypatch.setattr(array_api, "_args_to_xps", WeakValueDictionary())
75 assert len(array_api._args_to_xps) == 0 # sanity check
76 xps1 = array_api.make_strategies_namespace(xp, api_version=api_version1)
77 assert xps1.api_version == "2021.12" # sanity check
78 assert len(array_api._args_to_xps) == 1
79 xps2 = array_api.make_strategies_namespace(xp, api_version=api_version2)
80 assert xps2.api_version == "2021.12" # sanity check
81 assert len(array_api._args_to_xps) == 1
82 assert xps2 is xps1
83
84
85 def test_complex_dtypes_raises_on_2021_12():
86 """Accessing complex_dtypes() for 2021.12 strategy namespace raises helpful
87 error, but accessing on future versions returns expected strategy."""
88 first_xps = make_strategies_namespace(mock_xp, api_version="2021.12")
89 with pytest.raises(AttributeError, match="attempted to access"):
90 first_xps.complex_dtypes()
91 for api_version in NOMINAL_VERSIONS[1:]:
92 xps = make_strategies_namespace(mock_xp, api_version=api_version)
93 assert isinstance(xps.complex_dtypes(), SearchStrategy)