From 3f6b243cec6859185ea4064e84b33c07cc7febf6 Mon Sep 17 00:00:00 2001 From: pgjones Date: Sat, 15 Jul 2023 23:21:12 +0100 Subject: [PATCH] Support async template context processors This is useful as there is contextual information that could be loaded via IO e.g. information from a database. This also matches Quart and hence makes the shared signature easier to manage. --- src/flask/app.py | 2 +- src/flask/typing.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/flask/app.py b/src/flask/app.py index 34d315da..3278ae9a 100644 --- a/src/flask/app.py +++ b/src/flask/app.py @@ -478,7 +478,7 @@ class Flask(App): for name in names: if name in self.template_context_processors: for func in self.template_context_processors[name]: - context.update(func()) + context.update(self.ensure_sync(func)()) context.update(orig_ctx) diff --git a/src/flask/typing.py b/src/flask/typing.py index 50aef7f4..0cd4b3e7 100644 --- a/src/flask/typing.py +++ b/src/flask/typing.py @@ -61,7 +61,10 @@ TeardownCallable = t.Union[ t.Callable[[t.Optional[BaseException]], None], t.Callable[[t.Optional[BaseException]], t.Awaitable[None]], ] -TemplateContextProcessorCallable = t.Callable[[], t.Dict[str, t.Any]] +TemplateContextProcessorCallable = t.Union[ + t.Callable[[], t.Dict[str, t.Any]], + t.Callable[[], t.Awaitable[t.Dict[str, t.Any]]], +] TemplateFilterCallable = t.Callable[..., t.Any] TemplateGlobalCallable = t.Callable[..., t.Any] TemplateTestCallable = t.Callable[..., bool]