-
Notifications
You must be signed in to change notification settings - Fork 575
/
test_lookup_py37.py
264 lines (184 loc) · 6.92 KB
/
test_lookup_py37.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# 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/.
from __future__ import annotations
import collections
import collections.abc
import contextlib
import re
import sys
import pytest
from hypothesis import assume, given
# On Python 3.7 and 3.8, `from __future__ import annotations` means
# that the syntax is supported; but the feature fails at runtime. On Python
# 3.9 and later, it should all work.
#
# For details, see https://www.python.org/dev/peps/pep-0585/
if sys.version_info < (3, 9):
pytestmark = pytest.mark.xfail(
raises=Exception, reason="Requires Python 3.9 (PEP 585) or later."
)
class Elem:
pass
class Value:
pass
def check(t, ex):
assert isinstance(ex, t)
assert all(isinstance(e, Elem) for e in ex)
assume(ex)
@given(...)
def test_resolving_standard_tuple1_as_generic(x: tuple[Elem]):
check(tuple, x)
@given(...)
def test_resolving_standard_tuple2_as_generic(x: tuple[Elem, Elem]):
check(tuple, x)
@given(...)
def test_resolving_standard_tuple_variadic_as_generic(x: tuple[Elem, ...]):
check(tuple, x)
@given(...)
def test_resolving_standard_list_as_generic(x: list[Elem]):
check(list, x)
@given(...)
def test_resolving_standard_dict_as_generic(x: dict[Elem, Value]):
check(dict, x)
assert all(isinstance(e, Value) for e in x.values())
@given(...)
def test_resolving_standard_set_as_generic(x: set[Elem]):
check(set, x)
@given(...)
def test_resolving_standard_frozenset_as_generic(x: frozenset[Elem]):
check(frozenset, x)
@given(...)
def test_resolving_standard_deque_as_generic(x: collections.deque[Elem]):
check(collections.deque, x)
@given(...)
def test_resolving_standard_defaultdict_as_generic(
x: collections.defaultdict[Elem, Value]
):
check(collections.defaultdict, x)
assert all(isinstance(e, Value) for e in x.values())
@given(...)
def test_resolving_standard_ordered_dict_as_generic(
x: collections.OrderedDict[Elem, Value]
):
check(collections.OrderedDict, x)
assert all(isinstance(e, Value) for e in x.values())
@given(...)
def test_resolving_standard_counter_as_generic(x: collections.Counter[Elem]):
check(collections.Counter, x)
assume(any(x.values())) # Check that we generated at least one nonzero count
@given(...)
def test_resolving_standard_chainmap_as_generic(x: collections.ChainMap[Elem, Value]):
check(collections.ChainMap, x)
assert all(isinstance(e, Value) for e in x.values())
@given(...)
def test_resolving_standard_iterable_as_generic(x: collections.abc.Iterable[Elem]):
check(collections.abc.Iterable, x)
@given(...)
def test_resolving_standard_iterator_as_generic(x: collections.abc.Iterator[Elem]):
check(collections.abc.Iterator, x)
@given(...)
def test_resolving_standard_generator_as_generic(
x: collections.abc.Generator[Elem, None, Value]
):
assert isinstance(x, collections.abc.Generator)
try:
while True:
e = next(x)
assert isinstance(e, Elem)
x.send(None) # The generators we create don't check the send type
except StopIteration as stop:
assert isinstance(stop.value, Value)
@given(...)
def test_resolving_standard_reversible_as_generic(x: collections.abc.Reversible[Elem]):
check(collections.abc.Reversible, x)
@given(...)
def test_resolving_standard_container_as_generic(x: collections.abc.Container[Elem]):
check(collections.abc.Container, x)
@given(...)
def test_resolving_standard_collection_as_generic(x: collections.abc.Collection[Elem]):
check(collections.abc.Collection, x)
@given(...)
def test_resolving_standard_callable_ellipsis(x: collections.abc.Callable[..., Elem]):
assert isinstance(x, collections.abc.Callable)
assert callable(x)
# ... implies *args, **kwargs; as would any argument types
assert isinstance(x(), Elem)
assert isinstance(x(1, 2, 3, a=4, b=5, c=6), Elem)
@given(...)
def test_resolving_standard_callable_no_args(x: collections.abc.Callable[[], Elem]):
assert isinstance(x, collections.abc.Callable)
assert callable(x)
# [] implies that no arguments are accepted
assert isinstance(x(), Elem)
with pytest.raises(TypeError):
x(1)
with pytest.raises(TypeError):
x(a=1)
@given(...)
def test_resolving_standard_collections_set_as_generic(x: collections.abc.Set[Elem]):
check(collections.abc.Set, x)
@given(...)
def test_resolving_standard_collections_mutableset_as_generic(
x: collections.abc.MutableSet[Elem],
):
check(collections.abc.MutableSet, x)
@given(...)
def test_resolving_standard_mapping_as_generic(x: collections.abc.Mapping[Elem, Value]):
check(collections.abc.Mapping, x)
assert all(isinstance(e, Value) for e in x.values())
@given(...)
def test_resolving_standard_mutable_mapping_as_generic(
x: collections.abc.MutableMapping[Elem, Value],
):
check(collections.abc.MutableMapping, x)
assert all(isinstance(e, Value) for e in x.values())
@given(...)
def test_resolving_standard_sequence_as_generic(x: collections.abc.Sequence[Elem]):
check(collections.abc.Sequence, x)
@given(...)
def test_resolving_standard_mutable_sequence_as_generic(
x: collections.abc.MutableSequence[Elem],
):
check(collections.abc.MutableSequence, x)
@given(...)
def test_resolving_standard_keysview_as_generic(x: collections.abc.KeysView[Elem]):
check(collections.abc.KeysView, x)
@given(...)
def test_resolving_standard_itemsview_as_generic(
x: collections.abc.ItemsView[Elem, Value]
):
assert isinstance(x, collections.abc.ItemsView)
assert all(isinstance(e, Elem) and isinstance(v, Value) for e, v in x)
assume(x)
@given(...)
def test_resolving_standard_valuesview_as_generic(x: collections.abc.ValuesView[Elem]):
check(collections.abc.ValuesView, x)
@pytest.mark.xfail # Weird interaction with fixes in PR #2952
@given(...)
def test_resolving_standard_contextmanager_as_generic(
x: contextlib.AbstractContextManager[Elem],
):
assert isinstance(x, contextlib.AbstractContextManager)
@given(...)
def test_resolving_standard_re_match_bytes_as_generic(x: re.Match[bytes]):
assert isinstance(x, re.Match)
assert isinstance(x[0], bytes)
@given(...)
def test_resolving_standard_re_match_str_as_generic(x: re.Match[str]):
assert isinstance(x, re.Match)
assert isinstance(x[0], str)
@given(...)
def test_resolving_standard_re_pattern_bytes_as_generic(x: re.Pattern[bytes]):
assert isinstance(x, re.Pattern)
assert isinstance(x.pattern, bytes)
@given(...)
def test_resolving_standard_re_pattern_str_as_generic(x: re.Pattern[str]):
assert isinstance(x, re.Pattern)
assert isinstance(x.pattern, str)