Merge pull request #4662 from pallets/fix-run-skip

re-add flag to skip unguarded app.run from CLI
This commit is contained in:
David Lord 2022-06-29 21:21:52 -07:00 committed by GitHub
commit 3635583ce2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 7 deletions

View File

@ -916,8 +916,8 @@ class Flask(Scaffold):
if os.environ.get("FLASK_RUN_FROM_CLI") == "true":
if not is_running_from_reloader():
click.secho(
" * Ignoring a call to 'app.run()', the server is"
" already being run with the 'flask run' command.\n"
" * Ignoring a call to 'app.run()' that would block"
" the current 'flask' CLI command.\n"
" Only call 'app.run()' in an 'if __name__ =="
' "__main__"\' guard.',
fg="red",

View File

@ -715,6 +715,11 @@ class FlaskGroup(AppGroup):
parent: click.Context | None = None,
**extra: t.Any,
) -> click.Context:
# Set a flag to tell app.run to become a no-op. If app.run was
# not in a __name__ == __main__ guard, it would start the server
# when importing, blocking whatever command is being called.
os.environ["FLASK_RUN_FROM_CLI"] = "true"
# Attempt to load .env and .flask env files. The --env-file
# option can cause another file to be loaded.
if get_load_dotenv(self.load_dotenv):

View File

@ -13,7 +13,6 @@ import pytest
from _pytest.monkeypatch import notset
from click.testing import CliRunner
from flask import _app_ctx_stack
from flask import Blueprint
from flask import current_app
from flask import Flask
@ -322,13 +321,11 @@ def test_app_cli_has_app_context(app, runner):
app = click.get_current_context().obj.load_app()
# the loaded app should be the same as current_app
same_app = current_app._get_current_object() is app
# only one app context should be pushed
stack_size = len(_app_ctx_stack._local.stack)
return same_app, stack_size, value
return same_app, value
cli = FlaskGroup(create_app=lambda: app)
result = runner.invoke(cli, ["check", "x"], standalone_mode=False)
assert result.return_value == (True, 1, True)
assert result.return_value == (True, True)
def test_with_appcontext(runner):