-
-
Notifications
You must be signed in to change notification settings - Fork 684
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
base: master
Are you sure you want to change the base?
Conversation
To match the example, description is updated
📝 Docs preview for commit 729182b at: https://1d0969a6.typertiangolo.pages.dev Modified Pages |
There was a problem hiding this 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 preview for commit 014882c at: https://a3d15f63.typertiangolo.pages.dev Modified Pages |
📝 Docs preview for commit 26c7b41 at: https://e150a561.typertiangolo.pages.dev Modified Pages |
📝 Docs preview for commit 1e96b74 at: https://349df2d6.typertiangolo.pages.dev Modified Pages |
📝 Docs preview for commit 844c01a at: https://483c8040.typertiangolo.pages.dev Modified Pages |
📝 Docs preview for commit aea1d6d at: https://8b344b2d.typertiangolo.pages.dev Modified Pages |
There was a problem hiding this 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.
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`. |
There was a problem hiding this comment.
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.
To match the example, description is updated