diff --git a/CHANGES.rst b/CHANGES.rst index 95a004d7..078df76a 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -93,7 +93,8 @@ Unreleased JSON response like a dict is. :issue:`4672` - When type checking, allow ``TypedDict`` to be returned from view functions. :pr:`4695` - +- It is now possible to also call ``@with_appcontext`` decorator + with parenthesis, to have additional parity with Quart. Version 2.1.3 ------------- diff --git a/src/flask/cli.py b/src/flask/cli.py index ba0db467..94acd3b6 100644 --- a/src/flask/cli.py +++ b/src/flask/cli.py @@ -408,7 +408,7 @@ class ScriptInfo: pass_script_info = click.make_pass_decorator(ScriptInfo, ensure=True) -def with_appcontext(f): +def with_appcontext(f=None): """Wraps a callback so that it's guaranteed to be executed with the script's application context. @@ -416,12 +416,19 @@ def with_appcontext(f): ``blueprint.cli`` will always have an app context available, this decorator is not required in that case. + .. versionchanged:: 2.2 + It is now also possible to call the decorator with parenthesis. + .. versionchanged:: 2.2 The app context is active for subcommands as well as the decorated callback. The app context is always available to ``app.cli`` command and parameter callbacks. """ + # Decorator was used with parenthesis + if f is None: + return with_appcontext + @click.pass_context def decorator(__ctx, *args, **kwargs): if not current_app: diff --git a/tests/test_cli.py b/tests/test_cli.py index 107cbb11..d474d0af 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -341,6 +341,19 @@ def test_with_appcontext(runner): assert result.output == "testapp\n" +def test_with_appcontext_parenthesis(runner): + @click.command() + @with_appcontext() + def testcmd(): + click.echo(current_app.name) + + obj = ScriptInfo(create_app=lambda: Flask("testapp")) + + result = runner.invoke(testcmd, obj=obj) + assert result.exit_code == 0 + assert result.output == "testapp\n" + + def test_appgroup_app_context(runner): @click.group(cls=AppGroup) def cli():