deprecate passing script_info to factory
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 7e3b8ab · 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
Provenance
- Commit
fcac7f1- Tree
722bc7a- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
7e3b8ab
5 files changed+45 added−38 removed
modified:docs/cli.rst+3 added-7 removed
73 unmodified lines74 ``app`` or ``application``, then any application instance. If no instance is75 found, the command looks for a factory function named ``create_app`` or76 ``make_app`` that returns an instance.77 78-When calling an application factory, if the factory takes an argument named79-``script_info``, then the :class:`~cli.ScriptInfo` instance is passed as a80-keyword argument. If the application factory takes only one argument and no81-parentheses follow the factory name, the :class:`~cli.ScriptInfo` instance82-is passed as a positional argument. If parentheses follow the factory name,83-their contents are parsed as Python literals and passes as arguments to the84-function. This means that strings must still be in quotes.78+If parentheses follow the factory name, their contents are parsed as79+Python literals and passed as arguments to the function. This means that80+strings must still be in quotes.81 82 83 Run the Development Server84 --------------------------453 unmodified linesmodified:src/flask/cli.py+15 added-8 removed
3 unmodified lines4 import platform5 import re6 import sys7 import traceback8+import warnings9 from functools import update_wrapper10 from operator import attrgetter11 from threading import Lock12 from threading import Thread78 unmodified lines91 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.defaults95 97- 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)112 113 return app_factory()114 24 unmodified lines139 del tb140 141 142 def 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 Flask147 148 match = re.match(r"^ *([^ ()]+) *(?:\((.*?) *,? *\))? *$", app_name)797 unmodified linesmodified:tests/test_apps/cliapp/factory.py-4 removed
7 unmodified lines8 def create_app2(foo, bar):9 return Flask("_".join(["app2", foo, bar]))10 11 12-def create_app3(foo, script_info):13- return Flask("_".join(["app3", foo, script_info.data["test"]]))14-15-12 def no_app():13 passmodified:tests/test_cli.py+24 added-19 removed
65 unmodified lines66 @staticmethod67 def create_app():68 return Flask("appname")69 70- 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"73 74 class Module:75 @staticmethod76 def create_app(foo):77 return Flask("appname")78 78- 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)81 82+ assert isinstance(app, Flask)83+ assert app.name == "appname"84+85 class Module:86 @staticmethod87 def create_app(foo=None, script_info=None):88 return Flask("appname")89 86- 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)92 93+ assert isinstance(app, Flask)94+ assert app.name == "appname"95+96 class Module:97 @staticmethod98 def make_app():99 return Flask("appname")100 94- 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"104 105 class Module:106 myapp = Flask("appname1")107 98 unmodified lines206 # 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 )214 def test_locate_app(test_apps, iname, aname, result):215 info = ScriptInfo()210- info.data["test"] = "spam"216 assert locate_app(info, iname, aname).name == result217 218 219 @pytest.mark.parametrize(70 unmodified lines290 app = obj.load_app()291 assert app.name == "testapp"292 assert obj.load_app() is app293 289- def create_app(info):294+ def create_app():295 return Flask("createapp")296 297 obj = ScriptInfo(create_app=create_app)298 app = obj.load_app()29 unmodified lines328 @with_appcontext329 def testcmd():330 click.echo(current_app.name)331 327- obj = ScriptInfo(create_app=lambda info: Flask("testapp"))332+ obj = ScriptInfo(create_app=lambda: Flask("testapp"))333 334 result = runner.invoke(testcmd, obj=obj)335 assert result.exit_code == 0336 assert result.output == "testapp\n"17 unmodified lines354 @subgroup.command(with_appcontext=True)355 def test2():356 click.echo(current_app.name)357 353- obj = ScriptInfo(create_app=lambda info: Flask("testappgroup"))358+ obj = ScriptInfo(create_app=lambda: Flask("testappgroup"))359 360 result = runner.invoke(cli, ["test"], obj=obj)361 assert result.exit_code == 0362 assert result.output == "testappgroup\n"5 unmodified lines368 369 def test_flaskgroup(runner):370 """Test FlaskGroup."""371 367- def create_app(info):372+ def create_app():373 return Flask("flaskgroup")374 375 @click.group(cls=FlaskGroup, create_app=create_app)376 def cli(**params):11 unmodified lines388 @pytest.mark.parametrize("set_debug_flag", (True, False))389 def test_flaskgroup_debug(runner, set_debug_flag):390 """Test FlaskGroup debug flag behavior."""391 387- def create_app(info):392+ def create_app():393 app = Flask("flaskgroup")394 app.debug = True395 return app396 12 unmodified lines409 410 def test_print_exceptions(runner):411 """Print the stacktrace if the CLI."""412 408- def create_app(info):413+ def create_app():414 raise Exception("oh no")415 return Flask("flaskgroup")416 417 @click.group(cls=FlaskGroup, create_app=create_app)8 unmodified lines426 427 class TestRoutes:428 @pytest.fixture429 def invoke(self, runner):425- def create_app(info):430+ def create_app():431 app = Flask(__name__)432 app.testing = True433 434 @app.route("/get_post/<int:x>/<int:y>", methods=["GET", "POST"])10 unmodified lines445 return partial(runner.invoke, cli)446 447 @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 = True452 453 return app204 unmodified linesmodified:CHANGES.rst+3 added
7 unmodified lines8 - 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