Is there a non-performance regressive way to proxy attr access to func.__name__ of the partial function (or method; Callable)?
Would this affect documentation generation tools like e.g. sphinx-spidoc, which IIRC use __name__ and probably now __qualname__ for generating argspecs in RST for HTML and LaTeX?
- functions and methods have __name__ and __qualname__
- see: sphinx.utils.inspect
> partial Objects¶
> partial objects are callable objects created by partial(). They have three read-only attributes:
>
> partial.func
> A callable object or function. Calls to the partial object will be forwarded to func with new arguments and keywords.
>
> partial.args
> The leftmost positional arguments that will be prepended to the positional arguments provided to a partial object call.
>
> partial.keywords
> The keyword arguments that will be supplied when the partial object is called.
> partial objects are like function objects in that they are callable, weak referencable, and can have attributes. There are some important differences. For instance, the __name__ and __doc__ attributes are not created automatically. Also, partial objects defined in classes behave like static methods and do not transform into bound methods during instance attribute look-up.
```python
def unwrap_all(obj: Any, *, stop: Optional[Callable] = None) -> Any:
"""
Get an original object from wrapped object (unwrapping partials, wrapped
functions, and other decorators).
"""
while True:
if stop and stop(obj):
return obj
elif ispartial(obj):
obj = obj.func
elif inspect.isroutine(obj) and hasattr(obj, '__wrapped__'):
obj = obj.__wrapped__ # type: ignore
elif isclassmethod(obj):
obj = obj.__func__
elif isstaticmethod(obj):
obj = obj.__func__
else:
return obj
```
```python
def unpartial(obj: Any) -> Any:
"""Get an original object from partial object.
This returns given object itself if not partial.
"""
while ispartial(obj):
obj = obj.func
return obj
def ispartial(obj: Any) -> bool:
"""Check if the object is partial."""
return isinstance(obj, (partial, partialmethod))
```
Is there a non-performance regressive way to proxy attr access to __name__ of the partially curried (?) function?
From "PEP 3155 – Qualified name for classes and functions"
> ### Limitations
> With nested functions (and classes defined inside functions), the dotted path will not be walkable programmatically as a function’s namespace is not available from the outside. It will still be more helpful to the human reader than the bare __name__.
>
> As the __name__ attribute, the __qualname__ attribute is computed statically and it will not automatically follow rebinding.
> Proxy Object Attributes
> When an attempt is made to access an attribute from the proxy, the same named attribute would in normal circumstances be accessed from the wrapped object. When updating an attributes value, or deleting the attribute, that change will also be reflected in the wrapped object.
> weakref.proxy(object[, callback])¶
> Return a proxy to object which uses a weak reference. This supports use of the proxy in most contexts instead of requiring the explicit dereferencing used with weak reference objects. The returned object will have a type of either ProxyType or CallableProxyType, depending on whether object is callable. Proxy objects are not hashable regardless of the referent; this avoids a number of problems related to their fundamentally mutable nature, and prevent their use as dictionary keys. callback is the same as the parameter of the same name to the ref() function.