-
-
Notifications
You must be signed in to change notification settings - Fork 346
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
@enable_ki_protection breaks inspect.iscoroutinefunction #2670
Comments
3.12+ has it easy: python/cpython#99247 I'm not sure about before that. |
Here's some spooky stuff that works pre-3.12: (.venv) PS C:\Users\A5rocks\Documents\trio> ipython
Python 3.11.3 (tags/v3.11.3:f3909b8, Apr 4 2023, 23:49:59) [MSC v.1934 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.34.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import functools
In [2]: class Based(functools.partial):
...: def __init__(self, func):
...: super().__new__(functools.partial, func)
...:
...: def __call__(self):
...: print("insert logic to call `self.func` here")
...:
In [3]: @Based
...: async def async_function():
...: ...
...:
In [4]: async_function
Out[4]: Based(<function async_function at 0x000001B09888FF60>)
In [5]: import inspect
In [6]: inspect.iscoroutinefunction(async_function)
Out[6]: True
In [7]: async_function()
insert logic to call `self.func` here |
Nice. Could add something like that to the exported symbols so users can easily slap a decorator onto their non-coroutine wrapper functions and make the transition to using |
Oh yeah, it'll add a frame. But so will my voodoo class-based thing, annoying. |
I don't think That said, the fact that KI protection adds extra frames is a performance problem also and I think there's plenty of room for the KI protection mechanisms to be improved, which would solve this issue as well. #733 has a bunch of previous thoughts on this. |
I don't think it needs to be perfect, but rather a matter of "downsides of calling the function when trio could've instantly errored" vs "forcing users with weird sync-looking functions to add a decorator/wrapper so it actually looks like a coroutine function". Could also add a flag to I don't think the gain is huge though, and don't really know how the above tradeoff plays out in practice. |
I have to admit that I habitually write sync wrappers to async functions. Semantically there's nothing whatsoever wrong with
That being said, the likelihood that I'd use this kind of decorator on a function that's passed to Things would get more annoying if that check also happened when we call |
I propose that we have |
Found when I tried to update
coroutine_or_error
intrio/_util.py
to useinspect.iscoroutinefunction
in #2668trio/trio/_util.py
Lines 137 to 141 in 3146638
There are also a ton of wrapper/helper methods in tests that un-coroutinefunction-ify functions, which'd need to be modified in order to actually use
iscoroutinefunction
incouroutine_or_error
. This means it'd probably also break a bunch of downstream code ifcoroutine_or_error
was rewritten, but the upside of not having to call the function might mean that it's worth it (after a deprecation period)? That's probably for another issue though.The text was updated successfully, but these errors were encountered: