Python Sphinx autodoc and decorated members

前端 未结 6 947
孤城傲影
孤城傲影 2020-12-10 00:51

I am attempting to use Sphinx to document my Python class. I do so using autodoc:

.. autoclass:: Bus
   :members:

While it correctly fetche

6条回答
  •  执念已碎
    2020-12-10 01:27

    If you're particularly adamant about not adding another dependency here's a code snippet that works with the regular inspector by injecting into the docstring. It's quite hackey and not really recommended unless there are good reasons to not add another module, but here it is.

    # inject the wrapped functions signature at the top of a docstring
    args, varargs, varkw, defaults = inspect.getargspec(method)
    defaults = () if defaults is None else defaults
    defaults = ["\"{}\"".format(a) if type(a) == str else a for a in defaults]
    l = ["{}={}".format(arg, defaults[(idx+1)*-1]) if len(defaults)-1 >= idx else arg for idx, arg in enumerate(reversed(list(args)))]
    if varargs: allargs.append('*' + varargs)
    if varkw: allargs.append('**' + varkw)
    doc = "{}({})\n{}".format(method.__name__, ', '.join(reversed(l)), method.__doc__)
    wrapper.__doc__ = doc
    

提交回复
热议问题