`__set_name__` not called on class construction for `wrapt.decorator`-wrapped descriptors
See original GitHub issuewrapt
version: 1.12.1
I was using wrapt.decorator
in combination with descriptors. However, I found that __set_name__
was not called on the descriptor when it’s being decorated:
import wrapt
@wrapt.decorator
def decorator(wrapped, instance, args, kwargs):
return wrapped(*args, **kwargs)
class descriptor_wrapper:
def __init__(self, descriptor):
self.__wrapped__ = descriptor
def __set_name__(self, owner, name):
print("__set_name__", self, owner, name)
if hasattr(self.__wrapped__, "__set_name__"):
self.__wrapped__.__set_name__(owner, name)
def __get__(self, instance, owner=None):
print("__get__", self, instance, owner)
return self.__wrapped__.__get__(instance, owner)
class MyClass:
@descriptor_wrapper
def this_works(self):
pass
@decorator
@descriptor_wrapper
def this_doesnt_work(self):
pass
The output is:
__set_name__ <__main__.descriptor_wrapper object at 0x7f66360bac40> <class '__main__.MyClass'> this_works
But it is expected that __set_name__
be called for both descriptors.
On the other hand, lookup on the decorated descriptor seems to work:
>>> hasattr(MyClass.__dict__["this_doesnt_work"], "__set_name__")
True
>>> MyClass.__dict__["this_doesnt_work"].__set_name__(MyClass, "this_doesnt_work")
__set_name__ <__main__.descriptor_wrapper object at 0x7f66360baa00> <class '__main__.MyClass'> this_doesnt_work
but it just doesn’t get called during class construction.
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
adding descriptor with setattr doesn't call __set_name__ - ...
I'm using setattr to add a long list of descriptors to a class, and I have discovered that __set_name__ of the descriptor is...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The
__set_name__
support when usingwrapt.decorator
is included in 1.13.0.BTW, if interested in playing with pickling further and want an easy way of overriding the builtin
FunctionWrapper
to add the pickle methods, in 1.13.0 you can do:Version 1.13.0 is available on PyPi at the moment as release candidates.
Anyway, going to close out this issue at this point.
Have made changes to add
__set_name__()
propagation toFunctionWrapper
, but notObjectProxy
.You can try
develop
branch if you want to verify it works for you.