-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
typer officially does not support asyncio, there's a thread about it fastapi/typer#88 essentially we are using a decorator based workaround which has been adapted from a solution posted by @gilcu2 on his comment https://github.com/tiangolo/typer/issues/88\#issuecomment-1732469681 note that this requires the use of asyncer which uses a previous version of anyio, i am willing to live with this for now until an official solution is published by typer
- Loading branch information
Showing
6 changed files
with
88 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
""" Asyncio patches for Typer | ||
There's a thread open on the typer repo about this: | ||
https://github.com/tiangolo/typer/issues/88 | ||
Essentially, Typer doesn't support async functions. This is an issue | ||
post migration to async, @csheppard points out that there's a work | ||
around using a decorator on the click repository: | ||
https://github.com/tiangolo/typer/issues/88#issuecomment-612687289 | ||
@gilcu2 posted a similar solution on the typer repo: | ||
https://github.com/tiangolo/typer/issues/88#issuecomment-1732469681 | ||
this particular one uses asyncer to run the async function in a thread | ||
we're going in with this with the hope that the official solution is | ||
closer to this than a decorator per command. | ||
""" | ||
import inspect | ||
|
||
from functools import ( | ||
partial, | ||
wraps, | ||
) | ||
|
||
import asyncer | ||
from typer import Typer | ||
|
||
|
||
class AsyncTyper(Typer): | ||
@staticmethod | ||
def maybe_run_async(decorator, f): | ||
if inspect.iscoroutinefunction(f): | ||
|
||
@wraps(f) | ||
def runner(*args, **kwargs): | ||
return asyncer.runnify(f)(*args, **kwargs) | ||
|
||
decorator(runner) | ||
else: | ||
decorator(f) | ||
return f | ||
|
||
def callback(self, *args, **kwargs): | ||
decorator = super().callback(*args, **kwargs) | ||
return partial(self.maybe_run_async, decorator) | ||
|
||
def command(self, *args, **kwargs): | ||
decorator = super().command(*args, **kwargs) | ||
return partial(self.maybe_run_async, decorator) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters