How can I replace *args
and **kwargs
with the real signature in the documentation of decorated functions?
Let's say I have the following decorator and decorated function:
import functools
def mywrapper(func):
@functools.wraps(func)
def new_func(*args, **kwargs):
print('Wrapping Ho!')
return func(*args, **kwargs)
return new_func
@mywrapper
def myfunc(foo=42, bar=43):
"""Obscure Addition
:param foo: bar!
:param bar: bla bla
:return: foo + bar
"""
return foo + bar
Accordingly, calling print(myfunc(3, 4))
gives us:
Wrapping Ho!
7
So far so good. I also want my library containing myfunc
properly documented with Sphinx.
However, if I include my function in my sphinx html page via:
.. automodule:: mymodule
:members: myfunc
It will actually show up as:
myfunc(*args, **kwargs)
Obscure Addition
- Parameters:
- foo: bar!
- bar: bla bla
- Returns: foo + bar
How can I get rid of the generic myfunc(*args, **kwargs)
in the title? This should be replaced by myfunc(foo=42, bar=43). How can I change sphinx or my decorator mywrapper
such that the default keyword arguments are preserved in the documentation?
EDIT:
As pointed out this question has been asked before, but the answers are not so helpful.
However, I had an idea and wonder if this is possible. Does Sphinx set some environment variable that tells my module that it is actually imported by Sphinx? If so, I could simply monkey-patch my own wrappers. If my module is imported by Sphinx my wrappers return the original functions instead of wrapping them. Thus, the signature is preserved.