Skip to content
Myr

deprecate before_first_request

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by David Lord · parent 7a2d5fb · Browse files · 6 files+66 added−33 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+66 added−33 removed

  • tests:+22 added−14 removed
  • other:+44 added−19 removed

6 files+66 added−33 removed

modified:src/flask/app.py+28 added-17 removed
449 unmodified lines
450 #: A list of functions that will be called at the beginning of the451 #: first request to this instance. To register a function, use the452 #: :meth:`before_first_request` decorator.453 #:454 #: .. deprecated:: 2.2455 #: Will be removed in Flask 2.3. Run setup code when456 #: creating the application instead.457 #:458 #: .. versionadded:: 0.8459 self.before_first_request_funcs: t.List[ft.BeforeFirstRequestCallable] = []460461 #: A list of functions that are called when the application context
796 unmodified lines
12581259 The function will be called without any arguments and its return1260 value is ignored.12611262 .. deprecated:: 2.21263 Will be removed in Flask 2.3. Run setup code when creating1264 the application instead.12651266 .. versionadded:: 0.81267 """1268 import warnings12691270 warnings.warn(1271 "'before_first_request' is deprecated and will be removed"1272 " in Flask 2.3. Run setup code while creating the"1273 " application instead.",1274 DeprecationWarning,1275 stacklevel=2,1276 )1277 self.before_first_request_funcs.append(f)1278 return f12791280 @setupmethod
287 unmodified lines
1568 error handling.15691570 .. versionadded:: 0.71571 """1555 self.try_trigger_before_first_request_functions()1572 # Run before_first_request functions if this is the thread's first request.1573 # Inlined to avoid a method call on subsequent requests.1574 # This is deprecated, will be removed in Flask 2.3.1575 if not self._got_first_request:1576 with self._before_request_lock:1577 if not self._got_first_request:1578 for func in self.before_first_request_funcs:1579 self.ensure_sync(func)()15801581 self._got_first_request = True15821583 try:1584 request_started.send(self)1585 rv = self.preprocess_request()1586 if rv is None:
30 unmodified lines
1617 "Request finalizing failed with an error while handling an error"1618 )1619 return response16201594 def try_trigger_before_first_request_functions(self) -> None:1595 """Called before each request and will ensure that it triggers1596 the :attr:`before_first_request_funcs` and only exactly once per1597 application instance (which means process usually).15981599 :internal:1600 """1601 if self._got_first_request:1602 return1603 with self._before_request_lock:1604 if self._got_first_request:1605 return1606 for func in self.before_first_request_funcs:1607 self.ensure_sync(func)()1608 self._got_first_request = True16091621 def make_default_options_response(self) -> Response:1622 """This method is called to create the default ``OPTIONS`` response.1623 This can be changed through subclassing to change the default1624 behavior of ``OPTIONS`` responses.
670 unmodified lines
modified:src/flask/blueprints.py+13 added
541 unmodified lines
542 self, f: ft.BeforeFirstRequestCallable543 ) -> ft.BeforeFirstRequestCallable:544 """Like :meth:`Flask.before_first_request`. Such a function is545 executed before the first request to the application.546547 .. deprecated:: 2.2548 Will be removed in Flask 2.3. Run setup code when creating549 the application instead.550 """551 import warnings552553 warnings.warn(554 "'before_app_first_request' is deprecated and will be"555 " removed in Flask 2.3. Use 'record_once' instead to run"556 " setup code when registering the blueprint.",557 DeprecationWarning,558 stacklevel=2,559 )560 self.record_once(lambda s: s.app.before_first_request_funcs.append(f))561 return f562563 def after_app_request(self, f: ft.AfterRequestCallable) -> ft.AfterRequestCallable:
62 unmodified lines
modified:tests/test_async.py+6 added-4 removed
105 unmodified lines
106 @app.route("/")107 def index():108 return ""109110 @app.before_first_request111 async def before_first():112 nonlocal app_first_called113 app_first_called = True110 with pytest.deprecated_call():111112 @app.before_first_request113 async def before_first():114 nonlocal app_first_called115 app_first_called = True116117 @app.before_request118 async def before():119 nonlocal app_before_called120 app_before_called = True
34 unmodified lines
modified:tests/test_basic.py+11 added-7 removed
1682 unmodified lines
16831684def test_before_first_request_functions(app, client):1685 got = []16861687 @app.before_first_request1688 def foo():1689 got.append(42)1687 with pytest.deprecated_call():16881689 @app.before_first_request1690 def foo():1691 got.append(42)16921693 client.get("/")1694 assert got == [42]1695 client.get("/")1696 assert got == [42]
2 unmodified lines
16991700def test_before_first_request_functions_concurrent(app, client):1701 got = []17021701 @app.before_first_request1702 def foo():1703 time.sleep(0.2)1704 got.append(42)1703 with pytest.deprecated_call():17041705 @app.before_first_request1706 def foo():1707 time.sleep(0.2)1708 got.append(42)17091710 def get_and_assert():1711 client.get("/")1712 assert got == [42]1713
310 unmodified lines
modified:tests/test_blueprints.py+5 added-3 removed
720 unmodified lines
721def test_app_request_processing(app, client):722 bp = flask.Blueprint("bp", __name__)723 evts = []724725 @bp.before_app_first_request726 def before_first_request():727 evts.append("first")725 with pytest.deprecated_call():726727 @bp.before_app_first_request728 def before_first_request():729 evts.append("first")730731 @bp.before_app_request732 def before_app():733 evts.append("before")734
271 unmodified lines
modified:CHANGES.rst+3 added-2 removed
18 unmodified lines
19- Use Blueprint decorators and functions intended for setup after20 registering the blueprint will show a warning. In the next version,21 this will become an error just like the application setup methods.22 :issue:`4571`23- ``before_first_request`` is deprecated. Run setup code when creating24 the application instead. :issue:`4605`252627Version 2.1.328-------------
1004 unmodified lines
1033 view was attached after the first request was handled. This gives1034 earlier feedback when users forget to import view code ahead of1035 time.1036- Added the ability to register callbacks that are only triggered once1035 at the beginning of the first request.1036 (:meth:`Flask.before_first_request`)1037 at the beginning of the first request. (``before_first_request``)1038- Malformed JSON data will now trigger a bad request HTTP exception1039 instead of a value error which usually would result in a 5001040 internal server error if not handled. This is a backwards1041 incompatible change.
278 unmodified lines