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
Provenance
- Commit
ced719e- Tree
856095f- Author
Hendrik Makait <hendrik.makait@googlemail.com>- Committer
Hendrik Makait <hendrik.makait@googlemail.com>- Parent
b11f735
2 files changed+49 added−0 removed
modified:flask/cli.py+15 added
44 unmodified lines45 if isinstance(v, Flask)]46 47 if len(matches) == 1:48 return matches[0]49+50+ # 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__))63+64 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 linesmodified:tests/test_cli.py+34 added
50 unmodified lines51 myapp = Flask('appname')52 assert find_best_app(Module) == Module.myapp53 54 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'60+61+ 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'67+68+ class Module:69+ myapp = Flask('appname1')70+ @staticmethod71+ def create_app():72+ return Flask('appname2')73+ assert find_best_app(Module) == Module.myapp74+75+ class Module:76+ myapp = Flask('appname1')77+ @staticmethod78+ def create_app(foo):79+ return Flask('appname2')80+ assert find_best_app(Module) == Module.myapp81+82+ class Module:83 pass84 pytest.raises(NoAppException, find_best_app, Module)85 86 class Module:87 myapp1 = Flask('appname1')88 myapp2 = Flask('appname2')89 pytest.raises(NoAppException, find_best_app, Module)90 91+ class Module:92+ @staticmethod93+ def create_app(foo):94+ return Flask('appname2')95+ pytest.raises(NoAppException, find_best_app, Module)96 97+98 def test_prepare_exec_for_file(test_apps):99 """Expect the correct path to be set and the correct module name to be returned.100 101 :func:`prepare_exec_for_file` has a side effect, where203 unmodified lines