async_utils.py
This file
Path
src/jinja2/async_utils.py
84 lines · 2.4 KiB · Python
Last changed by
Merge pull request #1613 from dairiki/bug.environment-not-pickleableDavid Lordbefore adoptiond5b8f66
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 pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 9why, one step back9V = t.TypeVar("V")Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
lines 10–11why, one step back1011Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 12–13why, one step back12def async_variant(normal_func): # type: ignore13 def decorator(async_func): # type: ignoreMerge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
lines 14–18why, one step back14 pass_arg = _PassArg.from_obj(normal_func)15 need_eval_context = pass_arg is None1617 if pass_arg is _PassArg.environment:18Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 19–20why, one step back19 def is_async(args: t.Any) -> bool:20 return t.cast(bool, args[0].is_async)Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
lines 21–23why, one step back2122 else:23Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 24–25why, one step back24 def is_async(args: t.Any) -> bool:25 return t.cast(bool, args[0].environment.is_async)Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
line 26why, one step back26Merge pull request #1613 from dairiki/bug.environment-not-pickleableDavid Lordbefore adoptiond5b8f66
lines 27–35why, one step back27 # Take the doc and annotations from the sync function, but the28 # name from the async function. Pallets-Sphinx-Themes29 # build_function_directive expects __wrapped__ to point to the30 # sync function.31 async_func_attrs = ("__module__", "__name__", "__qualname__")32 normal_func_attrs = tuple(set(WRAPPER_ASSIGNMENTS).difference(async_func_attrs))3334 @wraps(normal_func, assigned=normal_func_attrs)35 @wraps(async_func, assigned=async_func_attrs, updated=())Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 36why, one step back36 def wrapper(*args, **kwargs): # type: ignoreMerge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
lines 37–55why, one step back37 b = is_async(args)3839 if need_eval_context:40 args = args[1:]4142 if b:43 return async_func(*args, **kwargs)4445 return normal_func(*args, **kwargs)4647 if need_eval_context:48 wrapper = pass_eval_context(wrapper)4950 wrapper.jinja_async_variant = True51 return wrapper5253 return decorator5455Merge branch '3.0.x'David Lordbefore adoptiona42b291
lines 56–58why, one step back56_common_primitives = {int, float, bool, str, list, dict, tuple, type(None)}5758Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 59why, one step back59async def auto_await(value: t.Union[t.Awaitable["V"], "V"]) -> "V":Merge branch '3.0.x'David Lordbefore adoptiona42b291
lines 60–63why, one step back60 # Avoid a costly call to isawaitable61 if type(value) in _common_primitives:62 return t.cast("V", value)63Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
line 64why, one step back64 if inspect.isawaitable(value):Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 65why, one step back65 return await t.cast("t.Awaitable[V]", value)Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
line 66why, one step back66Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 67why, one step back67 return t.cast("V", value)Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
lines 68–69why, one step back6869Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 70–72why, one step back70async def auto_aiter(71 iterable: "t.Union[t.AsyncIterable[V], t.Iterable[V]]",72) -> "t.AsyncIterator[V]":Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
line 73why, one step back73 if hasattr(iterable, "__aiter__"):Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 74why, one step back74 async for item in t.cast("t.AsyncIterable[V]", iterable):Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
lines 75–76why, one step back75 yield item76 else:Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 77why, one step back77 for item in t.cast("t.Iterable[V]", iterable):Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
lines 78–82why, one step back78 yield item798081async def auto_to_list(82 value: "t.Union[t.AsyncIterable[V], t.Iterable[V]]",Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 83–84why, one step back83) -> t.List["V"]:84 return [x async for x in auto_aiter(value)]