Skip to content
Myr

Auto-detect create_app and make_app factory functions

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

authored by Hendrik Makait · committed by Hendrik Makait · parent b11f735 · Browse files · 2 files+49 added−0 removed
This commit

State

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

Diff stats

2 files+49 added−0 removed

  • tests:+34 added−0 removed
  • other:+15 added−0 removed

2 files+49 added−0 removed

modified:flask/cli.py+15 added
44 unmodified lines
45 if isinstance(v, Flask)]4647 if len(matches) == 1:48 return matches[0]4950 # Search for app factory callables.51 for attr_name in 'create_app', 'make_app':52 app_factory = getattr(module, attr_name, None)53 if app_factory is not None and callable(app_factory):54 try:55 app = app_factory()56 if app is not None and isinstance(app, Flask):57 return app58 except TypeError:59 raise NoAppException('Auto-detected "%s()" in module "%s", '60 'but could not call it without '61 'specifying arguments.'62 % (attr_name, module.__name__))6364 raise NoAppException('Failed to find application in module "%s". Are '65 'you sure it contains a Flask application? Maybe '66 'you wrapped it in a WSGI middleware or you are '67 'using a factory function.' % module.__name__)
528 unmodified lines
modified:tests/test_cli.py+34 added
50 unmodified lines
51 myapp = Flask('appname')52 assert find_best_app(Module) == Module.myapp5354 class Module:55 @staticmethod56 def create_app():57 return Flask('appname')58 assert isinstance(find_best_app(Module), Flask)59 assert find_best_app(Module).name == 'appname'6061 class Module:62 @staticmethod63 def make_app():64 return Flask('appname')65 assert isinstance(find_best_app(Module), Flask)66 assert find_best_app(Module).name == 'appname'6768 class Module:69 myapp = Flask('appname1')70 @staticmethod71 def create_app():72 return Flask('appname2')73 assert find_best_app(Module) == Module.myapp7475 class Module:76 myapp = Flask('appname1')77 @staticmethod78 def create_app(foo):79 return Flask('appname2')80 assert find_best_app(Module) == Module.myapp8182 class Module:83 pass84 pytest.raises(NoAppException, find_best_app, Module)8586 class Module:87 myapp1 = Flask('appname1')88 myapp2 = Flask('appname2')89 pytest.raises(NoAppException, find_best_app, Module)9091 class Module:92 @staticmethod93 def create_app(foo):94 return Flask('appname2')95 pytest.raises(NoAppException, find_best_app, Module)969798def test_prepare_exec_for_file(test_apps):99 """Expect the correct path to be set and the correct module name to be returned.100101 :func:`prepare_exec_for_file` has a side effect, where
203 unmodified lines