Skip to content
Myr

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

3 files+72 added−8 removed

modified:flask/cli.py+35 added-8 removed
33 unmodified lines
34 """35 from . import Flask3637 # 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 app4243 # 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 ]4748 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 )5657 # Search for app factory callables.58 for attr_name in ('create_app', 'make_app'):59 app_factory = getattr(module, attr_name, None)6061 if callable(app_factory):62 try:63 app = app_factory()6465 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 )7475 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 )808182def 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 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
modified:CHANGES+3 added
38 unmodified lines
39 (`#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`_)4546.. _#1489: https://github.com/pallets/flask/pull/148947.. _#1898: https://github.com/pallets/flask/pull/189848.. _#1936: https://github.com/pallets/flask/pull/1936
2 unmodified lines
51.. _#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/22975657Version 0.12.258--------------59
615 unmodified lines