-
Notifications
You must be signed in to change notification settings - Fork 56
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
Raise error if EventEmitter used with async callback #312
Conversation
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.
is there no way to do this within python's typing instead of throwing our own error? otherwise lg
i think next steps would be to detect whether it's a coro and wrap it ourselves, because i agree the method of doing it right now is kind of cumbersome |
AFAIK python doesn't have any runtime type checking facilities. We do provide the necessary type hints but I think it requires some special configuration to get your local IDE to pull them through correctly.
Yes it's easy to accept coroutines and then create a task to run them in the |
assert len(calls) == 2 | ||
assert calls[0] == (1, 2, 3) | ||
assert calls[1] == (1, 2, 3, 4, 5) | ||
assert calls == [(1, 2, 3, 4, 5), (1, 2)] |
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 test was flaky because callbacks are stored in a set
and thus aren't explicitly ordered, and this check relied on a particular callback order to work.
I broke the test in two (one for regular args and one for varargs) and fixed the flakiness
Some reasonable feedback arrived in community that it's confusing that you can't use async handlers for events in this SDK.
I looked at the complexity of supporting async handlers and it seems like the largest barrier would be how to await them? Either the
.emit()
method would need to become async and await them directly, which sounds wrong as you wouldn't expect.emit()
to block on long-running applications, or you'd need to create tasks to run them asynchronously but then you have the question of where to collect the tasks and how to cancel them or cleanup.I think it's reasonable to pass this onto the end-developer to collect their own async tasks but we can make it easier to discover this and harder to write subtle bugs. Raising an error with an explanation and workaround suggestion if you pass an async callback seems like a good start.