Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📝 Fix outdated optional *CLI argument* section in tutorial #983

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/tutorial/arguments/optional.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `typer.Argument()` and make sure to provide a "default" value, for example `None`:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've now rewritten it so that it covers both Annotated and non-Annotated code snippets:

name: Optional[str] = typer.Argument(default=None)

&

name: Annotated[Optional[str], typer.Argument()] = None

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the point of someone, who were reading the tutorial for the first time (me), that's still confusing if you go from the previous examples:

name: Annotated[str, typer.Argument()]

...that works exactly the same as

name: str

where you only see typer.Argument() used inside the type definition. And peeking in the next example from the phrase in subject, you see only:

Now we have:

name: Annotated[Optional[str], typer.Argument()] = None

So I'd suggest adding both variants you mentioned in tutorial, like:

name: Annotated[Optional[str], typer.Argument()] = None

...that works exactly the same as

name: Optional[str] = typer.Argument(default=None)

And placing the Annotated version first as it is recommended in the rest of the tutorial.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I see.

I wonder whether it would be less confusing if the first example of an optional argument doesn't actually use None as default, but some other random value. I think that would help enforce the idea that the default is all that matters, and that Optional is only used to support None as a potential value. What do you think?


```Python hl_lines="7"
{!../docs_src/arguments/optional/tutorial002_an.py!}
Expand Down
Loading