From 729182b33170e6c3701524ac718ec2805e0e2d9d Mon Sep 17 00:00:00 2001 From: gkeuccsr <166070166+gkeuccsr@users.noreply.github.com> Date: Mon, 9 Sep 2024 22:24:25 +0200 Subject: [PATCH 1/3] Fix outdated optional *CLI argument* section in tutorial To match the example, description is updated --- docs/tutorial/arguments/optional.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tutorial/arguments/optional.md b/docs/tutorial/arguments/optional.md index e33b9cdc46..a0690d0cf2 100644 --- a/docs/tutorial/arguments/optional.md +++ b/docs/tutorial/arguments/optional.md @@ -113,7 +113,7 @@ name: str Now, finally what we came for, an optional *CLI argument*. -To make a *CLI argument* optional, use `typer.Argument()` and pass a different "default" as the first parameter to `typer.Argument()`, for example `None`: +To make a *CLI argument* optional, use `Optional[X]` type for `Annotated` and provide a "default" value for function parameter, for example `None`: ```Python hl_lines="7" {!../docs_src/arguments/optional/tutorial002_an.py!} From 014882c5f80994917769d6cb7d33bea4f69c2f3f Mon Sep 17 00:00:00 2001 From: Sofie Van Landeghem Date: Tue, 10 Sep 2024 11:03:28 +0200 Subject: [PATCH 2/3] Rephrase --- docs/tutorial/arguments/optional.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tutorial/arguments/optional.md b/docs/tutorial/arguments/optional.md index a0690d0cf2..728b1e6460 100644 --- a/docs/tutorial/arguments/optional.md +++ b/docs/tutorial/arguments/optional.md @@ -113,7 +113,7 @@ name: str Now, finally what we came for, an optional *CLI argument*. -To make a *CLI argument* optional, use `Optional[X]` type for `Annotated` and provide a "default" value for function parameter, for example `None`: +To make a *CLI argument* optional, use `typer.Argument()` and make sure to provide a "default" value, for example `None`: ```Python hl_lines="7" {!../docs_src/arguments/optional/tutorial002_an.py!} From 26c7b41060fc13be10f3127bca33df840671e5e2 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Wed, 8 Jan 2025 14:23:59 +0100 Subject: [PATCH 3/3] clarify with non-Optional typing --- docs/tutorial/arguments/optional.md | 12 +++++++++--- docs_src/arguments/optional/tutorial002.py | 9 ++------- docs_src/arguments/optional/tutorial002_an.py | 9 ++------- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/docs/tutorial/arguments/optional.md b/docs/tutorial/arguments/optional.md index 728b1e6460..441061a28d 100644 --- a/docs/tutorial/arguments/optional.md +++ b/docs/tutorial/arguments/optional.md @@ -113,7 +113,7 @@ name: str Now, finally what we came for, an optional *CLI argument*. -To make a *CLI argument* optional, use `typer.Argument()` and make sure to provide a "default" value, for example `None`: +To make a *CLI argument* optional, use `typer.Argument()` and make sure to provide a "default" value, for example `"World"`: ```Python hl_lines="7" {!../docs_src/arguments/optional/tutorial002_an.py!} @@ -122,14 +122,20 @@ To make a *CLI argument* optional, use `typer.Argument()` and make sure to provi Now we have: ```Python -name: Annotated[Optional[str], typer.Argument()] = None +name: Annotated[str, typer.Argument()] = "World" ``` Because we are using `typer.Argument()` **Typer** will know that this is a *CLI argument* (no matter if *required* or *optional*). /// tip -By using `Optional` your editor will be able to know that the value *could* be `None`, and will be able to warn you if you do something assuming it is a `str` that would break if it was `None`. +If you want the default value to be `None`, you have to additionally declare the parameter to be of type `Optional`, which will tell your editor that the value of this parameter can be `None`: + +```Python +name: Annotated[Optional[str], typer.Argument()] = None +``` + +Your editor can then warn you if you do something assuming it is a `str` that would break if it was `None`. /// diff --git a/docs_src/arguments/optional/tutorial002.py b/docs_src/arguments/optional/tutorial002.py index 2445b34016..f81b1a4543 100644 --- a/docs_src/arguments/optional/tutorial002.py +++ b/docs_src/arguments/optional/tutorial002.py @@ -1,13 +1,8 @@ -from typing import Optional - import typer -def main(name: Optional[str] = typer.Argument(default=None)): - if name is None: - print("Hello World!") - else: - print(f"Hello {name}") +def main(name: str = typer.Argument(default="World")): + print(f"Hello {name}!") if __name__ == "__main__": diff --git a/docs_src/arguments/optional/tutorial002_an.py b/docs_src/arguments/optional/tutorial002_an.py index 299a0bb6ea..71c272a671 100644 --- a/docs_src/arguments/optional/tutorial002_an.py +++ b/docs_src/arguments/optional/tutorial002_an.py @@ -1,14 +1,9 @@ -from typing import Optional - import typer from typing_extensions import Annotated -def main(name: Annotated[Optional[str], typer.Argument()] = None): - if name is None: - print("Hello World!") - else: - print(f"Hello {name}") +def main(name: Annotated[str, typer.Argument()] = "World"): + print(f"Hello {name}!") if __name__ == "__main__":