-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from mikenerone/mikenerone/safe_aclosing
Full support for all async iterables/iterators as inputs and sources (even if they don't have an `aclose()`)
- Loading branch information
Showing
20 changed files
with
193 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
Sphinx >= 6.2.1 | ||
sphinx-rtd-theme >= 2.0.0 | ||
trio >= 0.23.0 | ||
async-generator >= 1.10 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from typing import ( | ||
AsyncGenerator, | ||
AsyncIterable, | ||
AsyncIterator, | ||
Awaitable, | ||
Protocol, | ||
TypeVar, | ||
Union, | ||
runtime_checkable, | ||
) | ||
|
||
from contextlib import asynccontextmanager | ||
|
||
_T_co = TypeVar("_T_co", covariant=True) | ||
|
||
@asynccontextmanager | ||
async def safe_aclosing( | ||
obj: Union[AsyncIterable[_T_co], AsyncIterator[_T_co]] | ||
) -> AsyncGenerator[AsyncIterator[_T_co], None]: | ||
if not isinstance(obj, AsyncIterator): | ||
obj = obj.__aiter__() | ||
try: | ||
yield obj | ||
finally: | ||
await safe_aclose(obj) | ||
|
||
async def safe_aclose(obj: AsyncIterator[_T_co]) -> None: | ||
if isinstance(obj, _SupportsAclose): | ||
await obj.aclose() | ||
|
||
@runtime_checkable | ||
class _SupportsAclose(Protocol): | ||
def aclose(self) -> Awaitable[object]: | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import string | ||
from functools import wraps | ||
|
||
import pytest | ||
import trio | ||
|
||
from .fixtures import AsyncIteratorWithoutAclose | ||
|
||
def fixture_gen_with_and_without_aclose(async_gen): | ||
|
||
def fixture_func(_with_aclose): | ||
if _with_aclose: | ||
new_async_gen = async_gen | ||
else: | ||
@wraps(async_gen) | ||
def new_async_gen(*args, **kwargs): | ||
return AsyncIteratorWithoutAclose(async_gen(*args, **kwargs)) | ||
return new_async_gen | ||
|
||
fixture_func.__name__ = async_gen.__name__ | ||
fixture_func.__qualname__ = async_gen.__name__ | ||
|
||
return pytest.fixture(fixture_func) | ||
|
||
@pytest.fixture(params=[True, False], ids=["with_aclose", "without_aclose"]) | ||
def _with_aclose(request): | ||
return request.param | ||
|
||
@fixture_gen_with_and_without_aclose | ||
async def produce_increasing_integers(interval, *, max=3, delay=0): | ||
await trio.sleep(delay) | ||
for i in range(max): | ||
yield i | ||
if i == max-1: | ||
break | ||
await trio.sleep(interval) | ||
|
||
@fixture_gen_with_and_without_aclose | ||
async def produce_alphabet(interval, *, max=3, delay=0): | ||
await trio.sleep(delay) | ||
for i, c in enumerate(string.ascii_lowercase): | ||
yield c | ||
if i == max - 1: | ||
break | ||
await trio.sleep(interval) | ||
|
||
@pytest.fixture() | ||
def spam_wait_spam_integers(produce_increasing_integers): | ||
async def spam_wait_spam_integers(interval): | ||
async for i in produce_increasing_integers(.1, max=5, delay=.1): | ||
yield i | ||
await trio.sleep(interval) | ||
async for i in produce_increasing_integers(.1, max=5, delay=.1): | ||
yield i | ||
|
||
return spam_wait_spam_integers | ||
|
||
@fixture_gen_with_and_without_aclose | ||
async def produce_mappings(interval): | ||
vehicles = [ | ||
{'vehicle': 'motorcycle'}, | ||
{'vehicle': 'car'}, | ||
{'vehicle': 'motorcycle'}, | ||
{'vehicle': 'autocamper'}, | ||
{'vehicle': 'car'}, | ||
{'vehicle': 'car'}, | ||
{'vehicle': 'truck'}, | ||
{'vehicle': 'car'}, | ||
{'vehicle': 'motorcycle'}, | ||
] | ||
|
||
for i, vehicle in enumerate(vehicles): | ||
vehicle['number'] = i | ||
yield vehicle | ||
await trio.sleep(interval) |
Oops, something went wrong.