How do I reference a documented Python function parameter using Sphinx markup?

前端 未结 4 1157
闹比i
闹比i 2021-02-06 21:29

I\'d like to reference a previously-documented function parameter elsewhere in a Python docstring. Consider the following (admittedly completely artificial) example:

         


        
4条回答
  •  [愿得一人]
    2021-02-06 21:55

    For those, that want to use sphinx-paramlinks with sphinx.ext.napoleon here is a patch. Simple find the right fragment in the sphinx-paramlinks source code (sphinx_paramlinks\sphinx_paramlinks.py, somewhere around line 50) and replace it with this:

    def cvt(m):
        directive, modifier, objname, paramname = (
            m.group(1), m.group(2) or '', name, m.group(3))
        if directive == 'param':
            refname = _refname_from_paramname(paramname, strip_markup=True)
            item = ('single', '%s (%s parameter)' % (refname, objname),
                    '%s.params.%s' % (objname, refname), '')
            if LooseVersion(__version__) >= LooseVersion('1.4.0'):
                item += (None,)
            doc_idx.append(item)
        return ":%s %s_sphinx_paramlinks_%s.%s:" % (
            directive, modifier, objname, paramname)
    return re.sub(r'^:(param|type) ([^:]+? )?([^:]+?):', cvt, line)
    

    Note: remember about the right indent.

    I'm not a Sphinx specialist, but this seems to get the job done.

提交回复
热议问题