Skip to content

Commit

Permalink
Fixes more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jobh committed Jun 1, 2023
1 parent 89f03ea commit 0db6d7d
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 15 deletions.
3 changes: 2 additions & 1 deletion hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
RELEASE_TYPE: patch

Warn in :func:`~hypothesis.strategies.from_type` if the inferred strategy
has no variation (always returning default instances).
has no variation (always returning default instances). Also ensures
variation in numpy data types by inferring h.extras.numpy.from_dtype().
4 changes: 2 additions & 2 deletions hypothesis-python/src/hypothesis/strategies/_internal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,7 @@ def from_type_guarded(thing):
hints = get_type_hints(thing)
params = get_signature(thing).parameters
except Exception:
params = {}
params: MappingProxyType[str, Parameter] = {}
kwargs = {}
for k, p in params.items():
if (
Expand All @@ -1260,7 +1260,7 @@ def from_type_guarded(thing):
"find any (non-varargs) arguments. Use st.register_type_strategy() "
"to resolve to a strategy which can generate more than one value, "
"or silence this warning.",
SmallSearchSpaceWarning
SmallSearchSpaceWarning,
)
return builds(thing, **kwargs)
# And if it's an abstract type, we'll resolve to a union of subclasses instead.
Expand Down
6 changes: 6 additions & 0 deletions hypothesis-python/tests/cover/test_lookup_py37.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import pytest

from hypothesis import assume, given
from hypothesis import strategies as st

# On Python 3.7 and 3.8, `from __future__ import annotations` means
# that the syntax is supported; but the feature fails at runtime. On Python
Expand All @@ -40,6 +41,11 @@ class Value:
pass


# To avoid SmallSearchSpaceWarning
st.register_type_strategy(Elem, st.builds(Elem))
st.register_type_strategy(Value, st.builds(Value))


def check(t, ex):
assert isinstance(ex, t)
assert all(isinstance(e, Elem) for e in ex)
Expand Down
3 changes: 2 additions & 1 deletion hypothesis-python/tests/cover/test_type_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
HypothesisDeprecationWarning,
InvalidArgument,
ResolutionFailed,
SmallSearchSpaceWarning,
)
from hypothesis.internal.compat import get_type_hints
from hypothesis.internal.reflection import get_pretty_function_description
Expand Down Expand Up @@ -203,7 +204,7 @@ def test_uninspectable_builds():


def test_uninspectable_from_type():
with pytest.raises(TypeError, match="object is not callable"):
with pytest.warns(SmallSearchSpaceWarning), pytest.raises(TypeError, match="object is not callable"):
st.from_type(BrokenClass).example()


Expand Down
15 changes: 10 additions & 5 deletions hypothesis-python/tests/cover/test_type_lookup_forward_ref.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import pytest

from hypothesis import given, strategies as st
from hypothesis.errors import ResolutionFailed
from hypothesis.errors import ResolutionFailed, SmallSearchSpaceWarning

from tests.common import utils

Expand Down Expand Up @@ -91,10 +91,15 @@ def missing_dot_access_fun(thing: _MissingDotAccess) -> int:
return 1


@given(st.builds(correct_dot_access_fun))
def test_bound_correct_dot_access_forward_ref(built):
"""Correct resolution of dot access types."""
assert isinstance(built, int)
def test_bound_correct_dot_access_forward_ref():
with pytest.warns(SmallSearchSpaceWarning):

@given(st.builds(correct_dot_access_fun))
def test(built):
"""Correct resolution of dot access types."""
assert isinstance(built, int)

test()


@pytest.mark.parametrize("function", [wrong_dot_access_fun, missing_dot_access_fun])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ from example_code.future_annotations import CustomClass
from hypothesis import given, strategies as st


@given(c1=st.builds(CustomClass), c2=st.one_of(st.none(), st.builds(CustomClass)))
@given(
c1=st.builds(CustomClass, number=st.integers()),
c2=st.one_of(st.none(), st.builds(CustomClass, number=st.integers())),
)
def test_fuzz_add_custom_classes(
c1: example_code.future_annotations.CustomClass,
c2: typing.Union[example_code.future_annotations.CustomClass, None],
Expand Down
12 changes: 7 additions & 5 deletions hypothesis-python/tests/ghostwriter/test_expected_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
)

import hypothesis
from hypothesis.errors import SmallSearchSpaceWarning
from hypothesis.extra import ghostwriter
from hypothesis.utils.conventions import not_set

Expand Down Expand Up @@ -267,8 +268,9 @@ def test_ghostwriter_example_outputs(update_recorded_outputs, data):


def test_ghostwriter_on_hypothesis(update_recorded_outputs):
actual = ghostwriter.magic(hypothesis).replace("Strategy[+Ex]", "Strategy")
expected = get_recorded("hypothesis_module_magic", actual * update_recorded_outputs)
if sys.version_info[:2] < (3, 10):
assert actual == expected
exec(expected, {"not_set": not_set})
with pytest.warns(SmallSearchSpaceWarning):
actual = ghostwriter.magic(hypothesis).replace("Strategy[+Ex]", "Strategy")
expected = get_recorded("hypothesis_module_magic", actual * update_recorded_outputs)
if sys.version_info[:2] < (3, 10):
assert actual == expected
exec(expected, {"not_set": not_set})

0 comments on commit 0db6d7d

Please sign in to comment.