Code Monkey home page Code Monkey logo

Comments (11)

micheles avatar micheles commented on July 24, 2024

The goal of the decorator module is to provide a framework on top of which you can build useful decorators. It is not meant to be a library of decorators, so I cannot accept this kind of contributions. Still, if somebody wants to build a library of decorators on top of my module I am pretty happy with that ;-)

from decorator.

Victor-Savu avatar Victor-Savu commented on July 24, 2024

Thanks for the insights! I had a look at the code, mainly at the workhorse class that creates functions and I am not sure it can be used as is for generating coroutine functions, especially since the regular expression for verifying the function expects it to begin with def and not async def. So I am wondering if it would even be possible to leverage your library for creating such a decorator.

from decorator.

micheles avatar micheles commented on July 24, 2024

You raise an interesting point. I think that the decorator module should be made able to decorate async functions. I have not done that until now since I still need to support Python 2. But it is something I should think about and work on, when I will have the time. I am reopening the issue.

from decorator.

Victor-Savu avatar Victor-Savu commented on July 24, 2024

Thanks! Just to clarify: async functions are decorated more-or-less correctly since calling them produces an awaitable object. The problem is that in the process an important part of their signature is gone: the fact that they are coroutine functions.
I know that Python 2 compatibility requires a lot of jumping through hoops in order to provide support for new Python 3 features, so kudos on your awesome results!

from decorator.

micheles avatar micheles commented on July 24, 2024

I have just pushed in master a change that make decorators preserving the iscoroutinefunction property. Can you test it? It seems to be working with the async def syntax, but not with @asyncio.coroutine.
I do not have code using coroutines, but if you report that it works well I could make a new release 4.1.0
with this new feature.

from decorator.

Victor-Savu avatar Victor-Savu commented on July 24, 2024

Thanks for taking this up! The decorated function is now a coroutine function, but it returns another coroutine function, so two awaits are necessary:

from decorator import decorator
from asyncio import get_event_loop
from inspect import iscoroutinefunction

def awt(coro):
    """ A synchronous version for await """
    return get_event_loop().run_until_complete(coro)
 
@decorator
async def foo(coro, *args, **kwargs):
    return "<before>" + (await coro(*args, **kwargs)) + "<after>")

@foo
async def baba(ba):
    return ba

async def main():
    print(iscoroutinefunction(baba))  # prints: True

    print(await baba("banana"))  # prints: <coroutine object foo at 0x7f1dc04fc308>
                                 # and issues: RuntimeWarning: coroutine 'foo' was never awaited

    print(await (await baba("banana"))) # prints: <before>banana<after>

awt(main())

Before the change, one await was enough.

Regarding the @asyncio.coroutine decorator, it is perfectly acceptable since the decorated function would not satisfy iscoroutinefunction anyway:

In [9]: @asyncio.coroutine
   ...: def foo():
   ...:     a = yield from bar()
   ...:     return a
   ...: 

In [10]: iscoroutinefunction(foo)
Out[10]: False

from decorator.

micheles avatar micheles commented on July 24, 2024

I have fixed this in the current master. Let me know if you find other unexpected behaviors, I guess there are corner cases.

from decorator.

Victor-Savu avatar Victor-Savu commented on July 24, 2024

Thanks! I'll only be able to do a thorough review in the beginning of next week. Would that be OK?

from decorator.

Victor-Savu avatar Victor-Savu commented on July 24, 2024

I am sorry it took so long! I've had three rather busy weeks. However, I wanted to say that I think the implementation works great and I couldn't find any other problems with it. I am very happy with the solution and I want to thank you very much for your continued work!

from decorator.

micheles avatar micheles commented on July 24, 2024

from decorator.

Victor-Savu avatar Victor-Savu commented on July 24, 2024

Thanks! I hope you had a nice time at EuroPython! I'll keep an eye open for the recorded sessions on YouTube.

from decorator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.