Merge pull request #2297 from HndrkMkt/#2265-auto-detect-create-app-function
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Auto-detect create_app() in find_best_app() #2265
authored by David Lord · committed by GitHub · parents a693f22, 01ddf54 · Browse files · 3 files·+72 added−8 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+72 added−8 removed
- tests:+34 added−0 removed
- other:+38 added−8 removed
Provenance
- Commit
0c94908- Tree
0ad33e0- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
a693f22- Parent 2
01ddf54
3 files changed+72 added−8 removed
modified:flask/cli.py+35 added-8 removed
33 unmodified lines34 """35 from . import Flask36 37 # Search for the most common names first.38- for attr_name in 'app', 'application':38+ for attr_name in ('app', 'application'):39 app = getattr(module, attr_name, None)40- if app is not None and isinstance(app, Flask):40+ if isinstance(app, Flask):41 return app42 43 # Otherwise find the only object that is a Flask instance.44- matches = [v for k, v in iteritems(module.__dict__)45- if isinstance(v, Flask)]44+ matches = [45+ v for k, v in iteritems(module.__dict__) if isinstance(v, Flask)46+ ]47 48 if len(matches) == 1:49 return matches[0]49- raise NoAppException('Failed to find application in module "%s". Are '50- 'you sure it contains a Flask application? Maybe '51- 'you wrapped it in a WSGI middleware or you are '52- 'using a factory function.' % module.__name__)50+ elif len(matches) > 1:51+ raise NoAppException(52+ 'Auto-detected multiple Flask applications in module "{module}".'53+ ' Use "FLASK_APP={module}:name" to specify the correct'54+ ' one.'.format(module=module.__name__)55+ )56 57+ # Search for app factory callables.58+ for attr_name in ('create_app', 'make_app'):59+ app_factory = getattr(module, attr_name, None)60 61+ if callable(app_factory):62+ try:63+ app = app_factory()64+65+ if isinstance(app, Flask):66+ return app67+ except TypeError:68+ raise NoAppException(69+ 'Auto-detected "{callable}()" in module "{module}", but '70+ 'could not call it without specifying arguments.'.format(71+ callable=attr_name, module=module.__name__72+ )73+ )74+75+ raise NoAppException(76+ 'Failed to find application in module "{module}". Are you sure '77+ 'it contains a Flask application? Maybe you wrapped it in a WSGI '78+ 'middleware.'.format(module=module.__name__)79+ )80+81+82 def prepare_exec_for_file(filename):83 """Given a filename this will try to calculate the python path, add it84 to the search path and return the actual module name that is expected.85 """522 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 linesmodified:CHANGES+3 added
38 unmodified lines39 (`#2282`_)40 - Allow IP address as exact session cookie domain. (`#2282`_)41 - ``SESSION_COOKIE_DOMAIN`` is set if it is detected through ``SERVER_NAME``.42 (`#2282`_)43+- Auto-detect zero-argument app factory called ``create_app`` or ``make_app``44+ from ``FLASK_APP``. (`#2297`_)45 46 .. _#1489: https://github.com/pallets/flask/pull/148947 .. _#1898: https://github.com/pallets/flask/pull/189848 .. _#1936: https://github.com/pallets/flask/pull/19362 unmodified lines51 .. _#2254: https://github.com/pallets/flask/pull/225452 .. _#2256: https://github.com/pallets/flask/pull/225653 .. _#2259: https://github.com/pallets/flask/pull/225954 .. _#2282: https://github.com/pallets/flask/pull/228255+.. _#2297: https://github.com/pallets/flask/pull/229756 57 Version 0.12.258 --------------59 615 unmodified lines