Skip to content

Commit

Permalink
Clean up final warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Jun 4, 2023
1 parent 1b1d27b commit 0600055
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 18 deletions.
2 changes: 1 addition & 1 deletion hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RELEASE_TYPE: patch
RELEASE_TYPE: minor

Warn in :func:`~hypothesis.strategies.from_type` if the inferred strategy
has no variation (always returning default instances). Also handles numpy
Expand Down
7 changes: 5 additions & 2 deletions hypothesis-python/src/hypothesis/extra/ghostwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import re
import sys
import types
import warnings
from collections import OrderedDict, defaultdict
from itertools import permutations, zip_longest
from keyword import iskeyword
Expand Down Expand Up @@ -107,7 +108,7 @@
import black

from hypothesis import Verbosity, find, settings, strategies as st
from hypothesis.errors import InvalidArgument
from hypothesis.errors import InvalidArgument, SmallSearchSpaceWarning
from hypothesis.internal.compat import get_args, get_origin, get_type_hints
from hypothesis.internal.reflection import get_signature, is_mock
from hypothesis.internal.validation import check_type
Expand Down Expand Up @@ -613,7 +614,9 @@ def _imports_for_strategy(strategy):
return {(module, strategy.function.__name__)}

imports = set()
strategy = unwrap_strategies(strategy)
with warnings.catch_warnings():
warnings.simplefilter("ignore", SmallSearchSpaceWarning)
strategy = unwrap_strategies(strategy)

# Get imports for s.map(f), s.filter(f), s.flatmap(f), including both s and f
if isinstance(strategy, MappedSearchStrategy):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,7 @@ def from_type_guarded(thing):
kwargs[k] = from_type_guarded(hints[k])
if p.default is not Parameter.empty and kwargs[k] is not ...:
kwargs[k] = just(p.default) | kwargs[k]
if not kwargs and params:
if params and not kwargs:
from_type_repr = repr_call(from_type, (thing,), {})
builds_repr = repr_call(builds, (thing,), {})
warnings.warn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ def _networks(bits):
filter: st.builds(filter, st.just(lambda _: None), st.just(())),
map: st.builds(map, st.just(lambda _: None), st.just(())),
reversed: st.builds(reversed, st.just(())),
property: st.builds(property, st.just(lambda _: None)),
classmethod: st.builds(classmethod, st.just(lambda self: self)),
staticmethod: st.builds(staticmethod, st.just(lambda self: self)),
super: st.builds(super, st.from_type(type)),
Expand Down
11 changes: 6 additions & 5 deletions hypothesis-python/tests/cover/test_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import pytest

from hypothesis import HealthCheck, assume, given, settings, strategies as st
from hypothesis.errors import InvalidArgument, ResolutionFailed
from hypothesis.errors import InvalidArgument, ResolutionFailed, SmallSearchSpaceWarning
from hypothesis.internal.compat import PYPY, get_type_hints
from hypothesis.internal.reflection import get_pretty_function_description
from hypothesis.strategies import from_type
Expand Down Expand Up @@ -971,15 +971,16 @@ def test_from_type_can_be_default_or_annotation():
@pytest.mark.parametrize("t", BUILTIN_TYPES, ids=lambda t: t.__name__)
def test_resolves_builtin_types(t):
with warnings.catch_warnings():
# Some types may warn SmallSearchSpaceWarning, ignore those
warnings.simplefilter("ignore")
warnings.simplefilter("ignore", SmallSearchSpaceWarning)
v = st.from_type(t).example()
assert isinstance(v, t)


@pytest.mark.parametrize("t", BUILTIN_TYPES, ids=lambda t: t.__name__)
def test_resolves_forwardrefs_to_builtin_types(t):
v = st.from_type(typing.ForwardRef(t.__name__)).example()
@given(data=st.data())
def test_resolves_forwardrefs_to_builtin_types(t, data):
s = st.from_type(typing.ForwardRef(t.__name__))
v = data.draw(s)
assert isinstance(v, t)


Expand Down
14 changes: 5 additions & 9 deletions hypothesis-python/tests/ghostwriter/test_expected_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
)

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

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


def test_ghostwriter_on_hypothesis(update_recorded_outputs):
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})
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 0600055

Please sign in to comment.