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
Provenance
- Commit
e21e003- Tree
da76326- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
ea93a52
3 files changed+34 added−102 removed
modified:src/flask/cli.py+13 added-57 removed
3 unmodified lines4 import platform5 import re6 import sys7 import traceback8-import warnings8 from functools import update_wrapper9 from operator import attrgetter10 from threading import Lock11 from threading import Thread20 unmodified lines32 class NoAppException(click.UsageError):33 """Raised if an application cannot be found or loaded."""34 35 37-def find_best_app(script_info, module):36+def 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 Flask22 unmodified lines63 app_factory = getattr(module, attr_name, None)64 65 if inspect.isfunction(app_factory):66 try:68- app = call_factory(script_info, app_factory)67+ app = app_factory()68 69 if isinstance(app, Flask):70 return app71 except TypeError as e:13 unmodified lines85 " to specify one."86 )87 88 90-def 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 kwargs98-99- 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_info106-107- if not args and len(sig.parameters) == 1:108- first_parameter = next(iter(sig.parameters.values()))109-110- 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)122-123- return app_factory(*args, **kwargs)124-125-89 def _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 lines111 # https://docs.python.org/2/library/sys.html#sys.exc_info112 del tb113 114 152-def find_app_by_string(script_info, module, app_name):115+def 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 Flask8 unmodified lines128 ) from None129 130 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 lines165 # 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 lines216 217 return ".".join(module_name[::-1])218 219 256-def locate_app(script_info, module_name, app_name, raise_if_not_found=True):220+def locate_app(module_name, app_name, raise_if_not_found=True):221 __traceback_hide__ = True # noqa: F841222 223 try:224 __import__(module_name)11 unmodified lines236 237 module = sys.modules[module_name]238 239 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)243 244 245 def get_version(ctx, param, value):246 if not value or ctx.resilient_parsing:112 unmodified lines359 if self._loaded_app is not None:360 return self._loaded_app361 362 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)375 376 if app:377 break378 567 unmodified lines946 )947 948 949 def 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()951 952 953 if __name__ == "__main__":954 main()modified:tests/test_cli.py+16 added-44 removed
47 unmodified lines48 assert testapp.cli.name == testapp.name49 50 51 def test_find_best_app(test_apps):52- script_info = ScriptInfo()53-52 class Module:53 app = Flask("appname")54 57- assert find_best_app(script_info, Module) == Module.app55+ assert find_best_app(Module) == Module.app56 57 class Module:58 application = Flask("appname")59 62- assert find_best_app(script_info, Module) == Module.application60+ assert find_best_app(Module) == Module.application61 62 class Module:63 myapp = Flask("appname")64 67- assert find_best_app(script_info, Module) == Module.myapp65+ assert find_best_app(Module) == Module.myapp66 67 class Module:68 @staticmethod69 def create_app():70 return Flask("appname")71 74- app = find_best_app(script_info, Module)72+ app = find_best_app(Module)73 assert isinstance(app, Flask)74 assert app.name == "appname"75 76 class Module:77 @staticmethod78 def create_app(**kwargs):79 return Flask("appname")80 83- app = find_best_app(script_info, Module)81+ app = find_best_app(Module)82 assert isinstance(app, Flask)83 assert app.name == "appname"84 85 class Module:86 @staticmethod89- def create_app(foo):90- return Flask("appname")91-92- with pytest.deprecated_call(match="Script info"):93- app = find_best_app(script_info, Module)94-95- assert isinstance(app, Flask)96- assert app.name == "appname"97-98- class Module:99- @staticmethod100- def create_app(foo=None, script_info=None):101- return Flask("appname")102-103- with pytest.deprecated_call(match="script_info"):104- app = find_best_app(script_info, Module)105-106- assert isinstance(app, Flask)107- assert app.name == "appname"108-109- class Module:110- @staticmethod87 def make_app():88 return Flask("appname")89 114- app = find_best_app(script_info, Module)90+ app = find_best_app(Module)91 assert isinstance(app, Flask)92 assert app.name == "appname"93 94 class Module:2 unmodified lines97 @staticmethod98 def create_app():99 return Flask("appname2")100 125- assert find_best_app(script_info, Module) == Module.myapp101+ assert find_best_app(Module) == Module.myapp102 103 class Module:104 myapp = Flask("appname1")105 106 @staticmethod107 def create_app():108 return Flask("appname2")109 134- assert find_best_app(script_info, Module) == Module.myapp110+ assert find_best_app(Module) == Module.myapp111 112 class Module:113 pass114 139- pytest.raises(NoAppException, find_best_app, script_info, Module)115+ pytest.raises(NoAppException, find_best_app, Module)116 117 class Module:118 myapp1 = Flask("appname1")119 myapp2 = Flask("appname2")120 145- pytest.raises(NoAppException, find_best_app, script_info, Module)121+ pytest.raises(NoAppException, find_best_app, Module)122 123 class Module:124 @staticmethod125 def create_app(foo, bar):126 return Flask("appname2")127 152- pytest.raises(NoAppException, find_best_app, script_info, Module)128+ pytest.raises(NoAppException, find_best_app, Module)129 130 class Module:131 @staticmethod132 def create_app():133 raise TypeError("bad bad factory!")134 159- pytest.raises(TypeError, find_best_app, script_info, Module)135+ pytest.raises(TypeError, find_best_app, Module)136 137 138 @pytest.mark.parametrize(139 "value,path,result",55 unmodified lines195 ("cliapp.factory", " create_app () ", "app"),196 ),197 )198 def 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 == result200 201 202 @pytest.mark.parametrize(203 "iname,aname",13 unmodified lines217 ("cliapp.message.txt", None),218 ),219 )220 def test_locate_app_raises(test_apps, iname, aname):246- info = ScriptInfo()247-221 with pytest.raises(NoAppException):249- locate_app(info, iname, aname)222+ locate_app(iname, aname)223 224 225 def 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 None228 229 # 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)232 233 234 def test_get_version(test_apps, capsys):235 from flask import __version__ as flask_version429 unmodified linesmodified:CHANGES.rst+5 added-1 removed
3 unmodified lines4 -------------5 6 Unreleased7 8+- 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`11 12+ - The CLI does not pass ``script_info`` to app factory functions.13 14+15 Version 2.0.216 -------------17 18 Released 2021-10-041178 unmodified lines