Skip to content

Commit a960236

Browse files
decazpgjones
authored andcommitted
Fix type annotation for before_request and before_app_request decorators
1 parent f7adb2c commit a960236

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

CHANGES.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ Version 2.0.2
55

66
Unreleased
77

8-
- Fix type annotation for ``teardown_request``. :issue:`4093`
8+
- Fix type annotation for ``teardown_request``. :issue:`4093`
9+
- Fix type annotation for ``before_request`` and ``before_app_request``
10+
decorators. :issue:`4104`
911

1012

1113
Version 2.0.1

src/flask/app.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
from .templating import DispatchingJinjaLoader
6060
from .templating import Environment
6161
from .typing import AfterRequestCallable
62+
from .typing import BeforeFirstRequestCallable
6263
from .typing import BeforeRequestCallable
6364
from .typing import ErrorHandlerCallable
6465
from .typing import ResponseReturnValue
@@ -439,7 +440,7 @@ def __init__(
439440
#: :meth:`before_first_request` decorator.
440441
#:
441442
#: .. versionadded:: 0.8
442-
self.before_first_request_funcs: t.List[BeforeRequestCallable] = []
443+
self.before_first_request_funcs: t.List[BeforeFirstRequestCallable] = []
443444

444445
#: A list of functions that are called when the application context
445446
#: is destroyed. Since the application context is also torn down
@@ -1211,7 +1212,9 @@ def add_template_global(
12111212
self.jinja_env.globals[name or f.__name__] = f
12121213

12131214
@setupmethod
1214-
def before_first_request(self, f: BeforeRequestCallable) -> BeforeRequestCallable:
1215+
def before_first_request(
1216+
self, f: BeforeFirstRequestCallable
1217+
) -> BeforeFirstRequestCallable:
12151218
"""Registers a function to be run before the first request to this
12161219
instance of the application.
12171220

src/flask/blueprints.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .scaffold import _sentinel
77
from .scaffold import Scaffold
88
from .typing import AfterRequestCallable
9+
from .typing import BeforeFirstRequestCallable
910
from .typing import BeforeRequestCallable
1011
from .typing import ErrorHandlerCallable
1112
from .typing import TeardownCallable
@@ -537,8 +538,8 @@ def before_app_request(self, f: BeforeRequestCallable) -> BeforeRequestCallable:
537538
return f
538539

539540
def before_app_first_request(
540-
self, f: BeforeRequestCallable
541-
) -> BeforeRequestCallable:
541+
self, f: BeforeFirstRequestCallable
542+
) -> BeforeFirstRequestCallable:
542543
"""Like :meth:`Flask.before_first_request`. Such a function is
543544
executed before the first request to the application.
544545
"""

src/flask/typing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535

3636
AppOrBlueprintKey = t.Optional[str] # The App key is None, whereas blueprints are named
3737
AfterRequestCallable = t.Callable[["Response"], "Response"]
38-
BeforeRequestCallable = t.Callable[[], None]
38+
BeforeFirstRequestCallable = t.Callable[[], None]
39+
BeforeRequestCallable = t.Callable[[], t.Optional[ResponseReturnValue]]
3940
ErrorHandlerCallable = t.Callable[[Exception], ResponseReturnValue]
4041
TeardownCallable = t.Callable[[t.Optional[BaseException]], None]
4142
TemplateContextProcessorCallable = t.Callable[[], t.Dict[str, t.Any]]

0 commit comments

Comments
 (0)