diff --git a/CHANGES.rst b/CHANGES.rst index 72754ad6..f4e0b5d0 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -13,6 +13,12 @@ Version 2.0.2 Unreleased +- Fix type annotation for ``teardown_request``. :issue:`4093` +- Fix type annotation for ``before_request`` and ``before_app_request`` + decorators. :issue:`4104` +- Fixed the issue where typing requires template global + decorators to accept functions with no arguments. :issue:`4098` + Version 2.0.1 ------------- diff --git a/docs/tutorial/index.rst b/docs/tutorial/index.rst index 103c27a8..d5dc5b3c 100644 --- a/docs/tutorial/index.rst +++ b/docs/tutorial/index.rst @@ -55,7 +55,7 @@ this structure and take full advantage of Flask's flexibility. .. image:: flaskr_edit.png :align: center :class: screenshot - :alt: screenshot of login page + :alt: screenshot of edit page :gh:`The tutorial project is available as an example in the Flask repository `, if you want to compare your project 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 5f27308c..b1a6cbdc 100644 --- a/src/flask/typing.py +++ b/src/flask/typing.py @@ -35,12 +35,13 @@ 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]], "Response"] +TeardownCallable = t.Callable[[t.Optional[BaseException]], None] TemplateContextProcessorCallable = t.Callable[[], t.Dict[str, t.Any]] -TemplateFilterCallable = t.Callable[[t.Any], str] -TemplateGlobalCallable = t.Callable[[], t.Any] -TemplateTestCallable = t.Callable[[t.Any], bool] +TemplateFilterCallable = t.Callable[..., t.Any] +TemplateGlobalCallable = t.Callable[..., t.Any] +TemplateTestCallable = t.Callable[..., bool] URLDefaultCallable = t.Callable[[str, dict], None] URLValuePreprocessorCallable = t.Callable[[t.Optional[str], t.Optional[dict]], None]