-
Notifications
You must be signed in to change notification settings - Fork 575
/
test_type_lookup.py
460 lines (344 loc) · 12.7 KB
/
test_type_lookup.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# 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 abc
import enum
from inspect import Parameter as P, Signature
from typing import Callable, Dict, Generic, List, Sequence, TypeVar, Union
import pytest
from hypothesis import given, infer, settings, strategies as st
from hypothesis.errors import (
HypothesisDeprecationWarning,
InvalidArgument,
ResolutionFailed,
SmallSearchSpaceWarning,
)
from hypothesis.internal.compat import get_type_hints
from hypothesis.internal.reflection import get_pretty_function_description
from hypothesis.strategies._internal import types
from hypothesis.strategies._internal.types import _global_type_lookup
from hypothesis.strategies._internal.utils import _strategies
from tests.common.debug import assert_all_examples, find_any
from tests.common.utils import fails_with, temp_registered
# Build a set of all types output by core strategies
blocklist = {
"builds",
"data",
"deferred",
"from_regex",
"from_type",
"ip_addresses",
"iterables",
"just",
"nothing",
"one_of",
"permutations",
"random_module",
"randoms",
"recursive",
"runner",
"sampled_from",
"shared",
"timezone_keys",
"timezones",
}
assert set(_strategies).issuperset(blocklist), blocklist.difference(_strategies)
types_with_core_strat = set()
for thing in (
getattr(st, name)
for name in sorted(_strategies)
if name in dir(st) and name not in blocklist
):
for n in range(3):
try:
ex = thing(*([st.nothing()] * n)).example()
types_with_core_strat.add(type(ex))
break
except (TypeError, InvalidArgument, HypothesisDeprecationWarning):
continue
def test_generic_sequence_of_integers_may_be_lists_or_bytes():
strat = st.from_type(Sequence[int])
find_any(strat, lambda x: isinstance(x, bytes))
find_any(strat, lambda x: isinstance(x, list))
@pytest.mark.parametrize("typ", sorted(types_with_core_strat, key=str))
def test_resolve_core_strategies(typ):
@given(st.from_type(typ))
def inner(ex):
assert isinstance(ex, typ)
inner()
def test_lookup_knows_about_all_core_strategies():
cannot_lookup = types_with_core_strat - set(types._global_type_lookup)
assert not cannot_lookup
def test_lookup_keys_are_types():
with pytest.raises(InvalidArgument):
st.register_type_strategy("int", st.integers())
assert "int" not in types._global_type_lookup
@pytest.mark.parametrize(
"typ, not_a_strategy",
[
(int, 42), # Values must be strategies
# Can't register NotImplemented directly, even though strategy functions
# can return it.
(int, NotImplemented),
],
)
def test_lookup_values_are_strategies(typ, not_a_strategy):
with pytest.raises(InvalidArgument):
st.register_type_strategy(typ, not_a_strategy)
assert not_a_strategy not in types._global_type_lookup.values()
@pytest.mark.parametrize("typ", sorted(types_with_core_strat, key=str))
def test_lookup_overrides_defaults(typ):
sentinel = object()
with temp_registered(typ, st.just(sentinel)):
assert st.from_type(typ).example() is sentinel
assert st.from_type(typ).example() is not sentinel
class ParentUnknownType:
pass
class UnknownType(ParentUnknownType):
def __init__(self, arg):
pass
def test_custom_type_resolution_fails_without_registering():
fails = st.from_type(UnknownType)
with pytest.raises(ResolutionFailed):
fails.example()
def test_custom_type_resolution():
sentinel = object()
with temp_registered(UnknownType, st.just(sentinel)):
assert st.from_type(UnknownType).example() is sentinel
# Also covered by registration of child class
assert st.from_type(ParentUnknownType).example() is sentinel
def test_custom_type_resolution_with_function():
sentinel = object()
with temp_registered(UnknownType, lambda _: st.just(sentinel)):
assert st.from_type(UnknownType).example() is sentinel
assert st.from_type(ParentUnknownType).example() is sentinel
def test_custom_type_resolution_with_function_non_strategy():
with temp_registered(UnknownType, lambda _: None):
with pytest.raises(ResolutionFailed):
st.from_type(UnknownType).example()
with pytest.raises(ResolutionFailed):
st.from_type(ParentUnknownType).example()
@pytest.mark.parametrize("strategy_returned", [True, False])
def test_conditional_type_resolution_with_function(strategy_returned):
sentinel = object()
def resolve_strategy(thing):
assert thing == UnknownType
if strategy_returned:
return st.just(sentinel)
return NotImplemented
with temp_registered(UnknownType, resolve_strategy):
if strategy_returned:
assert st.from_type(UnknownType).example() is sentinel
else:
with pytest.raises(ResolutionFailed):
st.from_type(UnknownType).example()
def test_errors_if_generic_resolves_empty():
with temp_registered(UnknownType, lambda _: st.nothing()):
fails_1 = st.from_type(UnknownType)
with pytest.raises(ResolutionFailed):
fails_1.example()
fails_2 = st.from_type(ParentUnknownType)
with pytest.raises(ResolutionFailed):
fails_2.example()
def test_cannot_register_empty():
# Cannot register and did not register
with pytest.raises(InvalidArgument):
st.register_type_strategy(UnknownType, st.nothing())
fails = st.from_type(UnknownType)
with pytest.raises(ResolutionFailed):
fails.example()
assert UnknownType not in types._global_type_lookup
def test_pulic_interface_works():
st.from_type(int).example()
fails = st.from_type("not a type or annotated function")
with pytest.raises(InvalidArgument):
fails.example()
@pytest.mark.parametrize("infer_token", [infer, ...])
def test_given_can_infer_from_manual_annotations(infer_token):
# Editing annotations before decorating is hilariously awkward, but works!
def inner(a):
assert isinstance(a, int)
inner.__annotations__ = {"a": int}
given(a=infer_token)(inner)()
class EmptyEnum(enum.Enum):
pass
@fails_with(InvalidArgument)
@given(st.from_type(EmptyEnum))
def test_error_if_enum_is_empty(x):
pass
class BrokenClass:
__init__ = "Hello!"
def test_uninspectable_builds():
with pytest.raises(TypeError, match="object is not callable"):
st.builds(BrokenClass).example()
def test_uninspectable_from_type():
with pytest.raises(TypeError, match="object is not callable"):
with pytest.warns(SmallSearchSpaceWarning):
st.from_type(BrokenClass).example()
def _check_instances(t):
# See https://github.com/samuelcolvin/pydantic/discussions/2508
return (
t.__module__ != "typing"
and t.__name__ != "ByteString"
and not t.__module__.startswith("pydantic")
)
@pytest.mark.parametrize(
"typ", sorted((x for x in _global_type_lookup if _check_instances(x)), key=str)
)
@given(data=st.data())
def test_can_generate_from_all_registered_types(data, typ):
value = data.draw(st.from_type(typ), label="value")
assert isinstance(value, typ)
T = TypeVar("T")
class MyGeneric(Generic[T]):
def __init__(self, arg: T) -> None:
self.arg = arg
class Lines(Sequence[str]):
"""Represent a sequence of text lines.
It turns out that resolving a class which inherits from a parametrised generic
type is... tricky. See https://github.com/HypothesisWorks/hypothesis/issues/2951
"""
class SpecificDict(Dict[int, int]):
pass
def using_generic(instance: MyGeneric[T]) -> T:
return instance.arg
def using_concrete_generic(instance: MyGeneric[int]) -> int:
return instance.arg
def test_generic_origin_empty():
with pytest.raises(ResolutionFailed):
find_any(st.builds(using_generic))
def test_issue_2951_regression():
lines_strat = st.builds(Lines, lines=st.lists(st.text()))
with temp_registered(Lines, lines_strat):
assert st.from_type(Lines) == lines_strat
# Now let's test that the strategy for ``Sequence[int]`` did not
# change just because we registered a strategy for ``Lines``:
expected = "one_of(binary(), lists(integers()))"
assert repr(st.from_type(Sequence[int])) == expected
def test_issue_2951_regression_two_params():
map_strat = st.builds(SpecificDict, st.dictionaries(st.integers(), st.integers()))
expected = repr(st.from_type(Dict[int, int]))
with temp_registered(SpecificDict, map_strat):
assert st.from_type(SpecificDict) == map_strat
assert expected == repr(st.from_type(Dict[int, int]))
@pytest.mark.parametrize(
"generic",
(
Union[str, int],
Sequence[Sequence[int]],
MyGeneric[str],
Callable[..., str],
Callable[[int], str],
),
ids=repr,
)
@pytest.mark.parametrize("strategy", [st.none(), lambda _: st.none()])
def test_generic_origin_with_type_args(generic, strategy):
with pytest.raises(InvalidArgument):
st.register_type_strategy(generic, strategy)
assert generic not in types._global_type_lookup
@pytest.mark.parametrize(
"generic",
(
Callable,
List,
Sequence,
# you can register types with all generic parameters
List[T],
Sequence[T],
# User-defined generics should also work
MyGeneric,
MyGeneric[T],
),
)
def test_generic_origin_without_type_args(generic):
with temp_registered(generic, st.just("example")):
pass
@pytest.mark.parametrize(
"strat, type_",
[
(st.from_type, MyGeneric[T]),
(st.from_type, MyGeneric[int]),
(st.from_type, MyGeneric),
(st.builds, using_generic),
(st.builds, using_concrete_generic),
],
ids=get_pretty_function_description,
)
def test_generic_origin_from_type(strat, type_):
with temp_registered(MyGeneric, st.builds(MyGeneric)):
find_any(strat(type_))
def test_generic_origin_concrete_builds():
with temp_registered(MyGeneric, st.builds(MyGeneric, st.integers())):
assert_all_examples(
st.builds(using_generic), lambda example: isinstance(example, int)
)
class AbstractFoo(abc.ABC):
def __init__(self, x): # noqa: B027
pass
@abc.abstractmethod
def qux(self):
pass
class ConcreteFoo1(AbstractFoo):
# Can't resolve this one due to unannotated `x` param
def qux(self):
pass
class ConcreteFoo2(AbstractFoo):
def __init__(self, x: int):
pass
def qux(self):
pass
@given(st.from_type(AbstractFoo))
def test_gen_abstract(foo):
# This requires that we correctly checked which of the subclasses
# could be resolved, rather than unconditionally using all of them.
assert isinstance(foo, ConcreteFoo2)
class AbstractBar(abc.ABC):
def __init__(self, x): # noqa: B027
pass
@abc.abstractmethod
def qux(self):
pass
class ConcreteBar(AbstractBar):
def qux(self):
pass
def test_abstract_resolver_fallback():
# We create our distinct strategies for abstract and concrete types
gen_abstractbar = st.from_type(AbstractBar)
gen_concretebar = st.builds(ConcreteBar, x=st.none())
assert gen_abstractbar != gen_concretebar
# And trying to generate an instance of the abstract type fails,
# UNLESS the concrete type is currently resolvable
with pytest.raises(ResolutionFailed):
gen_abstractbar.example()
with temp_registered(ConcreteBar, gen_concretebar):
gen = gen_abstractbar.example()
with pytest.raises(ResolutionFailed):
gen_abstractbar.example()
# which in turn means we resolve to the concrete subtype.
assert isinstance(gen, ConcreteBar)
def _one_arg(x: int):
assert isinstance(x, int)
def _multi_arg(x: int, y: str):
assert isinstance(x, int)
assert isinstance(y, str)
def _kwd_only(*, y: str):
assert isinstance(y, str)
def _pos_and_kwd_only(x: int, *, y: str):
assert isinstance(x, int)
assert isinstance(y, str)
@pytest.mark.parametrize("func", [_one_arg, _multi_arg, _kwd_only, _pos_and_kwd_only])
def test_infer_all(func):
# tests @given(...) against various signatures
settings(max_examples=1)(given(...))(func)()
def test_does_not_add_param_empty_to_type_hints():
def f(x):
pass
f.__signature__ = Signature([P("y", P.KEYWORD_ONLY)], return_annotation=None)
assert get_type_hints(f) == {}