Skip to content

Commit

Permalink
remove warning-compensatory changes to test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
vreuter committed Dec 26, 2023
1 parent e84d050 commit 4eb0b50
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 89 deletions.
11 changes: 0 additions & 11 deletions hypothesis-python/tests/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import contextlib
import sys
import warnings
from io import StringIO
from types import SimpleNamespace

Expand Down Expand Up @@ -86,16 +85,6 @@ def capture_out():
sys.stdout = old_out


class catch_sampled_from_strategies_warning(warnings.catch_warnings):
def __enter__(self):
warnings.filterwarnings(
action="ignore",
message="sample_from was given a collection of strategies; was one_of intended?",
category=UserWarning,
)
super().__enter__()


class ExcInfo:
pass

Expand Down
27 changes: 9 additions & 18 deletions hypothesis-python/tests/cover/test_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@
find_any,
minimal,
)
from tests.common.utils import (
catch_sampled_from_strategies_warning,
fails_with,
temp_registered,
)
from tests.common.utils import fails_with, temp_registered

sentinel = object()
BUILTIN_TYPES = tuple(
Expand Down Expand Up @@ -319,24 +315,19 @@ class Baz(Foo):
[
# Expect a warning exactly when the type constraint/bound should trigger
# passing a strategy to sampled_from.
(typing.TypeVar("V"), object, False),
(typing.TypeVar("V", bound=int), int, False),
(typing.TypeVar("V", bound=Foo), (Bar, Baz), True),
(typing.TypeVar("V", bound=typing.Union[int, str]), (int, str), True),
(typing.TypeVar("V", int, str), (int, str), False),
(typing.TypeVar("V"), object),
(typing.TypeVar("V", bound=int), int),
(typing.TypeVar("V", bound=Foo), (Bar, Baz)),
(typing.TypeVar("V", bound=typing.Union[int, str]), (int, str)),
(typing.TypeVar("V", int, str), (int, str)),
],
)
@settings(suppress_health_check=[HealthCheck.too_slow])
@given(data=st.data())
def test_typevar_type_is_consistent(data, var, expected, exp_warn):
def test_typevar_type_is_consistent(data, var, expected):
strat = st.from_type(var)
if exp_warn:
with catch_sampled_from_strategies_warning():
v1 = data.draw(strat)
v2 = data.draw(strat)
else:
v1 = data.draw(strat)
v2 = data.draw(strat)
v1 = data.draw(strat)
v2 = data.draw(strat)
assume(v1 != v2) # Values may vary, just not types
assert type(v1) == type(v2)
assert isinstance(v1, expected)
Expand Down
64 changes: 31 additions & 33 deletions hypothesis-python/tests/nocover/test_pretty_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
from hypothesis.control import reject
from hypothesis.errors import HypothesisDeprecationWarning, InvalidArgument

from tests.common.utils import catch_sampled_from_strategies_warning


def foo(x):
pass
Expand Down Expand Up @@ -53,39 +51,39 @@ def splat(value):

values = st.integers() | st.text()

