|
8 | 8 | # v. 2.0. If a copy of the MPL was not distributed with this file, You can |
9 | 9 | # obtain one at https://mozilla.org/MPL/2.0/. |
10 | 10 |
|
11 | | -from collections.abc import AsyncIterator, Generator, Iterator |
| 11 | +from collections.abc import ( |
| 12 | + AsyncIterator, |
| 13 | + Generator, |
| 14 | + Iterator, |
| 15 | +) |
12 | 16 | from inspect import ( |
13 | 17 | isasyncgenfunction, |
14 | 18 | iscoroutinefunction, |
|
24 | 28 | from hypothesis.reporting import with_reporter |
25 | 29 | from hypothesis.strategies import booleans, functions, integers |
26 | 30 |
|
27 | | -from tests.common.debug import check_can_generate_examples |
| 31 | +from tests.common.debug import assert_all_examples, check_can_generate_examples |
28 | 32 | from tests.common.utils import capture_out |
29 | 33 |
|
30 | 34 |
|
@@ -473,3 +477,70 @@ def t(f): |
473 | 477 | def test_pure_is_invalid_except_for_plain_functions(like): |
474 | 478 | with pytest.raises(InvalidArgument, match="pure=True is invalid"): |
475 | 479 | check_can_generate_examples(functions(like=like, pure=True)) |
| 480 | + |
| 481 | + |
| 482 | +def make_gen_like(annotation, *, is_async=False): |
| 483 | + prefix = "async " if is_async else "" |
| 484 | + # pass our globals so we can reference things like the module-level IteratorSubclass |
| 485 | + # in test strings |
| 486 | + namespace = dict(globals()) |
| 487 | + exec( |
| 488 | + f"from collections.abc import *\n{prefix}def like(a) -> {annotation}:\n yield", |
| 489 | + namespace, |
| 490 | + ) |
| 491 | + return namespace["like"] |
| 492 | + |
| 493 | + |
| 494 | +class IteratorSubclass(Iterator[bool]): |
| 495 | + def __next__(self): |
| 496 | + raise StopIteration |
| 497 | + |
| 498 | + |
| 499 | +@pytest.mark.parametrize( |
| 500 | + "annotation", |
| 501 | + ["Iterator[bool]", "Iterable[bool]", "Generator[bool, None, None]"], |
| 502 | +) |
| 503 | +def test_generator_functions_accept_any_iterator_like_annotation(annotation): |
| 504 | + assert_all_examples( |
| 505 | + functions(like=make_gen_like(annotation)), |
| 506 | + lambda f: all(isinstance(value, bool) for value in f(1)), |
| 507 | + ) |
| 508 | + |
| 509 | + |
| 510 | +@pytest.mark.parametrize( |
| 511 | + "annotation", |
| 512 | + ["AsyncIterator[bool]", "AsyncIterable[bool]", "AsyncGenerator[bool, None]"], |
| 513 | +) |
| 514 | +def test_async_generator_functions_accept_any_async_iterator_like_annotation( |
| 515 | + annotation, |
| 516 | +): |
| 517 | + assert_all_examples( |
| 518 | + functions(like=make_gen_like(annotation, is_async=True)), |
| 519 | + lambda f: all(isinstance(value, bool) for value in collect_async_gen(f(1))), |
| 520 | + ) |
| 521 | + |
| 522 | + |
| 523 | +@pytest.mark.parametrize( |
| 524 | + "annotation", |
| 525 | + [ |
| 526 | + "bool", |
| 527 | + "tuple[bool, str]", |
| 528 | + "list[bool]", |
| 529 | + "IteratorSubclass", |
| 530 | + "AsyncIterator[bool]", |
| 531 | + ], |
| 532 | +) |
| 533 | +def test_generator_functions_reject_other_annotations(annotation): |
| 534 | + with pytest.raises(InvalidArgument, match="Cannot infer the yield type"): |
| 535 | + check_can_generate_examples(functions(like=make_gen_like(annotation))) |
| 536 | + |
| 537 | + |
| 538 | +@pytest.mark.parametrize( |
| 539 | + "annotation", |
| 540 | + ["bool", "tuple[bool, str]", "list[bool]", "Iterator[bool]"], |
| 541 | +) |
| 542 | +def test_async_generator_functions_reject_other_annotations(annotation): |
| 543 | + with pytest.raises(InvalidArgument, match="Cannot infer the yield type"): |
| 544 | + check_can_generate_examples( |
| 545 | + functions(like=make_gen_like(annotation, is_async=True)) |
| 546 | + ) |
0 commit comments