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

✨ Add support for PEP-593 Annotated for specifying options and arguments #584

Merged
merged 12 commits into from
May 2, 2023

Conversation

ryangalamb
Copy link
Contributor

@ryangalamb ryangalamb commented Apr 25, 2023

Implements #184

In addition to the tests/implementation, this PR also adds/updates docs_src files for all the tutorial files that can use Annotated.

Each tutorial00N.py file with Annotated is copied to tutorial00N_an.py and an _an.py version of its test is created in tests/test_tutorial. (This is modeled after the convention used in FastAPI.)

Adding the tutorial files results in a pretty massive PR, so let me know if you'd prefer those in a separate PR.

I've tested this on

  • python3.6
  • python3.7
  • python3.10

Please let me know if there's anything you'd like me to change/address 🙏

@github-actions
Copy link

📝 Docs preview for commit 4c8ec06 at: https://64485c5170ff0b41deceb08d--typertiangolo.netlify.app

@github-actions
Copy link

📝 Docs preview for commit af327f1 at: https://64485e9ce8a02045f093fbe4--typertiangolo.netlify.app

@github-actions
Copy link

📝 Docs preview for commit e4b0857 at: https://64486108fbfcff49634a32af--typertiangolo.netlify.app

A lot of the dev dependency packages weren't installable on 3.6, so I
had to remove them from the pyproject.toml.

This commit adds them back
@github-actions
Copy link

📝 Docs preview for commit 452fc6f at: https://644863863678c34b7b416e31--typertiangolo.netlify.app

@github-actions
Copy link

📝 Docs preview for commit 4c0d058 at: https://644867cee1091d4cc2a37c70--typertiangolo.netlify.app

@github-actions
Copy link

github-actions bot commented May 2, 2023

📝 Docs preview for commit 83cce68 at: https://64508bba537334776a49a9a1--typertiangolo.netlify.app

@github-actions
Copy link

github-actions bot commented May 2, 2023

📝 Docs preview for commit ba312c5 at: https://64508dfd6a3f5b73af9d5eec--typertiangolo.netlify.app

Comment on lines +134 to +150
# When used as a default, `Option` takes a default value and option names
# as positional arguments:
# `Option(some_value, "--some-argument", "-s")`
# When used in `Annotated` (ie, what this is handling), `Option` just takes
# option names as positional arguments:
# `Option("--some-argument", "-s")`
# In this case, the `default` attribute of `parameter_info` is actually
# meant to be the first item of `param_decls`.
if (
isinstance(parameter_info, OptionInfo)
and parameter_info.default is not ...
):
parameter_info.param_decls = (
cast(str, parameter_info.default),
*(parameter_info.param_decls or ()),
)
parameter_info.default = ...
Copy link
Member

Choose a reason for hiding this comment

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

This is very clever! 😎 🚀

@tiangolo
Copy link
Member

tiangolo commented May 2, 2023

Great job @ryangalamb! 👏 🍰

I tweaked a couple of things, tests, and examples. And I added all the new annotated examples to the docs. I also updated the docs to introduce the new Annotated.

This will be available in Typer 0.9.0 released in the next hours. 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants