From 97e1dfea3fe7dd4732daaf00011d35e2632e1ee7 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Fri, 13 Sep 2024 11:27:16 +0200 Subject: [PATCH 1/2] Deprecate support for (non-functional) is_flag and flag_value parameters --- typer/completion.py | 2 -- typer/core.py | 2 -- typer/main.py | 3 +-- typer/models.py | 12 ++++++++++-- typer/params.py | 3 +++ 5 files changed, 14 insertions(+), 8 deletions(-) diff --git a/typer/completion.py b/typer/completion.py index 1220a1b545..5b930bc20c 100644 --- a/typer/completion.py +++ b/typer/completion.py @@ -68,7 +68,6 @@ def _install_completion_placeholder_function( install_completion: bool = Option( None, "--install-completion", - is_flag=True, callback=install_callback, expose_value=False, help="Install completion for the current shell.", @@ -76,7 +75,6 @@ def _install_completion_placeholder_function( show_completion: bool = Option( None, "--show-completion", - is_flag=True, callback=show_callback, expose_value=False, help="Show completion for the current shell, to copy it or customize the installation.", diff --git a/typer/core.py b/typer/core.py index 00e21da869..84a151a0f1 100644 --- a/typer/core.py +++ b/typer/core.py @@ -413,7 +413,6 @@ def __init__( prompt_required: bool = True, hide_input: bool = False, is_flag: Optional[bool] = None, - flag_value: Optional[Any] = None, multiple: bool = False, count: bool = False, allow_from_autoenv: bool = True, @@ -440,7 +439,6 @@ def __init__( confirmation_prompt=confirmation_prompt, hide_input=hide_input, is_flag=is_flag, - flag_value=flag_value, multiple=multiple, count=count, allow_from_autoenv=allow_from_autoenv, diff --git a/typer/main.py b/typer/main.py index a621bda6ad..0b1837389f 100644 --- a/typer/main.py +++ b/typer/main.py @@ -880,7 +880,7 @@ def get_click_param( if is_tuple: convertor = generate_tuple_convertor(get_args(main_type)) if isinstance(parameter_info, OptionInfo): - if main_type is bool and parameter_info.is_flag is not False: + if main_type is bool: is_flag = True # Click doesn't accept a flag of type bool, only None, and then it sets it # to bool internally @@ -907,7 +907,6 @@ def get_click_param( prompt_required=parameter_info.prompt_required, hide_input=parameter_info.hide_input, is_flag=is_flag, - flag_value=parameter_info.flag_value, multiple=is_list, count=parameter_info.count, allow_from_autoenv=parameter_info.allow_from_autoenv, diff --git a/typer/models.py b/typer/models.py index 9bbe2a36d2..b2b534a8c5 100644 --- a/typer/models.py +++ b/typer/models.py @@ -298,6 +298,7 @@ def __init__( confirmation_prompt: bool = False, prompt_required: bool = True, hide_input: bool = False, + # TODO: remove is_flag and flag_value in a future release is_flag: Optional[bool] = None, flag_value: Optional[Any] = None, count: bool = False, @@ -378,12 +379,19 @@ def __init__( # Rich settings rich_help_panel=rich_help_panel, ) + if is_flag is not None or flag_value is not None: + import warnings + + warnings.warn( + "The 'is_flag' and 'flag_value' parameters are not supported by Typer " + " and will be removed entirely in a future release.", + DeprecationWarning, + stacklevel=2, + ) self.prompt = prompt self.confirmation_prompt = confirmation_prompt self.prompt_required = prompt_required self.hide_input = hide_input - self.is_flag = is_flag - self.flag_value = flag_value self.count = count self.allow_from_autoenv = allow_from_autoenv diff --git a/typer/params.py b/typer/params.py index 2fd025c90d..b446db2ff0 100644 --- a/typer/params.py +++ b/typer/params.py @@ -35,6 +35,7 @@ def Option( confirmation_prompt: bool = False, prompt_required: bool = True, hide_input: bool = False, + # TODO: remove is_flag and flag_value in a future release is_flag: Optional[bool] = None, flag_value: Optional[Any] = None, count: bool = False, @@ -98,6 +99,7 @@ def Option( confirmation_prompt: bool = False, prompt_required: bool = True, hide_input: bool = False, + # TODO: remove is_flag and flag_value in a future release is_flag: Optional[bool] = None, flag_value: Optional[Any] = None, count: bool = False, @@ -160,6 +162,7 @@ def Option( confirmation_prompt: bool = False, prompt_required: bool = True, hide_input: bool = False, + # TODO: remove is_flag and flag_value in a future release is_flag: Optional[bool] = None, flag_value: Optional[Any] = None, count: bool = False, From 0461b415f378cf5b01f13ef84a36ec0dab41ea56 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Fri, 13 Sep 2024 15:53:29 +0200 Subject: [PATCH 2/2] Add test for deprecation warning --- tests/test_deprecation.py | 27 +++++++++++++++++++++++++++ typer/models.py | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/test_deprecation.py diff --git a/tests/test_deprecation.py b/tests/test_deprecation.py new file mode 100644 index 0000000000..bcb6fb29e2 --- /dev/null +++ b/tests/test_deprecation.py @@ -0,0 +1,27 @@ +from typing import Optional + +import pytest +import typer +from typer.testing import CliRunner + +runner = CliRunner() + + +def test_deprecation(): + app = typer.Typer() + + def add_command(): + @app.command() + def cmd( + opt: Optional[float] = typer.Option( + 3.14, + is_flag=True, + flag_value="42", + help="Some wonderful number", + ), + ): ... # pragma: no cover + + with pytest.warns( + match="The 'is_flag' and 'flag_value' parameters are not supported by Typer" + ): + add_command() diff --git a/typer/models.py b/typer/models.py index b2b534a8c5..44daaebab3 100644 --- a/typer/models.py +++ b/typer/models.py @@ -384,7 +384,7 @@ def __init__( warnings.warn( "The 'is_flag' and 'flag_value' parameters are not supported by Typer " - " and will be removed entirely in a future release.", + "and will be removed entirely in a future release.", DeprecationWarning, stacklevel=2, )