From 29dee992cce3bed9ab4937110945f033ec73202e Mon Sep 17 00:00:00 2001 From: Leron Gray Date: Thu, 17 Dec 2020 17:36:46 -0800 Subject: [PATCH 1/6] fix context settings for single command --- tests/test_others.py | 11 +++++++++++ typer/main.py | 7 ++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/test_others.py b/tests/test_others.py index 897941a08..8e56d32f4 100644 --- a/tests/test_others.py +++ b/tests/test_others.py @@ -222,3 +222,14 @@ def main(arg1, arg2: int, arg3: "int", arg4: bool = False, arg5: "bool" = False) "arg1: Hello\narg2: 2\narg3: 3\narg4: True\narg5: True\n" in result.stdout ) + + +def test_context_settings_inheritance_single_command(): + app = typer.Typer(context_settings=dict(help_option_names=["-h", "--help"])) + + @app.command() + def main(name: str = typer.Argument("Camila")): + typer.echo(f"{name} wrote this test.") + + result = runner.invoke(app, ["main", "-h"]) + assert "Show this message and exit." in result.stdout diff --git a/typer/main.py b/typer/main.py index 02d9a5d7f..87a56be0b 100644 --- a/typer/main.py +++ b/typer/main.py @@ -236,7 +236,12 @@ def get_command(typer_instance: Typer) -> click.Command: return click_command elif len(typer_instance.registered_commands) == 1: # Create a single Command - click_command = get_command_from_info(typer_instance.registered_commands[0]) + single_command = typer_instance.registered_commands[0] + + if not single_command.context_settings: + single_command.context_settings = typer_instance.info.context_settings + + click_command = get_command_from_info(single_command) if typer_instance._add_completion: click_command.params.append(click_install_param) click_command.params.append(click_show_param) From 6481694a62079016207bf3c95c13743a4493fec8 Mon Sep 17 00:00:00 2001 From: Leron Gray Date: Thu, 17 Dec 2020 18:01:03 -0800 Subject: [PATCH 2/6] fix missing condition --- .coveragerc | 8 -------- typer/main.py | 2 +- 2 files changed, 1 insertion(+), 9 deletions(-) delete mode 100644 .coveragerc diff --git a/.coveragerc b/.coveragerc deleted file mode 100644 index 18cdff194..000000000 --- a/.coveragerc +++ /dev/null @@ -1,8 +0,0 @@ -[run] - -source = - typer - tests - docs_src - -parallel = True diff --git a/typer/main.py b/typer/main.py index 87a56be0b..464096857 100644 --- a/typer/main.py +++ b/typer/main.py @@ -238,7 +238,7 @@ def get_command(typer_instance: Typer) -> click.Command: # Create a single Command single_command = typer_instance.registered_commands[0] - if not single_command.context_settings: + if not single_command.context_settings and typer_instance.info.context_settings: single_command.context_settings = typer_instance.info.context_settings click_command = get_command_from_info(single_command) From 80efdd421ebfc2ed27220ce7ff3add3b6401513c Mon Sep 17 00:00:00 2001 From: Leron Gray Date: Thu, 17 Dec 2020 18:22:40 -0800 Subject: [PATCH 3/6] Revert "fix missing condition" This reverts commit 6481694a62079016207bf3c95c13743a4493fec8. --- .coveragerc | 8 ++++++++ typer/main.py | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 .coveragerc diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 000000000..18cdff194 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,8 @@ +[run] + +source = + typer + tests + docs_src + +parallel = True diff --git a/typer/main.py b/typer/main.py index 464096857..87a56be0b 100644 --- a/typer/main.py +++ b/typer/main.py @@ -238,7 +238,7 @@ def get_command(typer_instance: Typer) -> click.Command: # Create a single Command single_command = typer_instance.registered_commands[0] - if not single_command.context_settings and typer_instance.info.context_settings: + if not single_command.context_settings: single_command.context_settings = typer_instance.info.context_settings click_command = get_command_from_info(single_command) From 6f0756a40938dfdd71fd54fe27102574bacf4f1f Mon Sep 17 00:00:00 2001 From: Leron Gray Date: Thu, 17 Dec 2020 18:27:32 -0800 Subject: [PATCH 4/6] readd condition after revert --- typer/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typer/main.py b/typer/main.py index 87a56be0b..464096857 100644 --- a/typer/main.py +++ b/typer/main.py @@ -238,7 +238,7 @@ def get_command(typer_instance: Typer) -> click.Command: # Create a single Command single_command = typer_instance.registered_commands[0] - if not single_command.context_settings: + if not single_command.context_settings and typer_instance.info.context_settings: single_command.context_settings = typer_instance.info.context_settings click_command = get_command_from_info(single_command) From 2e79e27c522b0ead24a06dc04532ba73cbe0c2b7 Mon Sep 17 00:00:00 2001 From: Leron Gray Date: Thu, 17 Dec 2020 18:39:01 -0800 Subject: [PATCH 5/6] add nocover for context_settings test --- tests/test_others.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_others.py b/tests/test_others.py index 8e56d32f4..44fa38137 100644 --- a/tests/test_others.py +++ b/tests/test_others.py @@ -228,8 +228,8 @@ def test_context_settings_inheritance_single_command(): app = typer.Typer(context_settings=dict(help_option_names=["-h", "--help"])) @app.command() - def main(name: str = typer.Argument("Camila")): - typer.echo(f"{name} wrote this test.") + def main(name: str): + pass # pragma: nocover result = runner.invoke(app, ["main", "-h"]) assert "Show this message and exit." in result.stdout From a7b8cc27a16612dc1a3dcefd865d6a456130396d Mon Sep 17 00:00:00 2001 From: Leron Gray Date: Fri, 18 Dec 2020 12:56:24 -0800 Subject: [PATCH 6/6] use DefaultPlaceholder for typer condition --- typer/main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/typer/main.py b/typer/main.py index 464096857..3c5b51415 100644 --- a/typer/main.py +++ b/typer/main.py @@ -238,7 +238,9 @@ def get_command(typer_instance: Typer) -> click.Command: # Create a single Command single_command = typer_instance.registered_commands[0] - if not single_command.context_settings and typer_instance.info.context_settings: + if not single_command.context_settings and not isinstance( + typer_instance.info.context_settings, DefaultPlaceholder + ): single_command.context_settings = typer_instance.info.context_settings click_command = get_command_from_info(single_command)