with catch_sampled_from_strategies_warning():
Strategies = st.recursive(
st.one_of(
st.sampled_from(
[
st.none(),
st.booleans(),
st.randoms(use_true_random=True),
st.complex_numbers(),
st.randoms(use_true_random=True),
st.fractions(),
st.decimals(),
]
),
st.builds(st.just, values),
st.builds(st.sampled_from, st.lists(values, min_size=1)),
builds_ignoring_invalid(st.floats, st.floats(), st.floats()),

Strategies = st.recursive(
st.one_of(
st.sampled_from(
[
st.none(),
st.booleans(),
st.randoms(use_true_random=True),
st.complex_numbers(),
st.randoms(use_true_random=True),
st.fractions(),
st.decimals(),
]
),
lambda x: st.one_of(
builds_ignoring_invalid(st.lists, x, **size_strategies),
builds_ignoring_invalid(st.sets, x, **size_strategies),
builds_ignoring_invalid(lambda v: st.tuples(*v), st.lists(x)),
builds_ignoring_invalid(lambda v: st.one_of(*v), st.lists(x, min_size=1)),
builds_ignoring_invalid(
st.dictionaries,
x,
x,
dict_class=st.sampled_from([dict, OrderedDict]),
**size_strategies,
),
st.builds(lambda s, f: s.map(f), x, st.sampled_from(fns)),
st.builds(st.just, values),
st.builds(st.sampled_from, st.lists(values, min_size=1)),
builds_ignoring_invalid(st.floats, st.floats(), st.floats()),
),
lambda x: st.one_of(
builds_ignoring_invalid(st.lists, x, **size_strategies),
builds_ignoring_invalid(st.sets, x, **size_strategies),
builds_ignoring_invalid(lambda v: st.tuples(*v), st.lists(x)),
builds_ignoring_invalid(lambda v: st.one_of(*v), st.lists(x, min_size=1)),
builds_ignoring_invalid(
st.dictionaries,
x,
x,
dict_class=st.sampled_from([dict, OrderedDict]),
**size_strategies,
),
)
st.builds(lambda s, f: s.map(f), x, st.sampled_from(fns)),
),
)


strategy_globals = {k: getattr(st, k) for k in dir(st)}
Expand Down
39 changes: 18 additions & 21 deletions hypothesis-python/tests/nocover/test_reusable_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
from hypothesis import example, given, strategies as st
from hypothesis.errors import InvalidArgument

from tests.common.utils import catch_sampled_from_strategies_warning

# Be aware that tests in this file pass strategies as arguments to @example.
# That's normally a mistake, but for these tests it's intended.
# If one of these tests fails, Hypothesis will complain about the
Expand Down Expand Up @@ -47,25 +45,24 @@
def reusable():
"""Meta-strategy that produces strategies that should have
``.has_reusable_values == True``."""
with catch_sampled_from_strategies_warning():
return st.one_of(
# This looks like it should be `one_of`, but `sampled_from` is correct
# because we want this meta-strategy to yield strategies as its values.
st.sampled_from(base_reusable_strategies),
# This sometimes produces invalid combinations of arguments, which
# we filter out further down with an explicit validation check.
st.builds(
st.floats,
min_value=st.none() | st.floats(allow_nan=False),
max_value=st.none() | st.floats(allow_nan=False),
allow_infinity=st.booleans(),
allow_nan=st.booleans(),
),
st.builds(st.just, st.builds(list)),
st.builds(st.sampled_from, st.lists(st.builds(list), min_size=1)),
st.lists(reusable).map(st.one_of),
st.lists(reusable).map(lambda ls: st.tuples(*ls)),
)
return st.one_of(
# This looks like it should be `one_of`, but `sampled_from` is correct
# because we want this meta-strategy to yield strategies as its values.
st.sampled_from(base_reusable_strategies),
# This sometimes produces invalid combinations of arguments, which
# we filter out further down with an explicit validation check.
st.builds(
st.floats,
min_value=st.none() | st.floats(allow_nan=False),
max_value=st.none() | st.floats(allow_nan=False),
allow_infinity=st.booleans(),
allow_nan=st.booleans(),
),
st.builds(st.just, st.builds(list)),
st.builds(st.sampled_from, st.lists(st.builds(list), min_size=1)),
st.lists(reusable).map(st.one_of),
st.lists(reusable).map(lambda ls: st.tuples(*ls)),
)


def is_valid(s):
Expand Down
8 changes: 2 additions & 6 deletions hypothesis-python/tests/nocover/test_strategy_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
tuples,
)

from tests.common.utils import catch_sampled_from_strategies_warning

AVERAGE_LIST_LENGTH = 2


Expand Down Expand Up @@ -185,8 +183,7 @@ def repr_is_good(self, strat):

MAIN = __name__ == "__main__"

with catch_sampled_from_strategies_warning():
TestHypothesis = HypothesisSpec.TestCase
TestHypothesis = HypothesisSpec.TestCase

TestHypothesis.settings = settings(
TestHypothesis.settings,
Expand All @@ -196,5 +193,4 @@ def repr_is_good(self, strat):
)

if MAIN:
with catch_sampled_from_strategies_warning():
TestHypothesis().runTest()
TestHypothesis().runTest()

0 comments on commit 4eb0b50

Please sign in to comment.