Callable proxy importing the target of a JinjaItem on first use.
Attribute access is forwarded to the target, so Jinja sees jinja_pass_arg
(@pass_context & co.) and inspect sees the signature (via __wrapped__).
Source code in src/jinjarope/jinjafile.py
| class LazyCallable:
"""Callable proxy importing the target of a `JinjaItem` on first use.
Attribute access is forwarded to the target, so Jinja sees `jinja_pass_arg`
(`@pass_context` & co.) and `inspect` sees the signature (via `__wrapped__`).
"""
__slots__ = ("_item", "_target")
def __init__(self, item: JinjaItem) -> None:
self._item = item
self._target: Callable[..., Any] | None = None
@property
def __wrapped__(self) -> Callable[..., Any]:
if self._target is None:
self._target = self._item.filter_fn
return self._target
def __call__(self, *args: Any, **kwargs: Any) -> Any:
return self.__wrapped__(*args, **kwargs)
def __getattr__(self, name: str) -> Any:
return getattr(self.__wrapped__, name)
def __repr__(self) -> str:
state = "resolved" if self._target is not None else "lazy"
return f"{type(self).__name__}({self._item.fn!r}, {state})"
|