decorator icon indicating copy to clipboard operation
decorator copied to clipboard

Making __name__ optional attribute of decorated-function if underlying function is missing it.

Open ShivKJ opened this issue 2 years ago • 3 comments

Hi,

I created a function A, using decorator.decorator and applied it on another function B.

There were some more decorators (np.vectorize here is the detail) applied on function B but decorator A was the top decorator.

I got error that function B is missing __name__ attribute.

I was going through the decorator code and found this,

    try:
        fun.__defaults__ = func.__defaults__
    except AttributeError:
        pass
    try:
        fun.__kwdefaults__ = func.__kwdefaults__
    except AttributeError:
        pass
    try:
        fun.__annotations__ = func.__annotations__
    except AttributeError:
        pass
    try:
        fun.__module__ = func.__module__
    except AttributeError:
        pass

Can we also make __name__ as optional if underlying function misses this.

ShivKJ avatar Jan 16 '23 14:01 ShivKJ

Yes, we can, I will add this feature to the next release of the decorator module.

micheles avatar Jan 17 '23 07:01 micheles

BTW, you can probably solve your problem the decoration order: instead of


@your_decorator
@vectorize
def func():
    pass

just write

@vectorize
@your_decorator
def func():
    pass

micheles avatar Jan 17 '23 07:01 micheles

@micheles Thanks for incorporating the changes. Could you let me know when are you planning to release it?

@vectorize
@your_decorator
def func():
    pass

I actually wanted to update the output of np.vectorize with the help of decorator and that is why decorator will be on the top.

ShivKJ avatar Jan 18 '23 02:01 ShivKJ