Comments (8)
Maybe because I've gone through the trouble of understanding how normal decorators work, I'd rather not figure how the decorator.decorator abstraction works.
Yeah, but take into account that even if I will be able to implement decorator.wraps
, it will be a lie: it will look like the standard library utility outside, but it will work according to the decorator.decorator
logic inside.
from decorator.
So you would like a decorator.wraps
function which is a fully working replacement of functools.wraps
?
@decorator.wraps(original)
def wrapper(*args, **kwargs):
return original(*args, **kwargs)
Perhaps it is possible, but I will have to think about that. I will keep you posted when I will have time to work on that. I actually like the idea, if it can be implemented.
from decorator.
So you would like a decorator.wraps function which is a fully working replacement of functools.wraps?
Yes, this is what I had in mind. We're contemplating using something this for NumPy (but not yet sure it's worth the complexity/run-time of dynamic code generation): http://www.numpy.org/neps/nep-0018-array-function-protocol.html#changes-within-numpy-functions
from decorator.
Well, both scipy and ipython as already using the decorator module as a dependency. In the NEP that you pointed out, there is an array_function_dispatch
; if this is the only function of such a type you can just define it without using functools.wraps
but with decorator.decorator
, no?
You should also measure potential slowdowns due to the decoration, but I guess you have a performance regression suite just for that.
from decorator.
if this is the only function of such a type you can just define it without using functools.wraps but with decorator.decorator, no?
This is probably true, though I have a slight preference for the functools.wraps
API. Maybe because I've gone through the trouble of understanding how normal decorators work, I'd rather not figure how the decorator.decorator
abstraction works.
You should also measure potential slowdowns due to the decoration, but I guess you have a performance regression suite just for that.
Yes, would definitely be part of the plan before adopting this.
from decorator.
def wraps(func):
"""
:returns: a decorator for a wrapper function similar to functools.wraps
"""
return lambda wrapper: decorate(func, lambda f, *a, **k: wrapper(*a, **k))
should act as a replacement of functools.wrapper
. However, having thought about that, I will not include this in the decorator library. The point of my library is to make decorator easier, not more perverse. You should marry the decorator library and write your array_function_dispatch
somewhat like this, with two levels of nesting less:
@decorator.decorator
def array_function_dispatch(func, dispatcher=None, *args, **kwargs):
"""Wrap a function for dispatch with the __array_function__ protocol."""
relevant_arguments = dispatcher(*args, **kwargs)
success, value = try_array_function_override(
new_func, relevant_arguments, args, kwargs)
if success:
return value
return func(*args, **kwargs)
from decorator.
You should marry the decorator library and write your array_function_dispatch somewhat like this, with two levels of nesting less:
How would I get access to new_func
in this example? (I agree that this is a somewhat unusual use-case.)
from decorator.
Ops, I missed that. In your original code you are calling new_func from inside its own definition?
def new_func(*args, **kwargs):
relevant_arguments = dispatcher(*args, **kwargs)
success, value = try_array_function_override(
new_func, relevant_arguments, args, kwargs)
if success:
return value
return func(*args, **kwargs)
Then I should read the NEP and understand the context, first ;-) But now I am leaving for work, I will come back to this later on.
from decorator.
Related Issues (20)
- 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
- Create a decorator for both sync and async function HOT 1
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.