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

Conversation

gkeuccsr
Copy link

@gkeuccsr gkeuccsr commented Sep 9, 2024

To match the example, description is updated

To match the example, description is updated
@github-actions github-actions bot added the docs Improvements or additions to documentation label Sep 9, 2024
Copy link

github-actions bot commented Sep 9, 2024

@svlandeg svlandeg self-assigned this Sep 10, 2024
Copy link
Member

@svlandeg svlandeg left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! The text has indeed gotten a bit out of date as the example code now uses Annotated.

I've rewritten the text again, because we shouldn't focus on the Optional part of the type annotation, as that is not what is making the CLI argument optional 🙃 We need to focus the reader on the fact that there is a default value; whether that is None or not and whether or not the type is Optional[X] is irrelevant. We could also have had

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

for example. This is sufficiently explained in various parts of the documentation so doesn't need repeating here, but we just need to make sure to avoid confusion with the rephrasing.

Anyway - thanks again for spotting this!

docs/tutorial/arguments/optional.md Outdated Show resolved Hide resolved
@@ -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?

Copy link

@svlandeg svlandeg removed their assignment Sep 10, 2024
@svlandeg svlandeg changed the title Fix outdated optional *CLI argument* section in tutorial 📝 Fix outdated optional *CLI argument* section in tutorial Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants