1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Copyright the Hypothesis Authors.
# Individual contributors are listed in AUTHORS.rst and the git log.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.
import warnings
import attr
from hypothesis import given, strategies as st
from hypothesis.errors import SmallSearchSpaceWarning
from hypothesis.strategies._internal.utils import to_jsonable
from tests.common.debug import check_can_generate_examples
def a_converter(x) -> int:
return int(x)
@attr.s
class Inferrables:
annot_converter = attr.ib(converter=a_converter)
@given(st.builds(Inferrables))
def test_attrs_inference_builds(c):
pass
def test_attrs_inference_from_type():
s = st.from_type(Inferrables)
with warnings.catch_warnings():
warnings.simplefilter("ignore", SmallSearchSpaceWarning)
check_can_generate_examples(s)
@attr.s
class AttrsClass:
n = attr.ib()
def test_jsonable_attrs():
obj = AttrsClass(n=10)
assert to_jsonable(obj, avoid_realization=False) == {"n": 10}
def test_hypothesis_is_not_the_first_to_import_attrs(testdir):
# We only import attrs if the user did so first.
test_path = testdir.makepyfile(
"""
import os
# don't load hypothesis plugins, which might transitively import attrs
os.environ["HYPOTHESIS_NO_PLUGINS"] = "1"
import sys
assert "attrs" not in sys.modules
from hypothesis import given, strategies as st
assert "attrs" not in sys.modules
@given(st.integers() | st.floats() | st.sampled_from(["a", "b"]))
def test_no_attrs_import(x):
assert "attrs" not in sys.modules
"""
)
# don't load pytest plugins, which might transitively import attrs
result = testdir.runpytest(test_path, "--disable-plugin-autoload")
result.assert_outcomes(passed=1, failed=0)
|