(async_func)
| 11 | |
| 12 | def async_variant(normal_func): # type: ignore |
| 13 | def decorator(async_func): # type: ignore |
| 14 | pass_arg = _PassArg.from_obj(normal_func) |
| 15 | need_eval_context = pass_arg is None |
| 16 | |
| 17 | if pass_arg is _PassArg.environment: |
| 18 | |
| 19 | def is_async(args: t.Any) -> bool: |
| 20 | return t.cast(bool, args[0].is_async) |
| 21 | |
| 22 | else: |
| 23 | |
| 24 | def is_async(args: t.Any) -> bool: |
| 25 | return t.cast(bool, args[0].environment.is_async) |
| 26 | |
| 27 | # Take the doc and annotations from the sync function, but the |
| 28 | # name from the async function. Pallets-Sphinx-Themes |
| 29 | # build_function_directive expects __wrapped__ to point to the |
| 30 | # sync function. |
| 31 | async_func_attrs = ("__module__", "__name__", "__qualname__") |
| 32 | normal_func_attrs = tuple(set(WRAPPER_ASSIGNMENTS).difference(async_func_attrs)) |
| 33 | |
| 34 | @wraps(normal_func, assigned=normal_func_attrs) |
| 35 | @wraps(async_func, assigned=async_func_attrs, updated=()) |
| 36 | def wrapper(*args, **kwargs): # type: ignore |
| 37 | b = is_async(args) |
| 38 | |
| 39 | if need_eval_context: |
| 40 | args = args[1:] |
| 41 | |
| 42 | if b: |
| 43 | return async_func(*args, **kwargs) |
| 44 | |
| 45 | return normal_func(*args, **kwargs) |
| 46 | |
| 47 | if need_eval_context: |
| 48 | wrapper = pass_eval_context(wrapper) |
| 49 | |
| 50 | wrapper.jinja_async_variant = True |
| 51 | return wrapper |
| 52 | |
| 53 | return decorator |
| 54 |
nothing calls this directly
no test coverage detected