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

[FEATURE] Use Python 3.9's Annotated[T, *metadata] (PEP 593) to describe parameters #184

Closed
ewen-lbh opened this issue Nov 1, 2020 · 5 comments
Labels
answered feature New feature, enhancement or request investigate

Comments

@ewen-lbh
Copy link

ewen-lbh commented Nov 1, 2020

Is your feature request related to a problem

Not really, the current solution is already great.

The solution you would like

Add an alternative syntax to describe optional parameters, PEP 593's Annotated[T, *metadata]. Now, strictly speaking of syntax, it's not much better, but it's built into python, and thus might help integration with IDEs in the future.

Help for optional parameters could be provided as the second argument of Annotated:

def my_action(a: str, b: Annotated[bool, "description"], c: Annotated[str, "output"] = "~/.config/"):

is equivalent to

def my_action(a: str, b: bool = Argument(..., help="description"), c: str = Option("~/.config/", help="output")):

As a bonus, there's no need to do a weird thing: type = Argument(..., help="description") when you want to specify help without giving a default value, just do thing: Annotated[type, "description"]

Also, it might be a little easier to implement since there's no messing with default values.

An Annotated parameter to the function will resolve to:

  • Argument if it has no default value
  • Option else

Describe alternatives you've considered

Making it always resolve to Argument or always to Option, or use something like Annotated[type, "description", form=Argument for arguments and Annotated[type, "description", form=Option] for options. I'm not sure about this.

tbh this is debatable, but I figured I'd throw the idea out there, since this shiny new python 3.9 feature could be useful to Typer, as it seems that the use case described in the PEP is fitting:

This PEP adds an Annotated type to the typing module to decorate existing types with context-specific metadata. Specifically, a type T can be annotated with metadata x via the typehint Annotated[T, x]. This metadata can be used for either static analysis or at runtime. If a library (or tool) encounters a typehint Annotated[T, x] and has no special logic for metadata x, it should ignore it and simply treat the type as T.

PEP 593, python.org

@ewen-lbh ewen-lbh added the feature New feature, enhancement or request label Nov 1, 2020
@ewen-lbh ewen-lbh changed the title [FEATURE] Using Python 3.9's Annotated[T, m...] (PEP 593) to describe parameters [FEATURE] Using Python 3.9's Annotated[T, *metadata] (PEP 593) to describe parameters Nov 1, 2020
@ewen-lbh ewen-lbh changed the title [FEATURE] Using Python 3.9's Annotated[T, *metadata] (PEP 593) to describe parameters [FEATURE] Use Python 3.9's Annotated[T, *metadata] (PEP 593) to describe parameters Nov 14, 2020
@baterflyrity
Copy link

It would be very useful. Cant wait implementation.

@mficek
Copy link

mficek commented Mar 16, 2022

+1 for implementation. Can I help in any way? Is there any option to plug-in typer with this functionality?

@NowanIlfideme
Copy link

Similar PR in FastAPI repo hasn't been merged yet, it's possible this is waiting for Pydantic-Core to be released more broadly (I know Sebastian and Samuel Colvin talk 😋) to fix this at an even more fundamental level.

ryangalamb added a commit to ryangalamb/typer that referenced this issue Apr 25, 2023
ryangalamb added a commit to ryangalamb/typer that referenced this issue Apr 25, 2023
ryangalamb added a commit to ryangalamb/typer that referenced this issue Apr 25, 2023
tiangolo added a commit that referenced this issue May 2, 2023
…ments (#584)

* ✨ Add support for PEP-593 `Annotated` for specifying options and arguments

Implements #184

* Revert things I did to get a 3.6 virtualenv 😅

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

* Fix mypy/lint errors

* Skip coverage on test lines that shouldn't execute

* Missed a spot

* ♻️ Tweak examples and tests with Annotated, add extra examples and tests

* 🔥 Remove Pydantic-specific logic from _typing.py

* 📝 Update docs to use new Annotated examples

* 📝 Add docs introducing Annotated and previous versions

* 🔧 Add commented out MkDocs config for highlighting docs examples

* ✅ Fix tests for Click 7

---------

Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
@tiangolo
Copy link
Member

tiangolo commented May 2, 2023

Hello there! This was implemented in #584, here are the new docs: https://typer.tiangolo.com/tutorial/arguments/optional/#an-alternative-cli-argument-declaration, it's available in Typer 0.9.0, just released 🚀

@github-actions
Copy link

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered feature New feature, enhancement or request investigate
Projects
None yet
Development

No branches or pull requests

5 participants