|
19 | 19 | import typing |
20 | 20 | import warnings |
21 | 21 | from collections.abc import ( |
| 22 | + AsyncGenerator, |
| 23 | + AsyncIterable, |
| 24 | + AsyncIterator, |
22 | 25 | Callable, |
23 | 26 | Collection, |
| 27 | + Generator, |
24 | 28 | Hashable, |
25 | 29 | Iterable, |
| 30 | + Iterator, |
26 | 31 | Mapping, |
27 | 32 | Sequence, |
28 | 33 | ) |
29 | 34 | from contextvars import ContextVar |
30 | 35 | from decimal import Context, Decimal, localcontext |
31 | 36 | from fractions import Fraction |
32 | 37 | from functools import reduce |
33 | | -from inspect import Parameter, Signature, isabstract, isclass |
| 38 | +from inspect import ( |
| 39 | + Parameter, |
| 40 | + Signature, |
| 41 | + isabstract, |
| 42 | + isasyncgenfunction, |
| 43 | + isclass, |
| 44 | + iscoroutinefunction, |
| 45 | + isgeneratorfunction, |
| 46 | +) |
34 | 47 | from re import Pattern |
35 | 48 | from types import EllipsisType, FunctionType, GenericAlias |
36 | 49 | from typing import ( |
@@ -2630,12 +2643,49 @@ def _functions(*, like, returns, pure): |
2630 | 2643 | "The first argument to functions() must be a callable to imitate, " |
2631 | 2644 | f"but got non-callable like={nicerepr(like)!r}" |
2632 | 2645 | ) |
| 2646 | + if pure and ( |
| 2647 | + iscoroutinefunction(like) |
| 2648 | + or isgeneratorfunction(like) |
| 2649 | + or isasyncgenfunction(like) |
| 2650 | + ): |
| 2651 | + raise InvalidArgument( |
| 2652 | + f"pure=True is invalid for like={nicerepr(like)!r}, because async " |
| 2653 | + "functions are for non-deterministic IO and generators are consumed " |
| 2654 | + "by iteration, so returning a cached value makes no sense" |
| 2655 | + ) |
| 2656 | + is_gen = isgeneratorfunction(like) or isasyncgenfunction(like) |
2633 | 2657 | if returns in (None, ...): |
2634 | | - # Passing `None` has never been *documented* as working, but it still |
2635 | | - # did from May 2020 to Jan 2022 so we'll avoid breaking it without cause. |
2636 | 2658 | hints = get_type_hints(like) |
2637 | | - returns = from_type(hints.get("return", type(None))) |
| 2659 | + if is_gen: |
| 2660 | + # The return annotation describes the iterator, so e.g. yield |
| 2661 | + # integers for `-> Iterator[int]` or `-> AsyncIterator[int]`. |
| 2662 | + allowed = ( |
| 2663 | + (AsyncIterator, AsyncIterable, AsyncGenerator) |
| 2664 | + if isasyncgenfunction(like) |
| 2665 | + else (Iterator, Iterable, Generator) |
| 2666 | + ) |
| 2667 | + ret = hints.get("return") |
| 2668 | + # normalize eg Iterator[bool] to Iterator while keeping Iterator as Iterator. |
| 2669 | + kind = get_origin(ret) or ret |
| 2670 | + if ret is not None and kind not in allowed: |
| 2671 | + options = ", ".join(t.__name__ for t in allowed) |
| 2672 | + raise InvalidArgument( |
| 2673 | + f"Cannot infer the yield type of like={nicerepr(like)!r} " |
| 2674 | + f"from its return annotation {ret!r}. Expected one of " |
| 2675 | + f"{options}. Alternatively, pass returns= to specify the yield type " |
| 2676 | + "explicitly." |
| 2677 | + ) |
| 2678 | + args = get_args(ret) |
| 2679 | + returns = from_type(args[0]) if args else none() |
| 2680 | + else: |
| 2681 | + # Passing `None` has never been *documented* as working, but it |
| 2682 | + # still did from May 2020 to Jan 2022 so we'll avoid breaking it |
| 2683 | + # without cause. |
| 2684 | + returns = from_type(hints.get("return", type(None))) |
2638 | 2685 | check_strategy(returns, "returns") |
| 2686 | + if is_gen: |
| 2687 | + # Generated generator functions draw a list of values to yield up front. |
| 2688 | + returns = lists(returns) |
2639 | 2689 | return FunctionStrategy(like, returns, pure) |
2640 | 2690 |
|
2641 | 2691 |
|
@@ -2680,6 +2730,20 @@ def functions(*, like=lambda: None, returns=..., pure=False): |
2680 | 2730 | strategy. If ``returns`` is not passed, we attempt to infer a strategy |
2681 | 2731 | from the return-type annotation if present, falling back to :func:`~none`. |
2682 | 2732 |
|
| 2733 | + If ``like`` is an async function, a generator function, or an async |
| 2734 | + generator function, the generated function will be of the same kind. |
| 2735 | + Awaiting a generated async function returns a value drawn from |
| 2736 | + ``returns``, while generated generator functions draw a list of |
| 2737 | + values from ``returns`` up front and then yield from it - so a |
| 2738 | + return-type annotation like ``Iterator[int]`` or ``AsyncIterator[int]`` |
| 2739 | + means we infer ``returns=integers()``. ``pure=True`` is only |
| 2740 | + supported when ``like`` is a plain function. |
| 2741 | +
|
| 2742 | + Generated async functions and async generators follow Trio-style |
| 2743 | + checkpoint semantics, using :pypi:`anyio` or :pypi:`sniffio` if |
| 2744 | + imported to find the right way to checkpoint, and falling back to |
| 2745 | + :mod:`asyncio` otherwise. |
| 2746 | +
|
2683 | 2747 | If ``pure=True``, all arguments passed to the generated function must be |
2684 | 2748 | hashable, and if passed identical arguments the original return value will |
2685 | 2749 | be returned again - *not* regenerated, so beware mutable values. |
@@ -2711,6 +2775,20 @@ def functions( |
2711 | 2775 | strategy. If ``returns`` is not passed, we attempt to infer a strategy |
2712 | 2776 | from the return-type annotation if present, falling back to :func:`~none`. |
2713 | 2777 |
|
| 2778 | + If ``like`` is an async function, a generator function, or an async |
| 2779 | + generator function, the generated function will be of the same kind. |
| 2780 | + Awaiting a generated async function returns a value drawn from |
| 2781 | + ``returns``, while generated generator functions draw a list of |
| 2782 | + values from ``returns`` up front and then yield from it - so a |
| 2783 | + return-type annotation like ``Iterator[int]`` or ``AsyncIterator[int]`` |
| 2784 | + means we infer ``returns=integers()``. ``pure=True`` is only |
| 2785 | + supported when ``like`` is a plain function. |
| 2786 | +
|
| 2787 | + Generated async functions and async generators follow Trio-style |
| 2788 | + checkpoint semantics, using :pypi:`anyio` or :pypi:`sniffio` if |
| 2789 | + imported to find the right way to checkpoint, and falling back to |
| 2790 | + :mod:`asyncio` otherwise. |
| 2791 | +
|
2714 | 2792 | If ``pure=True``, all arguments passed to the generated function must be |
2715 | 2793 | hashable, and if passed identical arguments the original return value will |
2716 | 2794 | be returned again - *not* regenerated, so beware mutable values. |
|
0 commit comments