Comments (11)
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.
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.
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.
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.
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.
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.
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.
Thanks! I'll only be able to do a thorough review in the beginning of next week. Would that be OK?
from decorator.
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.
from decorator.
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)
- `decorator.contextmanager` instances do not work properly as decorators HOT 2
- 5.0.9: sphinx 4.x warnings HOT 3
- pylint warning W1113: keyword-arg-before-vararg HOT 1
- Regression in 5.1.0 - decorator.contextmanager no longer compat with contextlib.contextmanager HOT 4
- `FunctionMaker.create()` fails when given a function definition with a return type annotation. HOT 1
- Functionmaker.create (from doc example) fails with kwarg-only functions HOT 6
- `FunctionMaker.create` raises unexpected SyntaxError when return is present as substring for async function
- How to make `@decorator` with other decorators? HOT 3
- TypeError: missing 1 required positional argument HOT 1
- <=3.4.2 can't be installed with modern setuptools HOT 2
- Continuous fuzzing by way of OSS-Fuzz
- compatibility issue with kwargs HOT 2
- Not preserving low level signature metadada HOT 1
- decorator whether to support decorator partial functions?
- Confusion about license using HOT 1
- Making __name__ optional attribute of decorated-function if underlying function is missing it. HOT 3
- Compatibility with Python 3.12 - RuntimeError: There is no current event loop in thread 'MainThread'.
- Unable to pull 4.4.2 from the repo only pulls in 4.2.1 HOT 1
- [BUG] kwargs are not respected HOT 3
- Create SECURITY.md
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from decorator.