diff --git a/CHANGES.rst b/CHANGES.rst index 9c374f56..3e79b5cb 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,7 +5,9 @@ Version 2.0.2 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 diff --git a/src/flask/app.py b/src/flask/app.py index 3abce3ce..cacb40a5 100644 --- a/src/flask/app.py +++ b/src/flask/app.py @@ -59,6 +59,7 @@ from .signals import request_tearing_down from .templating import DispatchingJinjaLoader from .templating import Environment from .typing import AfterRequestCallable +from .typing import BeforeFirstRequestCallable from .typing import BeforeRequestCallable from .typing import ErrorHandlerCallable from .typing import ResponseReturnValue @@ -439,7 +440,7 @@ class Flask(Scaffold): #: :meth:`before_first_request` decorator. #: #: .. 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 #: 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 @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 instance of the application. diff --git a/src/flask/blueprints.py b/src/flask/blueprints.py index f3913b30..883fc2ff 100644 --- a/src/flask/blueprints.py +++ b/src/flask/blueprints.py @@ -6,6 +6,7 @@ from .scaffold import _endpoint_from_view_func from .scaffold import _sentinel from .scaffold import Scaffold from .typing import AfterRequestCallable +from .typing import BeforeFirstRequestCallable from .typing import BeforeRequestCallable from .typing import ErrorHandlerCallable from .typing import TeardownCallable @@ -537,8 +538,8 @@ class Blueprint(Scaffold): return f def before_app_first_request( - self, f: BeforeRequestCallable - ) -> BeforeRequestCallable: + self, f: BeforeFirstRequestCallable + ) -> BeforeFirstRequestCallable: """Like :meth:`Flask.before_first_request`. Such a function is executed before the first request to the application. """ diff --git a/src/flask/typing.py b/src/flask/typing.py index 60c725ee..8d89839f 100644 --- a/src/flask/typing.py +++ b/src/flask/typing.py @@ -35,7 +35,8 @@ ResponseReturnValue = t.Union[ AppOrBlueprintKey = t.Optional[str] # The App key is None, whereas blueprints are named AfterRequestCallable = t.Callable[["Response"], "Response"] -BeforeRequestCallable = t.Callable[[], None] +BeforeFirstRequestCallable = t.Callable[[], None] +BeforeRequestCallable = t.Callable[[], t.Optional[ResponseReturnValue]] ErrorHandlerCallable = t.Callable[[Exception], ResponseReturnValue] TeardownCallable = t.Callable[[t.Optional[BaseException]], None] TemplateContextProcessorCallable = t.Callable[[], t.Dict[str, t.Any]]