Skip to content
Myr

Merge pull request #3563 from pallets/deprecate-script-info-arg

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

deprecate passing script_info to factory

authored by David Lord · committed by GitHub · parents 7e3b8ab, fcac7f1 · Browse files · 5 files+45 added−38 removed
This commit

State

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

Diff stats

5 files+45 added−38 removed

  • tests:+24 added−23 removed
  • other:+21 added−15 removed

5 files+45 added−38 removed

modified:docs/cli.rst+3 added-7 removed
73 unmodified lines
74``app`` or ``application``, then any application instance. If no instance is75found, the command looks for a factory function named ``create_app`` or76``make_app`` that returns an instance.7778When calling an application factory, if the factory takes an argument named79``script_info``, then the :class:`~cli.ScriptInfo` instance is passed as a80keyword argument. If the application factory takes only one argument and no81parentheses follow the factory name, the :class:`~cli.ScriptInfo` instance82is passed as a positional argument. If parentheses follow the factory name,83their contents are parsed as Python literals and passes as arguments to the84function. This means that strings must still be in quotes.78If parentheses follow the factory name, their contents are parsed as79Python literals and passed as arguments to the function. This means that80strings must still be in quotes.818283Run the Development Server84--------------------------
453 unmodified lines
modified:src/flask/cli.py+15 added-8 removed
3 unmodified lines
4import platform5import re6import sys7import traceback8import warnings9from functools import update_wrapper10from operator import attrgetter11from threading import Lock12from threading import Thread
78 unmodified lines
91 of arguments. Checks for the existence of a script_info argument and calls92 the app_factory depending on that and the arguments provided.93 """94 args_spec = inspect.getfullargspec(app_factory)94 arg_names = args_spec.args95 arg_defaults = args_spec.defaults9597 if "script_info" in arg_names:96 if "script_info" in args_spec.args:97 warnings.warn(98 "The 'script_info' argument is deprecated and will not be"99 " passed to the app factory function in 2.1.",100 DeprecationWarning,101 )102 return app_factory(*arguments, script_info=script_info)103 elif arguments:104 return app_factory(*arguments)101 elif not arguments and len(arg_names) == 1 and arg_defaults is None:105 elif not arguments and len(args_spec.args) == 1 and args_spec.defaults is None:106 warnings.warn(107 "Script info is deprecated and will not be passed as the"108 " first argument to the app factory function in 2.1.",109 DeprecationWarning,110 )111 return app_factory(script_info)112113 return app_factory()114
24 unmodified lines
139 del tb140141142def find_app_by_string(script_info, module, app_name):134 """Checks if the given string is a variable name or a function. If it is a135 function, it checks for specified arguments and whether it takes a136 ``script_info`` argument and calls the function with the appropriate137 arguments.143 """Check if the given string is a variable name or a function. Call144 a function to get the app instance, or return the variable directly.145 """146 from . import Flask147148 match = re.match(r"^ *([^ ()]+) *(?:\((.*?) *,? *\))? *$", app_name)
797 unmodified lines
modified:tests/test_apps/cliapp/factory.py-4 removed
7 unmodified lines
8def create_app2(foo, bar):9 return Flask("_".join(["app2", foo, bar]))101112def create_app3(foo, script_info):13 return Flask("_".join(["app3", foo, script_info.data["test"]]))141512def no_app():13 pass
modified:tests/test_cli.py+24 added-19 removed
65 unmodified lines
66 @staticmethod67 def create_app():68 return Flask("appname")6970 assert isinstance(find_best_app(script_info, Module), Flask)71 assert find_best_app(script_info, Module).name == "appname"70 app = find_best_app(script_info, Module)71 assert isinstance(app, Flask)72 assert app.name == "appname"7374 class Module:75 @staticmethod76 def create_app(foo):77 return Flask("appname")7878 assert isinstance(find_best_app(script_info, Module), Flask)79 assert find_best_app(script_info, Module).name == "appname"79 with pytest.deprecated_call(match="Script info"):80 app = find_best_app(script_info, Module)8182 assert isinstance(app, Flask)83 assert app.name == "appname"8485 class Module:86 @staticmethod87 def create_app(foo=None, script_info=None):88 return Flask("appname")8986 assert isinstance(find_best_app(script_info, Module), Flask)87 assert find_best_app(script_info, Module).name == "appname"90 with pytest.deprecated_call(match="script_info"):91 app = find_best_app(script_info, Module)9293 assert isinstance(app, Flask)94 assert app.name == "appname"9596 class Module:97 @staticmethod98 def make_app():99 return Flask("appname")10094 assert isinstance(find_best_app(script_info, Module), Flask)95 assert find_best_app(script_info, Module).name == "appname"101 app = find_best_app(script_info, Module)102 assert isinstance(app, Flask)103 assert app.name == "appname"104105 class Module:106 myapp = Flask("appname1")107
98 unmodified lines
206 # no script_info207 ("cliapp.factory", 'create_app2("foo", "bar")', "app2_foo_bar"),208 # trailing comma space209 ("cliapp.factory", 'create_app2("foo", "bar", )', "app2_foo_bar"),202 # takes script_info203 ("cliapp.factory", 'create_app3("foo")', "app3_foo_spam"),210 # strip whitespace211 ("cliapp.factory", " create_app () ", "app"),212 ),213)214def test_locate_app(test_apps, iname, aname, result):215 info = ScriptInfo()210 info.data["test"] = "spam"216 assert locate_app(info, iname, aname).name == result217218219@pytest.mark.parametrize(
70 unmodified lines
290 app = obj.load_app()291 assert app.name == "testapp"292 assert obj.load_app() is app293289 def create_app(info):294 def create_app():295 return Flask("createapp")296297 obj = ScriptInfo(create_app=create_app)298 app = obj.load_app()
29 unmodified lines
328 @with_appcontext329 def testcmd():330 click.echo(current_app.name)331327 obj = ScriptInfo(create_app=lambda info: Flask("testapp"))332 obj = ScriptInfo(create_app=lambda: Flask("testapp"))333334 result = runner.invoke(testcmd, obj=obj)335 assert result.exit_code == 0336 assert result.output == "testapp\n"
17 unmodified lines
354 @subgroup.command(with_appcontext=True)355 def test2():356 click.echo(current_app.name)357353 obj = ScriptInfo(create_app=lambda info: Flask("testappgroup"))358 obj = ScriptInfo(create_app=lambda: Flask("testappgroup"))359360 result = runner.invoke(cli, ["test"], obj=obj)361 assert result.exit_code == 0362 assert result.output == "testappgroup\n"
5 unmodified lines
368369def test_flaskgroup(runner):370 """Test FlaskGroup."""371367 def create_app(info):372 def create_app():373 return Flask("flaskgroup")374375 @click.group(cls=FlaskGroup, create_app=create_app)376 def cli(**params):
11 unmodified lines
388@pytest.mark.parametrize("set_debug_flag", (True, False))389def test_flaskgroup_debug(runner, set_debug_flag):390 """Test FlaskGroup debug flag behavior."""391387 def create_app(info):392 def create_app():393 app = Flask("flaskgroup")394 app.debug = True395 return app396
12 unmodified lines
409410def test_print_exceptions(runner):411 """Print the stacktrace if the CLI."""412408 def create_app(info):413 def create_app():414 raise Exception("oh no")415 return Flask("flaskgroup")416417 @click.group(cls=FlaskGroup, create_app=create_app)
8 unmodified lines
426427class TestRoutes:428 @pytest.fixture429 def invoke(self, runner):425 def create_app(info):430 def create_app():431 app = Flask(__name__)432 app.testing = True433434 @app.route("/get_post/<int:x>/<int:y>", methods=["GET", "POST"])
10 unmodified lines
445 return partial(runner.invoke, cli)446447 @pytest.fixture448 def invoke_no_routes(self, runner):444 def create_app(info):449 def create_app():450 app = Flask(__name__, static_folder=None)451 app.testing = True452453 return app
204 unmodified lines
modified:CHANGES.rst+3 added
7 unmodified lines
8- Drop support for Python 2 and 3.5.9- JSON support no longer uses simplejson. To use another JSON module,10 override ``app.json_encoder`` and ``json_decoder``. :issue:`3555`11- The ``encoding`` option to JSON functions is deprecated. :pr:`3562`12- Passing ``script_info`` to app factory functions is deprecated. This13 was not portable outside the ``flask`` command. Use14 ``click.get_current_context().obj`` if it's needed. :issue:`3552`15- Add :meth:`sessions.SessionInterface.get_cookie_name` to allow16 setting the session cookie name dynamically. :pr:`3369`17- Add :meth:`Config.from_file` to load config using arbitrary file18 loaders, such as ``toml.load`` or ``json.load``.
1024 unmodified lines