Skip to content
Myr

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 file

Take it

Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b

lines 1–2why, one step back
1import inspect
2import typing as t

Merge pull request #1613 from dairiki/bug.environment-not-pickleableDavid Lordbefore adoptiond5b8f66

line 3why, one step back
3from functools import WRAPPER_ASSIGNMENTS

Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b

lines 4–8why, one step back
4from functools import wraps
56from .utils import _PassArg7from .utils import pass_eval_context8

Merge branch '3.1.x'David Lordbefore adoptionb002d9c

lines 9–11why, one step back
9if t.TYPE_CHECKING:
10 import typing_extensions as te11

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 12why, one step back
12V = t.TypeVar("V")

Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b

lines 13–14why, one step back
13
14

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 15–16why, one step back
15def async_variant(normal_func): # type: ignore
16 def decorator(async_func): # type: ignore

Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b

lines 17–21why, one step back
17 pass_arg = _PassArg.from_obj(normal_func)
18 need_eval_context = pass_arg is None1920 if pass_arg is _PassArg.environment:21

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 22–23why, one step back
22 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 back
24
25 else:26

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 27–28why, one step back
27 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 back
29

Merge pull request #1613 from dairiki/bug.environment-not-pickleableDavid Lordbefore adoptiond5b8f66

lines 30–38why, one step back
30 # Take the doc and annotations from the sync function, but the
31 # 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 back
39 def wrapper(*args, **kwargs): # type: ignore

Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b

lines 40–52why, one step back
40 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)52

switch to pyproject.toml (#1793)David Lordbefore adoption05a5f81

line 53why, one step back
53 wrapper.jinja_async_variant = True # type: ignore[attr-defined]

Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b

lines 54–58why, one step back
54 return wrapper
5556 return decorator5758

Merge branch '3.0.x'David Lordbefore adoptiona42b291

lines 59–61why, one step back
59_common_primitives = {int, float, bool, str, list, dict, tuple, type(None)}
6061

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 62why, one step back
62async def auto_await(value: t.Union[t.Awaitable["V"], "V"]) -> "V":

Merge branch '3.0.x'David Lordbefore adoptiona42b291

lines 63–66why, one step back
63 # Avoid a costly call to isawaitable
64 if type(value) in _common_primitives:65 return t.cast("V", value)66

Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b

line 67why, one step back
67 if inspect.isawaitable(value):

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 68why, one step back
68 return await t.cast("t.Awaitable[V]", value)

Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b

line 69why, one step back
69

Merge branch '3.1.x'David Lordbefore adoption7f0fc0a

line 70why, one step back
70 return value

Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b

lines 71–72why, one step back
71
72

Merge branch '3.1.x'David Lordbefore adoptionb002d9c

lines 73–87why, one step back
73class _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 back
88 iterable: "t.AsyncIterable[V] | t.Iterable[V]",

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 89why, one step back
89) -> "t.AsyncIterator[V]":

Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b

line 90why, one step back
90 if hasattr(iterable, "__aiter__"):

Merge branch '3.1.x'David Lordbefore adoptionb002d9c

line 91why, one step back
91 return iterable.__aiter__()

Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b

line 92why, one step back
92 else:

Merge branch '3.1.x'David Lordbefore adoptionb002d9c

line 93why, one step back
93 return _IteratorToAsyncIterator(iter(iterable))

Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b

lines 94–96why, one step back
94
9596async def auto_to_list(

drop end of life python versions (#2096)David Lordbefore adoption49c48a0

line 97why, one step back
97 value: "t.AsyncIterable[V] | t.Iterable[V]",

Updated dependency management to uv (#2093)David Lordbefore adoption05f5d74

line 98why, one step back
98) -> list["V"]:

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 99why, one step back
99 return [x async for x in auto_aiter(value)]