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
Provenance
- Commit
96c97de- Tree
08b4725- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
7a2d5fb
6 files changed+66 added−33 removed
modified:src/flask/app.py+28 added-17 removed
449 unmodified lines450 #: 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] = []460 461 #: A list of functions that are called when the application context796 unmodified lines1258 1259 The function will be called without any arguments and its return1260 value is ignored.1261 1262+ .. deprecated:: 2.21263+ Will be removed in Flask 2.3. Run setup code when creating1264+ the application instead.1265+1266 .. versionadded:: 0.81267 """1268+ import warnings1269+1270+ 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 f1279 1280 @setupmethod287 unmodified lines1568 error handling.1569 1570 .. 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)()1580+1581+ self._got_first_request = True1582+1583 try:1584 request_started.send(self)1585 rv = self.preprocess_request()1586 if rv is None:30 unmodified lines1617 "Request finalizing failed with an error while handling an error"1618 )1619 return response1620 1594- 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).1598-1599- :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 = True1609-1621 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 linesmodified:src/flask/blueprints.py+13 added
541 unmodified lines542 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.546+547+ .. deprecated:: 2.2548+ Will be removed in Flask 2.3. Run setup code when creating549+ the application instead.550 """551+ import warnings552+553+ 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 f562 563 def after_app_request(self, f: ft.AfterRequestCallable) -> ft.AfterRequestCallable:62 unmodified linesmodified:tests/test_async.py+6 added-4 removed
105 unmodified lines106 @app.route("/")107 def index():108 return ""109 110- @app.before_first_request111- async def before_first():112- nonlocal app_first_called113- app_first_called = True110+ with pytest.deprecated_call():111 112+ @app.before_first_request113+ async def before_first():114+ nonlocal app_first_called115+ app_first_called = True116+117 @app.before_request118 async def before():119 nonlocal app_before_called120 app_before_called = True34 unmodified linesmodified:tests/test_basic.py+11 added-7 removed
1682 unmodified lines1683 1684 def test_before_first_request_functions(app, client):1685 got = []1686 1687- @app.before_first_request1688- def foo():1689- got.append(42)1687+ with pytest.deprecated_call():1688 1689+ @app.before_first_request1690+ def foo():1691+ got.append(42)1692+1693 client.get("/")1694 assert got == [42]1695 client.get("/")1696 assert got == [42]2 unmodified lines1699 1700 def test_before_first_request_functions_concurrent(app, client):1701 got = []1702 1701- @app.before_first_request1702- def foo():1703- time.sleep(0.2)1704- got.append(42)1703+ with pytest.deprecated_call():1704 1705+ @app.before_first_request1706+ def foo():1707+ time.sleep(0.2)1708+ got.append(42)1709+1710 def get_and_assert():1711 client.get("/")1712 assert got == [42]1713 310 unmodified linesmodified:tests/test_blueprints.py+5 added-3 removed
720 unmodified lines721 def test_app_request_processing(app, client):722 bp = flask.Blueprint("bp", __name__)723 evts = []724 725- @bp.before_app_first_request726- def before_first_request():727- evts.append("first")725+ with pytest.deprecated_call():726 727+ @bp.before_app_first_request728+ def before_first_request():729+ evts.append("first")730+731 @bp.before_app_request732 def before_app():733 evts.append("before")734 271 unmodified linesmodified:CHANGES.rst+3 added-2 removed
18 unmodified lines19 - 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`25 26 27 Version 2.1.328 -------------1004 unmodified lines1033 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