functools wraps - Serbipunk/notes GitHub Wiki

https://stackoverflow.com/questions/308999/what-does-functools-wraps-do

from functools import wraps

def logged(func):
    def with_logging(*args, **kwargs):
        print(func.__name__ + "was called")
        return func(*args, **kwargs)
    return with_logging

@logged
def f(x):
    return x + x*x

f(5)

functools.py

def wraps(wrapped,
          assigned = WRAPPER_ASSIGNMENTS,
          updated = WRAPPER_UPDATES):
    """Decorator factory to apply update_wrapper() to a wrapper function

       Returns a decorator that invokes update_wrapper() with the decorated
       function as the wrapper argument and the arguments to wraps() as the
       remaining arguments. Default arguments are as for update_wrapper().
       This is a convenience function to simplify applying partial() to
       update_wrapper().
    """
    return partial(update_wrapper, wrapped=wrapped,
                   assigned=assigned, updated=updated)

F = TypeVar("F", bound=Callable[..., Any])

    @wraps(func)
    def wrapper(self: Any, *args: Any, **kwargs: Any) -> Any:
        if self.read_only:
            raise RuntimeError(
                f"Cannot perform '{func.__name__}' on read-only graph. "
                f"Use ToMemoryTransform to create a writable copy. "
                f"See: https://zozonz.github.io/gnn-data/latest/components/transforms/to_memory/"
            )
        return func(self, *args, **kwargs)

    return wrapper  # type: ignore[return-value]