Debian Codegraph @

hypothesis-python/tests/attrs/test_attrs.py

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