Skip to content
Myr

remove deprecated script_info factory arg

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 ea93a52 · Browse files · 3 files+34 added−102 removed
This commit

State

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

Diff stats

3 files+34 added−102 removed

  • tests:+16 added−44 removed
  • other:+18 added−58 removed

3 files+34 added−102 removed

modified:src/flask/cli.py+13 added-57 removed
3 unmodified lines
4import platform5import re6import sys7import traceback8import warnings8from functools import update_wrapper9from operator import attrgetter10from threading import Lock11from threading import Thread
20 unmodified lines
32class NoAppException(click.UsageError):33 """Raised if an application cannot be found or loaded."""343537def find_best_app(script_info, module):36def find_best_app(module):37 """Given a module instance this tries to find the best possible38 application in the module or raises an exception.39 """40 from . import Flask
22 unmodified lines
63 app_factory = getattr(module, attr_name, None)6465 if inspect.isfunction(app_factory):66 try:68 app = call_factory(script_info, app_factory)67 app = app_factory()6869 if isinstance(app, Flask):70 return app71 except TypeError as e:
13 unmodified lines
85 " to specify one."86 )878890def call_factory(script_info, app_factory, args=None, kwargs=None):91 """Takes an app factory, a ``script_info` object and optionally a tuple92 of arguments. Checks for the existence of a script_info argument and calls93 the app_factory depending on that and the arguments provided.94 """95 sig = inspect.signature(app_factory)96 args = [] if args is None else args97 kwargs = {} if kwargs is None else kwargs9899 if "script_info" in sig.parameters:100 warnings.warn(101 "The 'script_info' argument is deprecated and will not be"102 " passed to the app factory function in Flask 2.1.",103 DeprecationWarning,104 )105 kwargs["script_info"] = script_info106107 if not args and len(sig.parameters) == 1:108 first_parameter = next(iter(sig.parameters.values()))109110 if (111 first_parameter.default is inspect.Parameter.empty112 # **kwargs is reported as an empty default, ignore it113 and first_parameter.kind is not inspect.Parameter.VAR_KEYWORD114 ):115 warnings.warn(116 "Script info is deprecated and will not be passed as the"117 " single argument to the app factory function in Flask"118 " 2.1.",119 DeprecationWarning,120 )121 args.append(script_info)122123 return app_factory(*args, **kwargs)12412589def _called_with_wrong_args(f):90 """Check whether calling a function raised a ``TypeError`` because91 the call failed or because something in the factory raised the92 error.
18 unmodified lines
111 # https://docs.python.org/2/library/sys.html#sys.exc_info112 del tb113114152def find_app_by_string(script_info, module, app_name):115def find_app_by_string(module, app_name):116 """Check if the given string is a variable name or a function. Call117 a function to get the app instance, or return the variable directly.118 """119 from . import Flask
8 unmodified lines
128 ) from None129130 if isinstance(expr, ast.Name):131 name = expr.id169 args = kwargs = None132 args = []133 kwargs = {}134 elif isinstance(expr, ast.Call):135 # Ensure the function name is an attribute name only.136 if not isinstance(expr.func, ast.Name):137 raise NoAppException(
27 unmodified lines
165 # If the attribute is a function, call it with any args and kwargs166 # to get the real application.167 if inspect.isfunction(attr):168 try:205 app = call_factory(script_info, attr, args, kwargs)169 app = attr(*args, **kwargs)170 except TypeError as e:171 if not _called_with_wrong_args(attr):172 raise173
42 unmodified lines
216217 return ".".join(module_name[::-1])218219256def locate_app(script_info, module_name, app_name, raise_if_not_found=True):220def locate_app(module_name, app_name, raise_if_not_found=True):221 __traceback_hide__ = True # noqa: F841222223 try:224 __import__(module_name)
11 unmodified lines
236237 module = sys.modules[module_name]238239 if app_name is None:276 return find_best_app(script_info, module)240 return find_best_app(module)241 else:278 return find_app_by_string(script_info, module, app_name)242 return find_app_by_string(module, app_name)243244245def get_version(ctx, param, value):246 if not value or ctx.resilient_parsing:
112 unmodified lines
359 if self._loaded_app is not None:360 return self._loaded_app361362 if self.create_app is not None:399 app = call_factory(self, self.create_app)363 app = self.create_app()364 else:365 if self.app_import_path:366 path, name = (367 re.split(r":(?![\\/])", self.app_import_path, 1) + [None]368 )[:2]369 import_name = prepare_import(path)406 app = locate_app(self, import_name, name)370 app = locate_app(import_name, name)371 else:372 for path in ("wsgi.py", "app.py"):373 import_name = prepare_import(path)410 app = locate_app(self, import_name, None, raise_if_not_found=False)374 app = locate_app(import_name, None, raise_if_not_found=False)375376 if app:377 break378
567 unmodified lines
946)947948949def main() -> None:986 if int(click.__version__[0]) < 8:987 warnings.warn(988 "Using the `flask` cli with Click 7 is deprecated and"989 " will not be supported starting with Flask 2.1."990 " Please upgrade to Click 8 as soon as possible.",991 DeprecationWarning,992 )993 # TODO omit sys.argv once https://github.com/pallets/click/issues/536 is fixed994 cli.main(args=sys.argv[1:])950 cli.main()951952953if __name__ == "__main__":954 main()
modified:tests/test_cli.py+16 added-44 removed
47 unmodified lines
48 assert testapp.cli.name == testapp.name495051def test_find_best_app(test_apps):52 script_info = ScriptInfo()5352 class Module:53 app = Flask("appname")5457 assert find_best_app(script_info, Module) == Module.app55 assert find_best_app(Module) == Module.app5657 class Module:58 application = Flask("appname")5962 assert find_best_app(script_info, Module) == Module.application60 assert find_best_app(Module) == Module.application6162 class Module:63 myapp = Flask("appname")6467 assert find_best_app(script_info, Module) == Module.myapp65 assert find_best_app(Module) == Module.myapp6667 class Module:68 @staticmethod69 def create_app():70 return Flask("appname")7174 app = find_best_app(script_info, Module)72 app = find_best_app(Module)73 assert isinstance(app, Flask)74 assert app.name == "appname"7576 class Module:77 @staticmethod78 def create_app(**kwargs):79 return Flask("appname")8083 app = find_best_app(script_info, Module)81 app = find_best_app(Module)82 assert isinstance(app, Flask)83 assert app.name == "appname"8485 class Module:86 @staticmethod89 def create_app(foo):90 return Flask("appname")9192 with pytest.deprecated_call(match="Script info"):93 app = find_best_app(script_info, Module)9495 assert isinstance(app, Flask)96 assert app.name == "appname"9798 class Module:99 @staticmethod100 def create_app(foo=None, script_info=None):101 return Flask("appname")102103 with pytest.deprecated_call(match="script_info"):104 app = find_best_app(script_info, Module)105106 assert isinstance(app, Flask)107 assert app.name == "appname"108109 class Module:110 @staticmethod87 def make_app():88 return Flask("appname")89114 app = find_best_app(script_info, Module)90 app = find_best_app(Module)91 assert isinstance(app, Flask)92 assert app.name == "appname"9394 class Module:
2 unmodified lines
97 @staticmethod98 def create_app():99 return Flask("appname2")100125 assert find_best_app(script_info, Module) == Module.myapp101 assert find_best_app(Module) == Module.myapp102103 class Module:104 myapp = Flask("appname1")105106 @staticmethod107 def create_app():108 return Flask("appname2")109134 assert find_best_app(script_info, Module) == Module.myapp110 assert find_best_app(Module) == Module.myapp111112 class Module:113 pass114139 pytest.raises(NoAppException, find_best_app, script_info, Module)115 pytest.raises(NoAppException, find_best_app, Module)116117 class Module:118 myapp1 = Flask("appname1")119 myapp2 = Flask("appname2")120145 pytest.raises(NoAppException, find_best_app, script_info, Module)121 pytest.raises(NoAppException, find_best_app, Module)122123 class Module:124 @staticmethod125 def create_app(foo, bar):126 return Flask("appname2")127152 pytest.raises(NoAppException, find_best_app, script_info, Module)128 pytest.raises(NoAppException, find_best_app, Module)129130 class Module:131 @staticmethod132 def create_app():133 raise TypeError("bad bad factory!")134159 pytest.raises(TypeError, find_best_app, script_info, Module)135 pytest.raises(TypeError, find_best_app, Module)136137138@pytest.mark.parametrize(139 "value,path,result",
55 unmodified lines
195 ("cliapp.factory", " create_app () ", "app"),196 ),197)198def test_locate_app(test_apps, iname, aname, result):223 info = ScriptInfo()224 assert locate_app(info, iname, aname).name == result199 assert locate_app(iname, aname).name == result200201202@pytest.mark.parametrize(203 "iname,aname",
13 unmodified lines
217 ("cliapp.message.txt", None),218 ),219)220def test_locate_app_raises(test_apps, iname, aname):246 info = ScriptInfo()247221 with pytest.raises(NoAppException):249 locate_app(info, iname, aname)222 locate_app(iname, aname)223224225def test_locate_app_suppress_raise(test_apps):253 info = ScriptInfo()254 app = locate_app(info, "notanapp.py", None, raise_if_not_found=False)226 app = locate_app("notanapp.py", None, raise_if_not_found=False)227 assert app is None228229 # only direct import error is suppressed230 with pytest.raises(NoAppException):259 locate_app(info, "cliapp.importerrorapp", None, raise_if_not_found=False)231 locate_app("cliapp.importerrorapp", None, raise_if_not_found=False)232233234def test_get_version(test_apps, capsys):235 from flask import __version__ as flask_version
429 unmodified lines
modified:CHANGES.rst+5 added-1 removed
3 unmodified lines
4-------------56Unreleased78- Drop support for Python 3.6. :pr:`4335`8- Update Click dependency to >= 8.0.9- Update Click dependency to >= 8.0. :pr:`4008`10- Remove previously deprecated code. :pr:`4337`1112 - The CLI does not pass ``script_info`` to app factory functions.131415Version 2.0.216-------------1718Released 2021-10-04
1178 unmodified lines