Debian Codegraph @

hypothesis-python/tests/nocover/test_build_signature.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 from inspect import signature
12 from typing import get_type_hints
13
14 from hypothesis import given, strategies as st
15
16 from tests.common.debug import find_any
17
18
19 def use_this_signature(self, a: int, b: list | None = None, *, x: float, y: str):
20 pass
21
22
23 class Model:
24 # Emulates the implementation of Pydantic models. See e.g.
25 # https://github.com/timothycrosley/hypothesis-auto/issues/10
26 __annotations__ = get_type_hints(use_this_signature)
27 __signature__ = signature(use_this_signature)
28
29 def __init__(self, **kwargs):
30 # Check that we're being called with the expected arguments
31 assert set(kwargs) == {"a", "x", "y"}
32 assert isinstance(kwargs["a"], int)
33 assert isinstance(kwargs["x"], float)
34 assert isinstance(kwargs["y"], str)
35
36
37 @given(st.builds(Model))
38 def test_builds_uses_signature_attribute(val):
39 assert isinstance(val, Model)
40
41
42 class ModelForFromType(Model):
43 def __init__(self, **kwargs):
44 assert set(kwargs) == {"a", "b", "x", "y"}
45 self.b = kwargs["b"]
46 assert self.b is None or isinstance(self.b, list)
47
48
49 @given(st.from_type(ModelForFromType))
50 def test_from_type_uses_signature_attribute(val):
51 assert isinstance(val, ModelForFromType)
52
53
54 def test_from_type_can_be_default_or_annotation():
55 find_any(st.from_type(ModelForFromType), lambda m: m.b is None)
56 find_any(st.from_type(ModelForFromType), lambda m: isinstance(m.b, list))
57
58
59 def use_annotations(
60 self, test_a: int, test_b: str | None = None, *, test_x: float, test_y: str
61 ):
62 pass
63
64
65 def use_signature(
66 self, testA: int, testB: str | None = None, *, testX: float, testY: list[str]
67 ):
68 pass
69
70
71 class ModelWithAlias:
72 __annotations__ = get_type_hints(use_annotations)
73 __signature__ = signature(use_signature)
74
75 def __init__(self, **kwargs):
76 # Check that we're being called with the expected arguments
77 assert set(kwargs) == {"testA", "testX", "testY"}
78 assert isinstance(kwargs["testA"], int)
79 assert isinstance(kwargs["testX"], float)
80 assert isinstance(kwargs["testY"], list)
81 assert all(isinstance(elem, str) for elem in kwargs["testY"])
82
83
84 @given(st.builds(ModelWithAlias))
85 def test_build_using_different_signature_and_annotations(val):
86 assert isinstance(val, ModelWithAlias)
87
88
89 def use_bad_signature(self, testA: 1, *, testX: float):
90 pass
91
92
93 class ModelWithBadAliasSignature:
94 __annotations__ = get_type_hints(use_annotations)
95 __signature__ = signature(use_bad_signature)
96
97 def __init__(self, **kwargs):
98 assert set(kwargs) == {"testX"}
99 assert isinstance(kwargs["testX"], float)
100
101
102 @given(st.builds(ModelWithBadAliasSignature))
103 def test_build_with_non_types_in_signature(val):
104 assert isinstance(val, ModelWithBadAliasSignature)
105
106
107 class UnconventionalSignature:
108 def __init__(x: int = 0, self: bool = True): # noqa
109 assert not isinstance(x, int)
110 x.self = self
111
112
113 def test_build_in_from_type_with_self_named_something_else():
114 find_any(st.from_type(UnconventionalSignature), lambda x: x.self is True)
115 find_any(st.from_type(UnconventionalSignature), lambda x: x.self is False)