Debian Codegraph @

hypothesis-python/tests/cover/test_lookup_py37.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 collections
12 import collections.abc
13 import contextlib
14 import re
15
16 import pytest
17
18 from hypothesis import assume, given
19
20
21 class Elem:
22 pass
23
24
25 class Value:
26 pass
27
28
29 def check(t, ex):
30 assert isinstance(ex, t)
31 assert all(isinstance(e, Elem) for e in ex)
32 assume(ex)
33
34
35 @given(...)
36 def test_resolving_standard_tuple1_as_generic(x: tuple[Elem]):
37 check(tuple, x)
38
39
40 @given(...)
41 def test_resolving_standard_tuple2_as_generic(x: tuple[Elem, Elem]):
42 check(tuple, x)
43
44
45 @given(...)
46 def test_resolving_standard_tuple_variadic_as_generic(x: tuple[Elem, ...]):
47 check(tuple, x)
48
49
50 @given(...)
51 def test_resolving_standard_list_as_generic(x: list[Elem]):
52 check(list, x)
53
54
55 @given(...)
56 def test_resolving_standard_dict_as_generic(x: dict[Elem, Value]):
57 check(dict, x)
58 assert all(isinstance(e, Value) for e in x.values())
59
60
61 @given(...)
62 def test_resolving_standard_set_as_generic(x: set[Elem]):
63 check(set, x)
64
65
66 @given(...)
67 def test_resolving_standard_frozenset_as_generic(x: frozenset[Elem]):
68 check(frozenset, x)
69
70
71 @given(...)
72 def test_resolving_standard_deque_as_generic(x: collections.deque[Elem]):
73 check(collections.deque, x)
74
75
76 @given(...)
77 def test_resolving_standard_defaultdict_as_generic(
78 x: collections.defaultdict[Elem, Value],
79 ):
80 check(collections.defaultdict, x)
81 assert all(isinstance(e, Value) for e in x.values())
82
83
84 @given(...)
85 def test_resolving_standard_ordered_dict_as_generic(
86 x: collections.OrderedDict[Elem, Value],
87 ):
88 check(collections.OrderedDict, x)
89 assert all(isinstance(e, Value) for e in x.values())
90
91
92 @given(...)
93 def test_resolving_standard_counter_as_generic(x: collections.Counter[Elem]):
94 check(collections.Counter, x)
95 assume(any(x.values())) # Check that we generated at least one nonzero count
96
97
98 @given(...)
99 def test_resolving_standard_chainmap_as_generic(x: collections.ChainMap[Elem, Value]):
100 check(collections.ChainMap, x)
101 assert all(isinstance(e, Value) for e in x.values())
102
103
104 @given(...)
105 def test_resolving_standard_iterable_as_generic(x: collections.abc.Iterable[Elem]):
106 check(collections.abc.Iterable, x)
107
108
109 @given(...)
110 def test_resolving_standard_iterator_as_generic(x: collections.abc.Iterator[Elem]):
111 check(collections.abc.Iterator, x)
112
113
114 @given(...)
115 def test_resolving_standard_generator_as_generic(
116 x: collections.abc.Generator[Elem, None, Value],
117 ):
118 assert isinstance(x, collections.abc.Generator)
119 try:
120 while True:
121 e = next(x)
122 assert isinstance(e, Elem)
123 x.send(None) # The generators we create don't check the send type
124 except StopIteration as stop:
125 assert isinstance(stop.value, Value)
126
127
128 @given(...)
129 def test_resolving_standard_reversible_as_generic(x: collections.abc.Reversible[Elem]):
130 check(collections.abc.Reversible, x)
131
132
133 @given(...)
134 def test_resolving_standard_container_as_generic(x: collections.abc.Container[Elem]):
135 check(collections.abc.Container, x)
136
137
138 @given(...)
139 def test_resolving_standard_collection_as_generic(x: collections.abc.Collection[Elem]):
140 check(collections.abc.Collection, x)
141
142
143 @given(...)
144 def test_resolving_standard_callable_ellipsis(x: collections.abc.Callable[..., Elem]):
145 assert isinstance(x, collections.abc.Callable)
146 assert callable(x)
147 # ... implies *args, **kwargs; as would any argument types
148 assert isinstance(x(), Elem)
149 assert isinstance(x(1, 2, 3, a=4, b=5, c=6), Elem)
150
151
152 @given(...)
153 def test_resolving_standard_callable_no_args(x: collections.abc.Callable[[], Elem]):
154 assert isinstance(x, collections.abc.Callable)
155 assert callable(x)
156 # [] implies that no arguments are accepted
157 assert isinstance(x(), Elem)
158 with pytest.raises(TypeError):
159 x(1)
160 with pytest.raises(TypeError):
161 x(a=1)
162
163
164 @given(...)
165 def test_resolving_standard_collections_set_as_generic(x: collections.abc.Set[Elem]):
166 check(collections.abc.Set, x)
167
168
169 @given(...)
170 def test_resolving_standard_collections_mutableset_as_generic(
171 x: collections.abc.MutableSet[Elem],
172 ):
173 check(collections.abc.MutableSet, x)
174
175
176 @given(...)
177 def test_resolving_standard_mapping_as_generic(x: collections.abc.Mapping[Elem, Value]):
178 check(collections.abc.Mapping, x)
179 assert all(isinstance(e, Value) for e in x.values())
180
181
182 @given(...)
183 def test_resolving_standard_mutable_mapping_as_generic(
184 x: collections.abc.MutableMapping[Elem, Value],
185 ):
186 check(collections.abc.MutableMapping, x)
187 assert all(isinstance(e, Value) for e in x.values())
188
189
190 @given(...)
191 def test_resolving_standard_sequence_as_generic(x: collections.abc.Sequence[Elem]):
192 check(collections.abc.Sequence, x)
193
194
195 @given(...)
196 def test_resolving_standard_mutable_sequence_as_generic(
197 x: collections.abc.MutableSequence[Elem],
198 ):
199 check(collections.abc.MutableSequence, x)
200
201
202 @given(...)
203 def test_resolving_standard_keysview_as_generic(x: collections.abc.KeysView[Elem]):
204 check(collections.abc.KeysView, x)
205
206
207 @given(...)
208 def test_resolving_standard_itemsview_as_generic(
209 x: collections.abc.ItemsView[Elem, Value],
210 ):
211 assert isinstance(x, collections.abc.ItemsView)
212 assert all(isinstance(e, Elem) and isinstance(v, Value) for e, v in x)
213 assume(x)
214
215
216 @given(...)
217 def test_resolving_standard_valuesview_as_generic(x: collections.abc.ValuesView[Elem]):
218 check(collections.abc.ValuesView, x)
219
220
221 # Weird interaction with fixes in PR #2952
222 #
223 # 2025-08-11: this gets even weirder with 3.14, where memoryview is in fact a
224 # context manager, and so gets resolved for AbstractContextManager[Elem] here.
225 # (But then errors during generation, because Elem does not implement __buffer__).
226 @pytest.mark.xfail
227 @given(...)
228 def test_resolving_standard_contextmanager_as_generic(
229 x: contextlib.AbstractContextManager[Elem],
230 ):
231 assert isinstance(x, contextlib.AbstractContextManager)
232
233
234 @given(...)
235 def test_resolving_standard_re_match_bytes_as_generic(x: re.Match[bytes]):
236 assert isinstance(x, re.Match)
237 assert isinstance(x[0], bytes)
238
239
240 @given(...)
241 def test_resolving_standard_re_match_str_as_generic(x: re.Match[str]):
242 assert isinstance(x, re.Match)
243 assert isinstance(x[0], str)
244
245
246 @given(...)
247 def test_resolving_standard_re_pattern_bytes_as_generic(x: re.Pattern[bytes]):
248 assert isinstance(x, re.Pattern)
249 assert isinstance(x.pattern, bytes)
250
251
252 @given(...)
253 def test_resolving_standard_re_pattern_str_as_generic(x: re.Pattern[str]):
254 assert isinstance(x, re.Pattern)
255 assert isinstance(x.pattern, str)