From 10a36cb60e10594aa5dbf3c5aba6abbd5f6fcd70 Mon Sep 17 00:00:00 2001 From: Alex Hedges Date: Mon, 17 May 2021 15:30:05 -0400 Subject: [PATCH] Improve decorator factory type signatures These changes are required to preserve the type signatures of the created decorators. --- src/flask/app.py | 12 +++++++++--- src/flask/blueprints.py | 12 +++++++++--- src/flask/scaffold.py | 2 +- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/flask/app.py b/src/flask/app.py index f0f31486..cd1c42ad 100644 --- a/src/flask/app.py +++ b/src/flask/app.py @@ -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:: diff --git a/src/flask/blueprints.py b/src/flask/blueprints.py index 098eca8c..88883ba7 100644 --- a/src/flask/blueprints.py +++ b/src/flask/blueprints.py @@ -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. diff --git a/src/flask/scaffold.py b/src/flask/scaffold.py index f8f94af1..239bc46a 100644 --- a/src/flask/scaffold.py +++ b/src/flask/scaffold.py @@ -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