Preserve default arguments of wrapped/decorated Python function in Sphinx documentation

前端 未结 2 802
攒了一身酷
攒了一身酷 2021-01-05 01:00

How can I replace *args and **kwargs with the real signature in the documentation of decorated functions?

Let\'s say I have the following d

2条回答
  •  有刺的猬
    2021-01-05 01:31

    I came up with a monkey-patch for functools.wraps. Accordingly, I simply added this to the conf.py script in my project documentation's sphinx source folder:

    # Monkey-patch functools.wraps
    import functools
    
    def no_op_wraps(func):
        """Replaces functools.wraps in order to undo wrapping.
    
        Can be used to preserve the decorated function's signature
        in the documentation generated by Sphinx.
    
        """
        def wrapper(decorator):
            return func
        return wrapper
    
    functools.wraps = no_op_wraps
    

    Hence, when building the html page via make html, functools.wraps is replaced with this decorator no_op_wraps that does absolutely nothing but simply return the original function.

提交回复
热议问题