From 84d2fe476402a3763024f0d1857b9ca643019c0f Mon Sep 17 00:00:00 2001 From: theomartinez Date: Wed, 13 Sep 2023 18:57:21 +0200 Subject: [PATCH 1/4] List type options return None if default_value is None and no option given --- typer/main.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/typer/main.py b/typer/main.py index 9de5f5960d..271500b5e9 100644 --- a/typer/main.py +++ b/typer/main.py @@ -631,9 +631,11 @@ def convertor(value: Any) -> Any: def generate_list_convertor( - convertor: Optional[Callable[[Any], Any]] -) -> Callable[[Sequence[Any]], List[Any]]: - def internal_convertor(value: Sequence[Any]) -> List[Any]: + convertor: Optional[Callable[[Any], Any]], default_value: Optional[Any] +) -> Callable[[Sequence[Any]], Optional[List[Any]]]: + def internal_convertor(value: Sequence[Any]) -> Optional[List[Any]]: + if default_value is None and len(value) == 0: + return None return [convertor(v) if convertor else v for v in value] return internal_convertor @@ -852,7 +854,9 @@ def get_click_param( ) convertor = determine_type_convertor(main_type) if is_list: - convertor = generate_list_convertor(convertor) + convertor = generate_list_convertor( + convertor=convertor, default_value=default_value + ) if is_tuple: convertor = generate_tuple_convertor(main_type.__args__) if isinstance(parameter_info, OptionInfo): From df1096f0cc961a63aa35017f639665acb4e469d9 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Fri, 22 Mar 2024 16:28:51 +0100 Subject: [PATCH 2/4] show in the docs that the value is None and add a unit test --- docs/tutorial/multiple-values/multiple-options.md | 1 + docs_src/multiple_values/multiple_options/tutorial001.py | 2 +- .../test_multiple_options/test_tutorial001.py | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/tutorial/multiple-values/multiple-options.md b/docs/tutorial/multiple-values/multiple-options.md index 00754e93b4..7ff9bcdcb6 100644 --- a/docs/tutorial/multiple-values/multiple-options.md +++ b/docs/tutorial/multiple-values/multiple-options.md @@ -26,6 +26,7 @@ Check it:
```console +// The default value is 'None' $ python main.py No provided users diff --git a/docs_src/multiple_values/multiple_options/tutorial001.py b/docs_src/multiple_values/multiple_options/tutorial001.py index 141e9aa1ce..bce2322915 100644 --- a/docs_src/multiple_values/multiple_options/tutorial001.py +++ b/docs_src/multiple_values/multiple_options/tutorial001.py @@ -5,7 +5,7 @@ def main(user: Optional[List[str]] = typer.Option(None)): if not user: - print("No provided users") + print(f"No provided users (raw input = {user})") raise typer.Abort() for u in user: print(f"Processing user: {u}") diff --git a/tests/test_tutorial/test_multiple_values/test_multiple_options/test_tutorial001.py b/tests/test_tutorial/test_multiple_values/test_multiple_options/test_tutorial001.py index 3c57bfd33e..4293ed8d3d 100644 --- a/tests/test_tutorial/test_multiple_values/test_multiple_options/test_tutorial001.py +++ b/tests/test_tutorial/test_multiple_values/test_multiple_options/test_tutorial001.py @@ -15,6 +15,7 @@ def test_main(): result = runner.invoke(app) assert result.exit_code != 0 assert "No provided users" in result.output + assert "raw input = None" in result.output assert "Aborted" in result.output From 8d6aaf0cf31b2b8d73fd87539211847dd7a6c079 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Fri, 22 Mar 2024 16:29:49 +0100 Subject: [PATCH 3/4] update output in example --- docs/tutorial/multiple-values/multiple-options.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tutorial/multiple-values/multiple-options.md b/docs/tutorial/multiple-values/multiple-options.md index 7ff9bcdcb6..d61bee1588 100644 --- a/docs/tutorial/multiple-values/multiple-options.md +++ b/docs/tutorial/multiple-values/multiple-options.md @@ -29,7 +29,7 @@ Check it: // The default value is 'None' $ python main.py -No provided users +No provided users (raw input = None) Aborted! // Now pass a user From beaf3f19febd087ffb207e58021d33b57cbc3b89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Sat, 23 Mar 2024 12:12:43 -0500 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=93=9D=20Update=20source=20example=20?= =?UTF-8?q?for=20annotated=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs_src/multiple_values/multiple_options/tutorial001_an.py | 2 +- .../test_multiple_options/test_tutorial001_an.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/docs_src/multiple_values/multiple_options/tutorial001_an.py b/docs_src/multiple_values/multiple_options/tutorial001_an.py index 68ad2519ea..3dd19d8f59 100644 --- a/docs_src/multiple_values/multiple_options/tutorial001_an.py +++ b/docs_src/multiple_values/multiple_options/tutorial001_an.py @@ -6,7 +6,7 @@ def main(user: Annotated[Optional[List[str]], typer.Option()] = None): if not user: - print("No provided users") + print(f"No provided users (raw input = {user})") raise typer.Abort() for u in user: print(f"Processing user: {u}") diff --git a/tests/test_tutorial/test_multiple_values/test_multiple_options/test_tutorial001_an.py b/tests/test_tutorial/test_multiple_values/test_multiple_options/test_tutorial001_an.py index 0009fd2f05..20ea235307 100644 --- a/tests/test_tutorial/test_multiple_values/test_multiple_options/test_tutorial001_an.py +++ b/tests/test_tutorial/test_multiple_values/test_multiple_options/test_tutorial001_an.py @@ -15,6 +15,7 @@ def test_main(): result = runner.invoke(app) assert result.exit_code != 0 assert "No provided users" in result.output + assert "raw input = None" in result.output assert "Aborted" in result.output