From 0db6d7dbb5da2dbb6bd74393f22c8f581dd2b2b8 Mon Sep 17 00:00:00 2001 From: Joachim B Haga Date: Thu, 1 Jun 2023 15:31:05 +0200 Subject: [PATCH] Fixes more tests --- hypothesis-python/RELEASE.rst | 3 ++- .../src/hypothesis/strategies/_internal/core.py | 4 ++-- hypothesis-python/tests/cover/test_lookup_py37.py | 6 ++++++ hypothesis-python/tests/cover/test_type_lookup.py | 3 ++- .../tests/cover/test_type_lookup_forward_ref.py | 15 ++++++++++----- .../ghostwriter/recorded/add_custom_classes.txt | 5 ++++- .../tests/ghostwriter/test_expected_output.py | 12 +++++++----- 7 files changed, 33 insertions(+), 15 deletions(-) diff --git a/hypothesis-python/RELEASE.rst b/hypothesis-python/RELEASE.rst index 77428793c89..a589d777512 100644 --- a/hypothesis-python/RELEASE.rst +++ b/hypothesis-python/RELEASE.rst @@ -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(). diff --git a/hypothesis-python/src/hypothesis/strategies/_internal/core.py b/hypothesis-python/src/hypothesis/strategies/_internal/core.py index 0b2b40ec20a..a44d8624d57 100644 --- a/hypothesis-python/src/hypothesis/strategies/_internal/core.py +++ b/hypothesis-python/src/hypothesis/strategies/_internal/core.py @@ -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 ( @@ -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. diff --git a/hypothesis-python/tests/cover/test_lookup_py37.py b/hypothesis-python/tests/cover/test_lookup_py37.py index 6a1955eb6b6..caed7ba5e2b 100644 --- a/hypothesis-python/tests/cover/test_lookup_py37.py +++ b/hypothesis-python/tests/cover/test_lookup_py37.py @@ -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 @@ -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) diff --git a/hypothesis-python/tests/cover/test_type_lookup.py b/hypothesis-python/tests/cover/test_type_lookup.py index f4537e88438..44f05a8b56d 100644 --- a/hypothesis-python/tests/cover/test_type_lookup.py +++ b/hypothesis-python/tests/cover/test_type_lookup.py @@ -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 @@ -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() diff --git a/hypothesis-python/tests/cover/test_type_lookup_forward_ref.py b/hypothesis-python/tests/cover/test_type_lookup_forward_ref.py index 488023b6c74..e91ce9dff15 100644 --- a/hypothesis-python/tests/cover/test_type_lookup_forward_ref.py +++ b/hypothesis-python/tests/cover/test_type_lookup_forward_ref.py @@ -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 @@ -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]) diff --git a/hypothesis-python/tests/ghostwriter/recorded/add_custom_classes.txt b/hypothesis-python/tests/ghostwriter/recorded/add_custom_classes.txt index e315456ec4f..b13ae3f76ac 100644 --- a/hypothesis-python/tests/ghostwriter/recorded/add_custom_classes.txt +++ b/hypothesis-python/tests/ghostwriter/recorded/add_custom_classes.txt @@ -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], diff --git a/hypothesis-python/tests/ghostwriter/test_expected_output.py b/hypothesis-python/tests/ghostwriter/test_expected_output.py index 60a3c4f4d98..e2ee4ac54f7 100644 --- a/hypothesis-python/tests/ghostwriter/test_expected_output.py +++ b/hypothesis-python/tests/ghostwriter/test_expected_output.py @@ -32,6 +32,7 @@ ) import hypothesis +from hypothesis.errors import SmallSearchSpaceWarning from hypothesis.extra import ghostwriter from hypothesis.utils.conventions import not_set @@ -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})