Skip to content

Commit 47bb9d1

Browse files
authored
Merge pull request #4833 from Zac-HD/claude/hypothesis-issue-4149-16nmgw
Teach st.functions() to create async and generator functions
2 parents b2b77ec + 13ccc11 commit 47bb9d1

4 files changed

Lines changed: 414 additions & 11 deletions

File tree

‎hypothesis/RELEASE.rst‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
RELEASE_TYPE: minor
2+
3+
:func:`~hypothesis.strategies.functions` now generates functions of the same
4+
kind as the ``like=`` argument, so that async functions, generator functions,
5+
and async generator functions can be imitated as well as plain functions
6+
(:issue:`4149`). ``pure=True`` is only supported for plain functions.
7+
8+
Generated generator functions yield a list of values drawn from ``returns``,
9+
inferring e.g. ``returns=integers()`` from an ``Iterator[int]`` or
10+
``AsyncIterator[int]`` return-type annotation, and generated async functions
11+
follow Trio-style checkpoint semantics.

‎hypothesis/src/hypothesis/strategies/_internal/core.py‎

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,31 @@
1919
import typing
2020
import warnings
2121
from collections.abc import (
22+
AsyncGenerator,
23+
AsyncIterable,
24+
AsyncIterator,
2225
Callable,
2326
Collection,
27+
Generator,
2428
Hashable,
2529
Iterable,
30+
Iterator,
2631
Mapping,
2732
Sequence,
2833
)
2934
from contextvars import ContextVar
3035
from decimal import Context, Decimal, localcontext
3136
from fractions import Fraction
3237
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+
)
3447
from re import Pattern
3548
from types import EllipsisType, FunctionType, GenericAlias
3649
from typing import (
@@ -2630,12 +2643,49 @@ def _functions(*, like, returns, pure):
26302643
"The first argument to functions() must be a callable to imitate, "
26312644
f"but got non-callable like={nicerepr(like)!r}"
26322645
)
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)
26332657
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.
26362658
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)))
26382685
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)
26392689
return FunctionStrategy(like, returns, pure)
26402690

26412691

@@ -2680,6 +2730,20 @@ def functions(*, like=lambda: None, returns=..., pure=False):
26802730
strategy. If ``returns`` is not passed, we attempt to infer a strategy
26812731
from the return-type annotation if present, falling back to :func:`~none`.
26822732
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+
26832747
If ``pure=True``, all arguments passed to the generated function must be
26842748
hashable, and if passed identical arguments the original return value will
26852749
be returned again - *not* regenerated, so beware mutable values.
@@ -2711,6 +2775,20 @@ def functions(
27112775
strategy. If ``returns`` is not passed, we attempt to infer a strategy
27122776
from the return-type annotation if present, falling back to :func:`~none`.
27132777
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+
27142792
If ``pure=True``, all arguments passed to the generated function must be
27152793
hashable, and if passed identical arguments the original return value will
27162794
be returned again - *not* regenerated, so beware mutable values.

‎hypothesis/src/hypothesis/strategies/_internal/functions.py‎

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@
88
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
99
# obtain one at https://mozilla.org/MPL/2.0/.
1010

11-
from inspect import Parameter, Signature
11+
import sys
12+
from inspect import (
13+
Parameter,
14+
Signature,
15+
isasyncgenfunction,
16+
iscoroutinefunction,
17+
isgeneratorfunction,
18+
)
1219
from weakref import WeakKeyDictionary
1320

1421
from hypothesis.control import note, should_note
@@ -31,6 +38,22 @@
3138
can_vary = UniqueIdentifier("can_vary")
3239

3340

41+
async def _checkpoint(): # pragma: no cover # depends on installed frameworks
42+
# Generated async functions follow Trio-style checkpoint semantics, using
43+
# anyio or sniffio to find the right way to checkpoint if the user's async
44+
# framework might not be asyncio.
45+
if anyio := sys.modules.get("anyio"):
46+
await anyio.lowlevel.checkpoint()
47+
return
48+
if sniffio := sys.modules.get("sniffio"):
49+
if sniffio.current_async_library() == "trio":
50+
await sys.modules["trio"].lowlevel.checkpoint()
51+
return
52+
import asyncio # deferred to keep `import hypothesis` fast
53+
54+
await asyncio.sleep(0)
55+
56+
3457
class FunctionStrategy(SearchStrategy):
3558
def __init__(self, like, returns, pure):
3659
super().__init__()
@@ -76,11 +99,14 @@ def calc_is_empty(self, recur: RecurT) -> bool:
7699

77100
def do_draw(self, data):
78101
# If we know what the function returns, we show it as a constant lambda
79-
# instead of noting every call - the notes would be redundant.
80-
varies = self._constant is can_vary
102+
# instead of noting every call - the notes would be redundant. A
103+
# lambda is a poor description of an async function though, and
104+
# generator kinds are never constant, so this is for plain functions.
105+
varies = self._constant is can_vary or iscoroutinefunction(self.like)
81106

82-
@proxies(self.like)
83-
def inner(*args, **kwargs):
107+
def draw_value(args, kwargs):
108+
# `pure=True` is rejected for non-plain `like`s, so only the plain
109+
# `inner` below can ever take the caching branch.
84110
if data.frozen:
85111
raise InvalidState(
86112
f"This generated {nicerepr(self.like)} function can only "
@@ -104,6 +130,39 @@ def inner(*args, **kwargs):
104130
note(f"Called function: {rep} -> {val!r}")
105131
return val
106132

133+
# Define an inner function of the same kind as `like`, so that the
134+
# proxy (see `proxies`) is a coroutine, generator, or async-generator
135+
# function whenever `like` is. For generator kinds, the drawn value
136+
# is a list of values to yield.
137+
if iscoroutinefunction(self.like):
138+
139+
@proxies(self.like)
140+
async def inner(*args, **kwargs):
141+
value = draw_value(args, kwargs)
142+
await _checkpoint()
143+
return value
144+
145+
elif isasyncgenfunction(self.like):
146+
147+
@proxies(self.like)
148+
async def inner(*args, **kwargs):
149+
for value in draw_value(args, kwargs):
150+
await _checkpoint()
151+
yield value
152+
await _checkpoint()
153+
154+
elif isgeneratorfunction(self.like):
155+
156+
@proxies(self.like)
157+
def inner(*args, **kwargs):
158+
yield from draw_value(args, kwargs)
159+
160+
else:
161+
162+
@proxies(self.like)
163+
def inner(*args, **kwargs):
164+
return draw_value(args, kwargs)
165+
107166
if not varies:
108167
inner._repr_pretty_ = self._pretty_constant_function
109168
return inner

0 commit comments

Comments
 (0)