-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create a decorator for both sync and async function #159
Comments
Python 3.12 finally added
Of course you could wrap Background Python allows creating callables which are not coroutines (not defined with So everyone who uses
So instead of if inspect.iscoroutinefunction(f):
result = await f(...) # correct
else:
result = f(...) # could be wrong, f might return an awaitable the universally correct solution is result = f(...)
if inspect.isawaitable(result):
result = await result And if we're in a wrapper that needs to support both sync and async functions, you just return whatever return f(...) I infer So |
In Python without
decorator
package, if I want to create a custom decorator that supports both sync and async function, I can write the following:However, with the
decorator
package, how can I write a custom decorator that supports both sync and async function?I've tried this way: (minimal example)
In this way, I can call the async
asum
function withawait asum(1, 2)
, but if I check whetherasum
is a coroutine function withinspect.iscoroutinefunction
, it returnsFalse
.I've also tried another way:
But this time, calling
inspect.iscoroutinefunction(sum)
andinspect.iscoroutinefunction(asum)
will both returnTrue
.Is there any way I can write a custom decorator that supports both sync and async function with
decorator
package? If not, would you consider supporting this use case?The text was updated successfully, but these errors were encountered: