From b7fda79489b24ff88def49c61bf4c31a59833807 Mon Sep 17 00:00:00 2001 From: Matthew Barber Date: Wed, 17 Nov 2021 10:40:12 -0600 Subject: [PATCH] Monkey patch `_from_dtype()` to work around `st.floats()` bug --- array_api_tests/__init__.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/array_api_tests/__init__.py b/array_api_tests/__init__.py index 763cef14..92c18f84 100644 --- a/array_api_tests/__init__.py +++ b/array_api_tests/__init__.py @@ -1,10 +1,27 @@ -from hypothesis.extra.array_api import make_strategies_namespace +from hypothesis.extra import array_api +from . import dtype_helpers as dh from ._array_module import mod as _xp +__all__ = ["xps"] -xps = make_strategies_namespace(_xp) +# For now we monkey patch the internal _from_dtype() method to work around a bug +# in st.floats() - see https://github.com/HypothesisWorks/hypothesis/issues/3153 -del _xp -del make_strategies_namespace + +broken_from_dtype = array_api._from_dtype + + +def _from_dtype(xp, dtype, **kwargs): + strat = broken_from_dtype(_xp, dtype, **kwargs) + if dh.is_float_dtype(dtype): + smallest_normal = xp.finfo(dtype).smallest_normal + strat = strat.filter(lambda n: abs(n) >= smallest_normal) + return strat + + +array_api._from_dtype = _from_dtype + + +xps = array_api.make_strategies_namespace(_xp)