Skip to content

Commit 1fa44c7

Browse files
committed
Monkey patch _from_dtype() to ignore troublesome internal logic
1 parent 50e7775 commit 1fa44c7

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

array_api_tests/__init__.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
from functools import wraps
22

33
from hypothesis import strategies as st
4-
from hypothesis.extra.array_api import make_strategies_namespace
4+
from hypothesis.extra import array_api
55

66
from ._array_module import mod as _xp
77

88
__all__ = ["xps"]
99

10-
xps = make_strategies_namespace(_xp)
11-
1210

1311
# We monkey patch floats() to always disable subnormals as they are out-of-scope
1412

@@ -23,5 +21,29 @@ def floats(*a, **kw):
2321

2422
st.floats = floats
2523

24+
25+
# We do the same with xps.from_dtype() - this is not strictly necessary, as
26+
# the underlying floats() will never generate subnormals. We only do this
27+
# because internal logic in xps.from_dtype() assumes xp.finfo() has its
28+
# attributes as scalar floats, which is expected behaviour but disrupts many
29+
# unrelated tests.
30+
try:
31+
__from_dtype = array_api._from_dtype
32+
33+
@wraps(__from_dtype)
34+
def _from_dtype(*a, **kw):
35+
kw["allow_subnormal"] = False
36+
return __from_dtype(*a, **kw)
37+
38+
array_api._from_dtype = _from_dtype
39+
except AttributeError:
40+
# Ignore monkey patching if Hypothesis changes the private API
41+
pass
42+
43+
44+
xps = array_api.make_strategies_namespace(_xp)
45+
46+
2647
from . import _version
27-
__version__ = _version.get_versions()['version']
48+
49+
__version__ = _version.get_versions()["version"]

0 commit comments

Comments
 (0)