Fix type annotation for `before_request` and `before_app_request` decorators

This commit is contained in:
Marat Sharafutdinov 2021-05-25 18:04:41 +03:00 committed by Phil Jones
parent f7adb2c813
commit a960236117
4 changed files with 13 additions and 6 deletions

View File

@ -6,6 +6,8 @@ Version 2.0.2
Unreleased Unreleased
- Fix type annotation for ``teardown_request``. :issue:`4093` - Fix type annotation for ``teardown_request``. :issue:`4093`
- Fix type annotation for ``before_request`` and ``before_app_request``
decorators. :issue:`4104`
Version 2.0.1 Version 2.0.1

View File

@ -59,6 +59,7 @@ from .signals import request_tearing_down
from .templating import DispatchingJinjaLoader from .templating import DispatchingJinjaLoader
from .templating import Environment from .templating import Environment
from .typing import AfterRequestCallable from .typing import AfterRequestCallable
from .typing import BeforeFirstRequestCallable
from .typing import BeforeRequestCallable from .typing import BeforeRequestCallable
from .typing import ErrorHandlerCallable from .typing import ErrorHandlerCallable
from .typing import ResponseReturnValue from .typing import ResponseReturnValue
@ -439,7 +440,7 @@ class Flask(Scaffold):
#: :meth:`before_first_request` decorator. #: :meth:`before_first_request` decorator.
#: #:
#: .. versionadded:: 0.8 #: .. versionadded:: 0.8
self.before_first_request_funcs: t.List[BeforeRequestCallable] = [] self.before_first_request_funcs: t.List[BeforeFirstRequestCallable] = []
#: A list of functions that are called when the application context #: A list of functions that are called when the application context
#: is destroyed. Since the application context is also torn down #: is destroyed. Since the application context is also torn down
@ -1211,7 +1212,9 @@ class Flask(Scaffold):
self.jinja_env.globals[name or f.__name__] = f self.jinja_env.globals[name or f.__name__] = f
@setupmethod @setupmethod
def before_first_request(self, f: BeforeRequestCallable) -> BeforeRequestCallable: def before_first_request(
self, f: BeforeFirstRequestCallable
) -> BeforeFirstRequestCallable:
"""Registers a function to be run before the first request to this """Registers a function to be run before the first request to this
instance of the application. instance of the application.

View File

@ -6,6 +6,7 @@ from .scaffold import _endpoint_from_view_func
from .scaffold import _sentinel from .scaffold import _sentinel
from .scaffold import Scaffold from .scaffold import Scaffold
from .typing import AfterRequestCallable from .typing import AfterRequestCallable
from .typing import BeforeFirstRequestCallable
from .typing import BeforeRequestCallable from .typing import BeforeRequestCallable
from .typing import ErrorHandlerCallable from .typing import ErrorHandlerCallable
from .typing import TeardownCallable from .typing import TeardownCallable
@ -537,8 +538,8 @@ class Blueprint(Scaffold):
return f return f
def before_app_first_request( def before_app_first_request(
self, f: BeforeRequestCallable self, f: BeforeFirstRequestCallable
) -> BeforeRequestCallable: ) -> BeforeFirstRequestCallable:
"""Like :meth:`Flask.before_first_request`. Such a function is """Like :meth:`Flask.before_first_request`. Such a function is
executed before the first request to the application. executed before the first request to the application.
""" """

View File

@ -35,7 +35,8 @@ ResponseReturnValue = t.Union[
AppOrBlueprintKey = t.Optional[str] # The App key is None, whereas blueprints are named AppOrBlueprintKey = t.Optional[str] # The App key is None, whereas blueprints are named
AfterRequestCallable = t.Callable[["Response"], "Response"] AfterRequestCallable = t.Callable[["Response"], "Response"]
BeforeRequestCallable = t.Callable[[], None] BeforeFirstRequestCallable = t.Callable[[], None]
BeforeRequestCallable = t.Callable[[], t.Optional[ResponseReturnValue]]
ErrorHandlerCallable = t.Callable[[Exception], ResponseReturnValue] ErrorHandlerCallable = t.Callable[[Exception], ResponseReturnValue]
TeardownCallable = t.Callable[[t.Optional[BaseException]], None] TeardownCallable = t.Callable[[t.Optional[BaseException]], None]
TemplateContextProcessorCallable = t.Callable[[], t.Dict[str, t.Any]] TemplateContextProcessorCallable = t.Callable[[], t.Dict[str, t.Any]]