Skip to content

Dunder attributes added to wrapped function objects #282

@lazorchakp

Description

@lazorchakp

I've come across a case similar to #93 - I have wrapped a function using a wrapt.function_wrapper, but this adds attributes (dunder methods) to the resulting function that are not present on the unwrapped function.

The original issue discusses several difficulties with implementing a fix on ObjectProxy. I'm wondering if limiting the scope of a fix to just FunctionWrapper would be more reasonable / acceptable.

A simplified demonstration of the issue:

import wrapt

def my_func(): pass

@wrapt.function_wrapper
def wrapper(wrapped, instance, args, kwargs):
    return wrapped(*args, **kwargs)

wrapped_func = wrapper(my_func)

assert not hasattr(my_func, "__add__")
assert not hasattr(wrapped_func, "__add__")  # fails

I'm mostly curious about the feasibility of defining __getattribute__ on FunctionWrapper (or possibly _FunctionWrapperBase). I understand that doing this directly on ObjectProxy would have too much of a negative performance impact, but I don't expect attribute access to occur often enough on function objects for this to be a concern for FunctionWrappers. I've tried out a few fixes, but so far haven't been able to come up with a correct implementation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions