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 6 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
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
@svlandeg svlandeg self-assigned this Oct 25, 2024
Copy link

github-actions bot commented Jan 8, 2025

Copy link

github-actions bot commented Jan 8, 2025

@svlandeg svlandeg marked this pull request as draft January 8, 2025 13:38
Copy link

github-actions bot commented Jan 8, 2025

Copy link

github-actions bot commented Jan 8, 2025

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.

I've gone ahead and edited this PR further, and I chose to use a default argument "World" in this part of the docs instead of None, to more clearly separate the meaning of "an optional argument" from the type Optional. This meant changing tutorial002 and simplifying it - I think this should make the section more clear.

I'll leave the final review with Tiangolo.

@svlandeg svlandeg marked this pull request as ready for review January 8, 2025 14:57
@svlandeg svlandeg removed their assignment Jan 8, 2025
Comment on lines -126 to +132
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`.
Copy link
Member

@svlandeg svlandeg Jan 8, 2025

Choose a reason for hiding this comment

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

I'm not even sure we need all this additional explanation about None being a potential default - it may still cause some confusion and we're probably already explaining Optional in various other places.

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