typer icon indicating copy to clipboard operation
typer copied to clipboard

✨ Add support for `typing.TypeAliasType` as valid parameter type.

Open zarch opened this issue 1 year ago • 4 comments

See discussion for further details and sample code.

zarch avatar Sep 03 '24 00:09 zarch

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

github-actions[bot] avatar Sep 03 '24 00:09 github-actions[bot]

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

github-actions[bot] avatar Sep 03 '24 08:09 github-actions[bot]

@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 avatar Sep 03 '24 09:09 zarch

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

lachaib avatar May 05 '25 08:05 lachaib

Hi @lachaib, Sorry for the delayed response. To help move this forward, could you please:

Share the branch/commit - Do you have a specific branch or commit with these changes that can be pushed to this branch? Provide test coverage - Could you include unit tests that cover the additional functionalities you've implemented?

This will help ensure we can properly review and integrate your changes. Thanks!

zarch avatar Jul 31 '25 13:07 zarch

Hi @lachaib, Sorry for the delayed response. To help move this forward, could you please:

Share the branch/commit - Do you have a specific branch or commit with these changes that can be pushed to this branch? Provide test coverage - Could you include unit tests that cover the additional functionalities you've implemented?

This will help ensure we can properly review and integrate your changes. Thanks!

Hello @zarch ,

This is not a ready-to-merge contribution but I pushed my state so that you can get inspiration and consolidate this existing pull request.

https://github.com/lachaib/typer/tree/pep695

Thanks a lot

lachaib avatar Aug 01 '25 14:08 lachaib

This pull request has a merge conflict that needs to be resolved.

github-actions[bot] avatar Sep 01 '25 14:09 github-actions[bot]

📝 Docs preview

Last commit 110972ac54d65d46dc39781096c7dd0f1d2db885 at: https://3a314827.typertiangolo.pages.dev

github-actions[bot] avatar Oct 31 '25 14:10 github-actions[bot]