Improve decorator factory type signatures

These changes are required to preserve the type signatures of the
created decorators.
This commit is contained in:
Alex Hedges 2021-05-17 15:30:05 -04:00 committed by Phil Jones
parent 8796b2a784
commit 10a36cb60e
3 changed files with 19 additions and 7 deletions

View File

@ -1089,7 +1089,9 @@ class Flask(Scaffold):
self.view_functions[endpoint] = view_func
@setupmethod
def template_filter(self, name: t.Optional[str] = None) -> t.Callable:
def template_filter(
self, name: t.Optional[str] = None
) -> t.Callable[[TemplateFilterCallable], TemplateFilterCallable]:
"""A decorator that is used to register custom template filter.
You can specify a name for the filter, otherwise the function
name will be used. Example::
@ -1121,7 +1123,9 @@ class Flask(Scaffold):
self.jinja_env.filters[name or f.__name__] = f
@setupmethod
def template_test(self, name: t.Optional[str] = None) -> t.Callable:
def template_test(
self, name: t.Optional[str] = None
) -> t.Callable[[TemplateTestCallable], TemplateTestCallable]:
"""A decorator that is used to register custom template test.
You can specify a name for the test, otherwise the function
name will be used. Example::
@ -1162,7 +1166,9 @@ class Flask(Scaffold):
self.jinja_env.tests[name or f.__name__] = f
@setupmethod
def template_global(self, name: t.Optional[str] = None) -> t.Callable:
def template_global(
self, name: t.Optional[str] = None
) -> t.Callable[[TemplateGlobalCallable], TemplateGlobalCallable]:
"""A decorator that is used to register a custom template global function.
You can specify a name for the global function, otherwise the function
name will be used. Example::

View File

@ -387,7 +387,9 @@ class Blueprint(Scaffold):
)
)
def app_template_filter(self, name: t.Optional[str] = None) -> t.Callable:
def app_template_filter(
self, name: t.Optional[str] = None
) -> t.Callable[[TemplateFilterCallable], TemplateFilterCallable]:
"""Register a custom template filter, available application wide. Like
:meth:`Flask.template_filter` but for a blueprint.
@ -417,7 +419,9 @@ class Blueprint(Scaffold):
self.record_once(register_template)
def app_template_test(self, name: t.Optional[str] = None) -> t.Callable:
def app_template_test(
self, name: t.Optional[str] = None
) -> t.Callable[[TemplateTestCallable], TemplateTestCallable]:
"""Register a custom template test, available application wide. Like
:meth:`Flask.template_test` but for a blueprint.
@ -451,7 +455,9 @@ class Blueprint(Scaffold):
self.record_once(register_template)
def app_template_global(self, name: t.Optional[str] = None) -> t.Callable:
def app_template_global(
self, name: t.Optional[str] = None
) -> t.Callable[[TemplateGlobalCallable], TemplateGlobalCallable]:
"""Register a custom template global, available application wide. Like
:meth:`Flask.template_global` but for a blueprint.

View File

@ -644,7 +644,7 @@ class Scaffold:
@setupmethod
def errorhandler(
self, code_or_exception: t.Union[t.Type[Exception], int]
) -> t.Callable:
) -> t.Callable[[ErrorHandlerCallable], ErrorHandlerCallable]:
"""Register a function to handle errors by code or exception class.
A decorator that is used to register a function given an