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
In Lucretiel/autocommand#38, we've learned that autocommand is essentially abandoned, with workflow-breaking bugs unable to be fixed, and the maintainer is unwilling to hand off the projects, so the options are to fork the project or identify an alternative.
I've been pleased with typer in the few places I've tried to use it.
I'd like to explore replacing the use of autocommand with typer. If that goes well, that'll be the way to go. Otherwise, we can fork autocommand as coherent-oss/autocommand or maybe do a rewrite of autocommand.
I'd like to use jaraco.develop as the proving ground, as it has a lot of autocommand usage.
Oh, shoot. Reading through the intro docs, it looks like typer doesn't offer one of the basic ergonomic features of autocommand, the ability to decorate a function and have it be the command. It supports invoking a function as a command, but it still requires the __name__ == '__main__' boilerplate.
looks like typer doesn't offer one of the basic ergonomic features of autocommand, the ability to decorate a function and have it be the command
Typer docs focus on running example main functions through typer.run, but there is a possibility of registering custom commands with typer.Typer.command() as a decorator, as in our cli. If that's not it, what do you mean by "the ability to decorate a function and have it be the command" specifically?
there is a possibility of registering custom commands with typer.Typer.command() as a decorator,
Right, but that creates a subcommand. I'd like to decorate and be the command. There's no way to decorate main without it becoming {script} main, as far as I can tell.
There's no way to decorate main without it becoming {script} main, as far as I can tell.
I think you are looking for a simple Typer.command() or Typer.callback(invoke_without_command=True). Yet, FWIW, it still doesn't behave as the autorun feature you've proposed in fastapi/typer#928.
# foo.pyfromtyperimportTyperapp=Typer()
@app.command()# or @app.callback(invoke_without_command=True)defmain() ->None:
print("called")
__name__=="__main__"andapp()
Using both versions of the above snippet in foo.py, python -m foo --help doesn't list main as a command, and python -m foo prints out called.
jaraco.develop main 🐚 .tox/py/bin/python -m jaraco.develop.add-github-secret
(traceback)
RuntimeError: Type not yet supported: <class 'jaraco.develop.github.Repo'>
I see there is support for custom types, which uses Annotated, so maybe that's better. I just hope I don't have to explicitly create a "parser" for a type that's capable of constructing itself from a string.
Ugh. And it's ugly - if you want to supply a custom parser class, you have to overspecify whether it's an argument or an option (you lose the inference of Argument or Option based on whether a default has been supplied).
Another feature that autocommand has was the ability to automatically create long and short switches. Typer will only infer long names, and if you want to specify the short name, you have to supply the long name as well :(. Reported.
Activity
jaraco commentedon Aug 15, 2024
Oh, shoot. Reading through the intro docs, it looks like typer doesn't offer one of the basic ergonomic features of autocommand, the ability to decorate a function and have it be the command. It supports invoking a function as a command, but it still requires the
__name__ == '__main__'boilerplate.johnslavik commentedon Aug 15, 2024
Typer docs focus on running example
mainfunctions throughtyper.run, but there is a possibility of registering custom commands withtyper.Typer.command()as a decorator, as in our cli. If that's not it, what do you mean by "the ability to decorate a function and have it be the command" specifically?jaraco commentedon Aug 15, 2024
See fastapi/typer#928 where I describe what I'd like to see.
Right, but that creates a subcommand. I'd like to decorate and be the command. There's no way to decorate
mainwithout it becoming{script} main, as far as I can tell.jaraco commentedon Aug 15, 2024
I'm now realizing that I can probably build that wrapper.
And
coherent.maincould do all of the magic (set up the app, infer the globals, conditionally run).johnslavik commentedon Aug 15, 2024
My friend's project https://github.com/nekitdev/entrypoint does exactly this, I think.
And I'm very frustrated that it doesn't just examine
function.__module__--there's never any need to check globals, given that attribute.jaraco commentedon Aug 15, 2024
Nice! But I also want all of the features of typer (function parameter to argument inference, rich help, and completion support).
johnslavik commentedon Aug 15, 2024
I think you are looking for a simple
Typer.command()orTyper.callback(invoke_without_command=True). Yet, FWIW, it still doesn't behave as theautorunfeature you've proposed in fastapi/typer#928.Using both versions of the above snippet in
foo.py,python -m foo --helpdoesn't listmainas a command, andpython -m fooprints outcalled.So, trimming down the boilerplate, you could use:
with
run()creating an app on the fly.But yeah, having the
autorundecorator would be more useful.jaraco commentedon Aug 15, 2024
In jaraco/jaraco.ui@208a1c2, I've added a
mainfunction, and I tried applying it to jaraco.develop.add-github-secret.But when I did, it failed:
Details
It seems that unlike
autocommand,typercan't use arbitrary classes in the annotations to construct an object from the argument.jaraco commentedon Aug 15, 2024
I see there is support for custom types, which uses
Annotated, so maybe that's better. I just hope I don't have to explicitly create a "parser" for a type that's capable of constructing itself from a string.jaraco commentedon Aug 15, 2024
Ugh. And it's ugly - if you want to supply a custom parser class, you have to overspecify whether it's an argument or an option (you lose the inference of Argument or Option based on whether a default has been supplied).
jaraco commentedon Aug 15, 2024
It does seem that this works:
I wonder if it's possible to have
mainconvert the type annotation so that the simpler syntax also works.jaraco commentedon Aug 15, 2024
Another feature that autocommand has was the ability to automatically create long and short switches. Typer will only infer long names, and if you want to specify the short name, you have to supply the long name as well :(. Reported.