Repository navigation
Best practices for managing Typer objects and callers at scale? #1152
Replies: 1 comment
|
You can reuse from typing import Annotated
import typer
app = typer.Typer()
def show_version(value: bool):
if value:
typer.echo("1.0")
raise typer.Exit()
VersionOption = Annotated[
bool,
typer.Option("--version", callback=show_version, is_eager=True),
]
@app.command()
def run(version: VersionOption = False):
typer.echo("running")
if __name__ == "__main__":
app()Put genuinely shared aliases/callbacks in an Keep command functions as adapters: accept CLI values, construct a configuration object if useful, then call ordinary service functions. Those services should not depend on Typer's Some explicit signature repetition is useful because Typer inspects that signature to build help and parse inputs. I would avoid hiding all options in an arbitrary wrapper or replacing the command signature with |
Uh oh!
There was an error while loading. Please reload this page.
First Check
Commit to Help
Example Code
Description
Hi guys, I have been tinkering with Typer more recently and I would like to take the cleaniness that the library brings to the table to the next level.
Using type annotations is one of the key strengths of Typer, and like the example, which implements a main caller and an option to display the version, it's simple, doesn't need boilerplate and gets the job done.
And while the documentation provides a great suggestion to better organize complex programs, I've been wondering what are some other best practices when scaling the usage of Typer? 🤔
More specifically, declaring the
typer.Optionwithin theAnnotatedobject within themainfunction seems to be just one example of many in which function calls can become giant and verbose. Are there any suggestions on how to tackle simplifying and perhaps creating these objects elsewhere to later import and use in these definitions?This is more of a discussion about better usage of Typer and proper project structure to ensure things don't get out of control. I personally tend to think of storing the help messages for options in a single place should they need more text do be properly explained, but is this really the best way to do it? Shouldn't one store the actual whole
typer.Optionobject instead? Where? Or perhaps there is a completely different way to approach this, so many possibilities!I'd like your thoughts on this, especially if you have dealt with Typer on a larger scale. Knowing the better ways to apply the tool in both small and large scales would be great for my future projects!
Operating System
Linux
Operating System Details
No response
Typer Version
0.15.1
Python Version
3.13.1
Additional Context
No response
All reactions