gh-94912: Added marker for non-standard coroutine function detection by carltongibson · Pull Request #99247 · python/cpython (original) (raw)
Now, I'm not entirely sure why you're testing for call
OK, so the issue is we have classes like this:
class Cl:
async def __call__(self):
pass
... where we need instances to be identified as async, and iscoroutinefunction
doesn't (currently) pick it up.
Note that this is an actual, already-out-there, need. e.g. the Starlette framework has this exact check to work around this limitation. asgiref
works around this limitation in its usage by marking the instance itself with the _is_coroutine
marker.
Either way, I think it is desirable to return True
for class instance of classes such as Cl
with an async def __call__()
So that's the reason for adding the additional ... _has_code_flag(obj.__call__, ...)
check.
In this PR, given that we were adding @staticmethod
and @classmethod
and so on, it seemed that we also should cover this kind of case:
class Cl2:
@inspect.markcoroutinefunction
def __call__(self):
pass
Which leads to needing similar in the _is_coroutine_marker
block.
The latter case is somewhat hypothetical... — it comes from trying to cover all the cases here, rather than from real-use™. To that extent I'd be happy to drop it, but I guess if we do there's an issue in X months time, saying "I need to do this". 🤔
I don't know the correct thing to say. (?)
(FWIW I wonder if the variable _is_coroutine shouldn't be renamed _is_coroutine_marker.)
Yep OK. +1
What would happen if we wrote a helper function like this?
Let me have a read.
Thanks so much for your thought and input on this.