Skip to content

✨ Add support for typing.TypeAliasType as valid parameter type. #970

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

zarch
Copy link

@zarch zarch commented Sep 3, 2024

See discussion for further details and sample code.

Copy link

github-actions bot commented Sep 3, 2024

📝 Docs preview for commit e13d1d8 at: https://45f079b8.typertiangolo.pages.dev

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.

Hi @zarch, thanks for the contribution, we appreciate it!

The CI is currently failing - could you look into that? I'll put the PR in draft in the meantime. The review process will go much smoother once the CI is green 😉

@svlandeg svlandeg changed the title Add support for typing.TypeAliasType as valid parameter type. ✨ Add support for typing.TypeAliasType as valid parameter type. Sep 3, 2024
@svlandeg svlandeg added the feature New feature, enhancement or request label Sep 3, 2024
@svlandeg svlandeg marked this pull request as draft September 3, 2024 08:39
Copy link

github-actions bot commented Sep 3, 2024

📝 Docs preview for commit c97aff6 at: https://8682c9ff.typertiangolo.pages.dev

@zarch
Copy link
Author

zarch commented Sep 3, 2024

@svlandeg I've fixed all the CI. To properly handle the TypeAliasType I also have to update mypy version to v1.10, I don't know if this might be an issue.

The only failing check is the one using Python 3.7 (not supported), se my other PR to bump the minimum requirement to Python 3.8.
Therefore, these change should be put on queue with: 🔥 Drop support for Python 3.7

@zarch zarch marked this pull request as ready for review September 4, 2024 08:20
@lachaib
Copy link

lachaib commented May 5, 2025

Hello,

I was digging on the topic on that matter and I think this PR is incomplete:

  • one of the things I'm trying to achieve is to share parameter definition across sub-commands, e.g.
type Name = Annotated[str, Argument(help="The human in front of the screen")]

@app.command()
def greet(name: Name):
      echo(f"Hello {name}")
      
@app.command()
def farewell(name: Name):
      echo(f"Good bye, {name}")

To get it working, I had to modify utils._split_annotation_from_typer_annotations to enable extraction of ParameterInfo from the type alias
(basically added a recursive call to same method with base_annotation.__value__)

  • also, because it can be useful to support nested annotations under the TypeAliasType, I had to unwrap a bit further down the types before reaching get_click_type
    e.g.
from typing import Doc
from pydantic import PositiveInt, validate_call

type Name = Annotated[str, Doc("An human's full name")]
type Age = Annotated[PositiveInt, Doc("An human's age")]

type Identity = tuple[Name, Age]

@app.command()
@validate_call
def register_to_vote(user_identity: Identity):
      ...

Happy to discuss/help on this topic

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

Successfully merging this pull request may close these issues.

3 participants