Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ShivKJ opened this issue Jan 16, 2023 · 3 comments

Comments

@ShivKJ
Copy link

ShivKJ commented Jan 16, 2023

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 ShivKJ changed the title Avoiding setting __name__ attribute if it is missing. Making __name__ optional attribute of decorated-function if underlying function is missing it. Jan 16, 2023
@micheles
Copy link
Owner

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

@micheles
Copy link
Owner

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

@ShivKJ
Copy link
Author

ShivKJ commented Jan 18, 2023

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants