Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add allow_subnormal passthrough to strategies.numpy.from_dtype() for complex values #3558

Merged
merged 5 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RELEASE_TYPE: patch

This makes :func:`hypothesis.extra.numpy.from_dtype` pass through the parameter `allow_subnormal` for complex dtypes.
5 changes: 3 additions & 2 deletions hypothesis-python/src/hypothesis/extra/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,12 @@ def compat_kw(*args, **kw):
# If anyone wants to add a `width` argument to `complex_numbers()`, we would
# accept a pull request and add passthrough support for magnitude bounds,
# but it's a low priority otherwise.
kws = compat_kw("allow_nan", "allow_infinity", "allow_subnormal")
if dtype.itemsize == 8:
float32 = st.floats(width=32, **compat_kw("allow_nan", "allow_infinity"))
float32 = st.floats(width=32, **kws)
result = st.builds(complex, float32, float32)
else:
result = st.complex_numbers(**compat_kw("allow_nan", "allow_infinity"))
result = st.complex_numbers(**kws)
elif dtype.kind in ("S", "a"):
# Numpy strings are null-terminated; only allow round-trippable values.
# `itemsize == 0` means 'fixed length determined at array creation'
Expand Down
36 changes: 35 additions & 1 deletion hypothesis-python/tests/numpy/test_from_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
from hypothesis import assume, given, settings, strategies as st
from hypothesis.errors import InvalidArgument
from hypothesis.extra import numpy as nps
from hypothesis.internal.floats import width_smallest_normals
from hypothesis.strategies._internal import SearchStrategy

from tests.common.debug import find_any
from tests.common.debug import assert_no_examples, find_any

STANDARD_TYPES = [
np.dtype(t)
Expand Down Expand Up @@ -222,3 +223,36 @@ def test_customize_structured_dtypes(x):
assert len(name) >= 1
assert 0 <= age <= 255
assert not np.isnan(score)


@pytest.mark.parametrize("allow_subnormal", [False, True])
@pytest.mark.parametrize("width", [32, 64])
def test_float_subnormal_generation(allow_subnormal, width):
dtype = np.dtype(f"float{width}")
strat = nps.from_dtype(dtype, allow_subnormal=allow_subnormal).filter(
lambda n: n != 0
)
smallest_normal = width_smallest_normals[width]
condition = lambda n: -smallest_normal < n < smallest_normal
if allow_subnormal:
find_any(strat, condition)
else:
assert_no_examples(strat, condition)


@pytest.mark.parametrize("allow_subnormal", [False, True])
@pytest.mark.parametrize("width", [64, 128])
def test_complex_subnormal_generation(allow_subnormal, width):
dtype = np.dtype(f"complex{width}")
strat = nps.from_dtype(dtype, allow_subnormal=allow_subnormal).filter(
lambda n: n.real != 0 and n.imag != 0
)
smallest_normal = width_smallest_normals[width / 2]
condition = lambda n: (
-smallest_normal < n.real < smallest_normal
or -smallest_normal < n.imag < smallest_normal
)
if allow_subnormal:
find_any(strat, condition)
else:
assert_no_examples(strat, condition)