Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: type annotate async function (and an internal function) #123

Merged
merged 1 commit into from
May 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions stream_zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import asyncio
import secrets
import zlib
from typing import Any, Iterable, Tuple, Optional, Deque, Type, AsyncIterable
from typing import Any, Iterable, Tuple, Optional, Deque, Type, AsyncIterable, Callable

from Crypto.Cipher import AES
from Crypto.Hash import HMAC, SHA1
Expand Down Expand Up @@ -156,7 +156,7 @@ def _raise_if_beyond(offset: int, maximum: int, exception_class: Type[Exception]
if offset > maximum:
raise exception_class()

def _with_returned(gen):
def _with_returned(gen) -> Tuple[Callable[[], Optional[Any]], Iterable[bytes]]:
# We leverage the not-often used "return value" of generators. Here, we want to iterate
# over chunks (to encrypt them), but still return the same "return value". So we use a
# bit of a trick to extract the return value but still have access to the chunks as
Expand Down Expand Up @@ -719,7 +719,7 @@ def _no_compression_streamed_data(chunks, uncompressed_size, crc_32, maximum_siz

async def async_stream_zip(member_files, *args, **kwargs) -> AsyncIterable[bytes]:

async def to_async_iterable(sync_iterable):
async def to_async_iterable(sync_iterable) -> AsyncIterable[Any]:
# asyncio.to_thread is not available until Python 3.9, and StopIteration doesn't get
# propagated by run_in_executor, so we use a sentinel to detect the end of the iterable
done = object()
Expand All @@ -729,17 +729,18 @@ async def to_async_iterable(sync_iterable):
try:
import contextvars
except ImportError:
get_args = lambda: (next, it, done)
get_func_args: Callable[[], Tuple[Callable[..., Any], Tuple[Any, ...]]] = lambda: (next, (it, done))
else:
get_args = lambda: (contextvars.copy_context().run, next, it, done)
get_func_args = lambda: (contextvars.copy_context().run, (next, it, done))

while True:
value = await loop.run_in_executor(None, *get_args())
func, args = get_func_args()
value = await loop.run_in_executor(None, func, *args)
if value is done:
break
yield value

def to_sync_iterable(async_iterable):
def to_sync_iterable(async_iterable) -> Iterable[Any]:
# The built-in aiter and anext functions are not available until Python 3.10
async_it = async_iterable.__aiter__()
while True:
Expand Down
Loading