Usually you shouldn't be checking for couroutine functions directly. There are too many cases where something returns in fact a coroutine but you cannot tell by inspecting the thing you call -- you can only tell once you've called it (using iscoroutine()). Partials are just one approach -- lambdas are a different one (personally I usually prefer lambdas over partials). An advantage of using a lambda (or a plain old function) is that you can decorate it with @coroutine.
(Note that there are a few places in asyncio that check for coroutine functions -- but always in the negative, in places where callbacks are taken that should *not* be coroutines. And this test doesn't need to be perfect -- it's just helpful to call out common anti-patterns early.) On Mon, Feb 16, 2015 at 11:26 AM, Ron Cohen <[email protected]> wrote: > Hey! > > In APIs that take a coroutine callbacks as a parameter, one would like to > make sure the callback given is in fact a coroutine. Partials are of course > useful in this scenario. > It occurs to me that `asyncio.iscoroutinefunction` should know about > coroutines wrapped in partials: > > >>> import asyncio > >>> import functools > >>> @asyncio.coroutine > ... def myfunc(): > ... pass > ... > >>> part = functools.partial(myfunc) > >>> asyncio.iscoroutinefunction(myfunc) > True > >>> asyncio.iscoroutinefunction(part) > False > >>> > > Here's a naive workaround: > > def iscoroutinefunction(func): > if asyncio.iscoroutinefunction(func): > return True > > return asyncio.iscoroutinefunction(getattr(func, 'func', None)) > > > What do you think? > > Best regards > Ron Cohen > CTO & co-founder @ Opbeat > -- --Guido van Rossum (python.org/~guido)
