You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I used the GitHub search to find a similar question and didn't find it.
I searched the Typer documentation, with the integrated search.
I already searched in Google "How to X in Typer" and didn't find any information.
I already read and followed all the tutorials in the docs and didn't find an answer.
I already checked if it is not related to Typer but to Click.
Commit to Help
I commit to help with one of those options 👆
Example Code
importTyperapp=typer.Typer()
@dataclassclassMyObj:
prop: int# Currently@app.command()defmy_command(ctx: typer.Context):
obj: MyObj=ctx.obj# I have to specify type of object hereprint(obj.prop)
# If `typer.Context` is generic@app.command()defmy_command(ctx: typer.Context[MyObj]):
obj=ctx.obj# type is automatically inferredprint(obj.prop)
# Possible solution. Not tested yet.# Modify `typer.Context` asclassContext[T](click.Context):
obj: T
Description
I used to use click and frequently use click.pass_obj since I do not use other features in Context. I usually type my object in the command definition like this:
classMyObj:
...
@click.command()@click.pass_objdefmy_command(obj: MyObj, ...):
... # My type checker is aware that `obj` is of type `MyObj`
In Typer, I would have to do this:
@app.command()defmy_command(ctx: typer.Context, ...):
obj: MyObj=ctx.obj# I have to specify type of object hereprint(obj.prop)
I believe it would be better if I can still specify object type in type annotation. Making typing.Conext generic might be a solution.
@app.command()defmy_command(ctx: typer.Context[MyObj], ...):
obj=ctx.obj# type is automatically inferredprint(obj.prop)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
First Check
Commit to Help
Example Code
Description
I used to use click and frequently use
click.pass_obj
since I do not use other features inContext
. I usually type my object in the command definition like this:In Typer, I would have to do this:
I believe it would be better if I can still specify object type in type annotation. Making
typing.Conext
generic might be a solution.Operating System
Windows
Operating System Details
No response
Typer Version
0.13.1
Python Version
3.12
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions