Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fe16e5a

Browse files
committedMay 28, 2023
Move both async generatrs into common iterators module
1 parent ac9d08f commit fe16e5a

File tree

5 files changed

+35
-32
lines changed

5 files changed

+35
-32
lines changed
 

‎src/graphql/execution/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
FormattedIncrementalResult,
3333
Middleware,
3434
)
35-
from .map_async_iterable import map_async_iterable
35+
from .iterators import map_async_iterable
3636
from .middleware import MiddlewareManager
3737
from .values import get_argument_values, get_directive_values, get_variable_values
3838

‎src/graphql/execution/execute.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@
6969
is_object_type,
7070
)
7171
from .collect_fields import FieldsAndPatches, collect_fields, collect_subfields
72-
from .flatten_async_iterable import flatten_async_iterable
73-
from .map_async_iterable import map_async_iterable
72+
from .iterators import flatten_async_iterable, map_async_iterable
7473
from .middleware import MiddlewareManager
7574
from .values import get_argument_values, get_directive_values, get_variable_values
7675

‎src/graphql/execution/flatten_async_iterable.py ‎src/graphql/execution/iterators.py

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
from typing import AsyncGenerator, AsyncIterable, TypeVar, Union
1+
from __future__ import annotations # Python < 3.10
2+
3+
from typing import (
4+
Any,
5+
AsyncGenerator,
6+
AsyncIterable,
7+
Awaitable,
8+
Callable,
9+
TypeVar,
10+
Union,
11+
)
212

313

414
try:
@@ -18,7 +28,7 @@ async def aclosing(thing):
1828

1929
AsyncIterableOrGenerator = Union[AsyncGenerator[T, None], AsyncIterable[T]]
2030

21-
__all__ = ["flatten_async_iterable"]
31+
__all__ = ["flatten_async_iterable", "map_async_iterable"]
2232

2333

2434
async def flatten_async_iterable(
@@ -34,3 +44,23 @@ async def flatten_async_iterable(
3444
async with aclosing(sub_iterator) as items: # type: ignore
3545
async for item in items:
3646
yield item
47+
48+
49+
async def map_async_iterable(
50+
iterable: AsyncIterable[T], callback: Callable[[T], Awaitable[Any]]
51+
) -> None:
52+
"""Map an AsyncIterable over a callback function.
53+
54+
Given an AsyncIterable and an async callback callable, return an AsyncGenerator
55+
which produces values mapped via calling the callback.
56+
If the inner iterator supports an `aclose()` method, it will be called when
57+
the generator finishes or closes.
58+
"""
59+
60+
aiter = iterable.__aiter__()
61+
try:
62+
async for element in aiter:
63+
yield await callback(element)
64+
finally:
65+
if hasattr(aiter, "aclose"):
66+
await aiter.aclose()

‎src/graphql/execution/map_async_iterable.py

-26
This file was deleted.

‎tests/execution/test_flatten_async_iterable.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from pytest import mark, raises
44

5-
from graphql.execution.flatten_async_iterable import flatten_async_iterable
5+
from graphql.execution.iterators import flatten_async_iterable
66

77

88
try: # pragma: no cover

0 commit comments

Comments
 (0)
Please sign in to comment.