-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add support for decorating async functions #33
Conversation
Needs improvement - this is just a copy-pasta
) | ||
# Reraise exception | ||
raise exception | ||
|
||
"""Decorator for tracking function calls and duration.""" | ||
|
||
def decorator(func: Callable[P, T]) -> Callable[P, T]: | ||
module_name = get_module_name(func) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this logic might not need to live here too — i could try factoring out of deocrator
and async_decorator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work! Got a couple of minor points of feedback though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
|
||
if func is None: | ||
return decorator | ||
return sync_decorator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm actually not quite sure what this accomplishes! now that we have two different decorators (one for sync, one for async) i am a slightly worried about returning the sync decorator here when the func
arg is None
.
i did try a version of this PR that used one decorator, but to get the value of an async function synchronously, you have to do some tricks with the event loop which seemed like they were against best practices. e.g.,:
import asyncio
async def async_function():
# Your async code here
return result
def sync_function():
loop = asyncio.get_event_loop()
result = loop.run_until_complete(async_function())
return result
anyways, we don't seem to document this case (where the decorator is called without being attached to a function), so this might not be a problem
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of returning different decorators, would it be possible to use only one and check whether the result is a coroutine?
https://docs.python.org/3/library/inspect.html#inspect.iscoroutine
This PR adds support for async functions, making the following changes:
iscoroutinefunction
autometrics
decorator, usingCallable[P, Awaitable[T]]
as part of the type signaturedecorator
andasync_decorator
test_decorator.py
(which required adding thepytest-asyncio
module to dev dependencies)fastapi-example.py
exampleFixes #29