如何将装饰器参数注入docstring?

时间:2019-04-11 13:36:20

标签: python python-sphinx python-decorators docstring

我在我的API中使用了一个特定的装饰器模式,它看起来像这样(简化):

@api(
    path='/double',
    method='GET',
    input={'x': int},
    output={'x_double': int}
)
def double(x):
    """
    This endpoint doubles the number you give it.

    Args:
        x: (int) the number you want to double

    Returns:
        x_double: (int) x * 2

    """
    return jsonify({'x_double': x*2})

我正在使用 Sphinx 生成我的文档。我想将装饰器的参数注入到其装饰函数的文档字符串中。对于上面的示例,我希望文档看起来像这样:

    mypackage.something module

    mypackage.something.double(x)

    This endpoint doubles the number you give it.

    Attributes:
        path: /double
        method: GET

    Args:
        x: (int) the number you want to double

    Returns:
        x_double: (int) x * 2

我该如何实现?我当时正在考虑编写一个脚本,将它们注入到生成的文档中,但是想知道是否有更简单的方法?

此外,是的,装饰器正在使用functools.wraps

0 个答案:

没有答案
相关问题