async_utils.py
This file
Path
src/jinja2/async_utils.py
99 lines · 2.8 KiB · Python
Last changed by
drop end of life python versions (#2096)David Lordbefore adoption49c48a0
History of this fileTake it
Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
lines 1–2why, one step back1import inspect2import typing as tMerge pull request #1613 from dairiki/bug.environment-not-pickleableDavid Lordbefore adoptiond5b8f66
line 3why, one step back3from functools import WRAPPER_ASSIGNMENTSMerge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
lines 4–8why, one step back4from functools import wraps56from .utils import _PassArg7from .utils import pass_eval_context8Merge branch '3.1.x'David Lordbefore adoptionb002d9c
lines 9–11why, one step back9if t.TYPE_CHECKING:10 import typing_extensions as te11Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 12why, one step back12V = t.TypeVar("V")Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
lines 13–14why, one step back1314Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 15–16why, one step back15def async_variant(normal_func): # type: ignore16 def decorator(async_func): # type: ignoreMerge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
lines 17–21why, one step back17 pass_arg = _PassArg.from_obj(normal_func)18 need_eval_context = pass_arg is None1920 if pass_arg is _PassArg.environment:21Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 22–23why, one step back22 def is_async(args: t.Any) -> bool:23 return t.cast(bool, args[0].is_async)Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
lines 24–26why, one step back2425 else:26Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 27–28why, one step back27 def is_async(args: t.Any) -> bool:28 return t.cast(bool, args[0].environment.is_async)Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
line 29why, one step back29Merge pull request #1613 from dairiki/bug.environment-not-pickleableDavid Lordbefore adoptiond5b8f66
lines 30–38why, one step back30 # Take the doc and annotations from the sync function, but the31 # name from the async function. Pallets-Sphinx-Themes32 # build_function_directive expects __wrapped__ to point to the33 # sync function.34 async_func_attrs = ("__module__", "__name__", "__qualname__")35 normal_func_attrs = tuple(set(WRAPPER_ASSIGNMENTS).difference(async_func_attrs))3637 @wraps(normal_func, assigned=normal_func_attrs)38 @wraps(async_func, assigned=async_func_attrs, updated=())Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 39why, one step back39 def wrapper(*args, **kwargs): # type: ignoreMerge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
lines 40–52why, one step back40 b = is_async(args)4142 if need_eval_context:43 args = args[1:]4445 if b:46 return async_func(*args, **kwargs)4748 return normal_func(*args, **kwargs)4950 if need_eval_context:51 wrapper = pass_eval_context(wrapper)52switch to pyproject.toml (#1793)David Lordbefore adoption05a5f81
line 53why, one step back53 wrapper.jinja_async_variant = True # type: ignore[attr-defined]Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
lines 54–58why, one step back54 return wrapper5556 return decorator5758Merge branch '3.0.x'David Lordbefore adoptiona42b291
lines 59–61why, one step back59_common_primitives = {int, float, bool, str, list, dict, tuple, type(None)}6061Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 62why, one step back62async def auto_await(value: t.Union[t.Awaitable["V"], "V"]) -> "V":Merge branch '3.0.x'David Lordbefore adoptiona42b291
lines 63–66why, one step back63 # Avoid a costly call to isawaitable64 if type(value) in _common_primitives:65 return t.cast("V", value)66Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
line 67why, one step back67 if inspect.isawaitable(value):Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 68why, one step back68 return await t.cast("t.Awaitable[V]", value)Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
line 69why, one step back69Merge branch '3.1.x'David Lordbefore adoption7f0fc0a
line 70why, one step back70 return valueMerge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
lines 71–72why, one step back7172Merge branch '3.1.x'David Lordbefore adoptionb002d9c
lines 73–87why, one step back73class _IteratorToAsyncIterator(t.Generic[V]):74 def __init__(self, iterator: "t.Iterator[V]"):75 self._iterator = iterator7677 def __aiter__(self) -> "te.Self":78 return self7980 async def __anext__(self) -> V:81 try:82 return next(self._iterator)83 except StopIteration as e:84 raise StopAsyncIteration(e.value) from e858687def auto_aiter(drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 88why, one step back88 iterable: "t.AsyncIterable[V] | t.Iterable[V]",Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 89why, one step back89) -> "t.AsyncIterator[V]":Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
line 90why, one step back90 if hasattr(iterable, "__aiter__"):Merge branch '3.1.x'David Lordbefore adoptionb002d9c
line 91why, one step back91 return iterable.__aiter__()Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
line 92why, one step back92 else:Merge branch '3.1.x'David Lordbefore adoptionb002d9c
line 93why, one step back93 return _IteratorToAsyncIterator(iter(iterable))Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
lines 94–96why, one step back949596async def auto_to_list(drop end of life python versions (#2096)David Lordbefore adoption49c48a0
line 97why, one step back97 value: "t.AsyncIterable[V] | t.Iterable[V]",Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74
line 98why, one step back98) -> list["V"]:Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 99why, one step back99 return [x async for x in auto_aiter(value)]