-
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.
Iterators without aclose() (rare) are notoriously problematic...
- So now aclose() is explicitly required. - Since we effectively had an in-project `aclosing()` implementation, also removed async_generator dep. - The new, better-annotated `aclosing()` triggered Pyright to alert on a few places where it was being applied to an iterable instead of an iterator. Fixed those, too.
- Loading branch information
1 parent
4a2e9f4
commit e688d43
Showing
18 changed files
with
100 additions
and
109 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
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 |
---|---|---|
@@ -1,10 +1,18 @@ | ||
from typing import Any, AsyncIterable, Awaitable, Protocol, Tuple, Union, runtime_checkable | ||
from typing import Any, AsyncIterable, Awaitable, Protocol, Tuple, TypeVar, Union, runtime_checkable | ||
|
||
from .sections.abc import Section | ||
from .sections import abc | ||
|
||
PipelineSection = Union[AsyncIterable[Any], Section, Tuple["PipelineSection", ...]] | ||
PipelineSection = Union["AsyncIterableWithAcloseableIterator[Any]", "abc.Section", Tuple["PipelineSection", ...]] | ||
|
||
_T_co = TypeVar("_T_co", covariant=True) | ||
|
||
@runtime_checkable | ||
class SupportsAclose(Protocol): | ||
def aclose(self) -> Awaitable[object]: | ||
... | ||
def aclose(self) -> Awaitable[object]: ... | ||
|
||
class AcloseableAsyncIterator(SupportsAclose, Protocol[_T_co]): | ||
def __anext__(self) -> Awaitable[_T_co]: ... | ||
def __aiter__(self) -> "AcloseableAsyncIterator[_T_co]": ... | ||
|
||
class AsyncIterableWithAcloseableIterator(Protocol[_T_co]): | ||
def __aiter__(self) -> AcloseableAsyncIterator[_T_co]: ... |
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,15 +1,14 @@ | ||
from typing import AsyncGenerator, AsyncIterator, TypeVar | ||
from typing import AsyncGenerator, TypeVar | ||
|
||
from ._types import SupportsAclose | ||
from ._types import AcloseableAsyncIterator | ||
|
||
from contextlib import asynccontextmanager | ||
|
||
_T = TypeVar("_T") | ||
|
||
@asynccontextmanager | ||
async def safe_aclosing(obj: AsyncIterator[_T]) -> AsyncGenerator[AsyncIterator[_T], None]: | ||
async def aclosing(obj: AcloseableAsyncIterator[_T]) -> AsyncGenerator[AcloseableAsyncIterator[_T], None]: | ||
try: | ||
yield obj | ||
finally: | ||
if isinstance(obj, SupportsAclose): | ||
await obj.aclose() | ||
await obj.aclose() |
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 |
---|---|---|
@@ -1,13 +1,16 @@ | ||
"""The Trio environment implements ``TrioSection``, which is a Trio-native | ||
:class:`AsyncSection <slurry.sections.abc.AsyncSection>`.""" | ||
from typing import Any, AsyncIterable, Awaitable, Callable, Optional | ||
from typing import Any, Awaitable, Callable, Optional | ||
|
||
from ..sections.abc import AsyncSection | ||
from .._types import AsyncIterableWithAcloseableIterator | ||
|
||
class TrioSection(AsyncSection): | ||
"""Since Trio is the native Slurry event loop, this environment is simple to implement. | ||
The pump method does not need to do anything special to bridge the input and output. It | ||
simply delegates directly to the refine method, as the api is identical.""" | ||
async def pump(self, input: Optional[AsyncIterable[Any]], output: Callable[[Any], Awaitable[None]]): | ||
async def pump( | ||
self, input: Optional[AsyncIterableWithAcloseableIterator[Any]], output: Callable[[Any], Awaitable[None]] | ||
): | ||
"""Calls refine.""" | ||
await self.refine(input, output) |
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
Oops, something went wrong.