click_extra.sphinx.myst_docstrings module

Convert MyST-flavored docstrings to reST for sphinx.ext.autodoc.

Hooks into autodoc-process-docstring to convert the MyST markdown of Python docstrings to reStructuredText before Sphinx processes them, so sphinx_autodoc_typehints, autodoc_default_options and every other extension built on sphinx.ext.autodoc keep working. It replaces sphinx-autodoc2, whose native MyST parsing does not work with current Sphinx and docutils. See MyST docstrings for setup, the syntax reference and the limitations.

The conversion is idempotent: docstrings already in reST pass through unchanged. This allows incremental migration one module at a time.

Supported conversions:

  • Cross-references: a MyST role, its name in braces, becomes a reST role, its name between colons.

  • Directive fences, with backticks or colons, become reST directives, their leading option lines right under the directive header.

  • Plain code fences become code-block directives. The content of a code block is kept verbatim.

  • A fence longer than three characters can hold fences of its own character, as in any MyST document. click_extra.markdown.blocks.fence_spans() reads the fences.

  • Inline code spans get the double backticks of reST literals.

  • Markdown links become reST hyperlinks, without the backticks of their label.

  • Footnote references and definitions become auto-numbered reST footnotes.

Field list markers (:param:, :return:) need no conversion; the content inside field list entries is converted normally (inline code, cross-references, links).

Register this extension in your Sphinx conf.py, before sphinx_autodoc_typehints if present, with click-extra[sphinx] in your docs dependency group:

extensions = [
    "sphinx.ext.autodoc",
    "click_extra.sphinx.myst_docstrings",
    "sphinx_autodoc_typehints",  # must come after
]
click_extra.sphinx.myst_docstrings.myst_to_rst(lines)[source]

Convert MyST syntax to reST, modifying lines in place.

The conversion is idempotent: reST-only docstrings pass through unchanged because none of the patterns match reST syntax.

Return type:

None

click_extra.sphinx.myst_docstrings.setup(app)[source]

Sphinx extension entry point.

Raises:

sphinx.errors.ExtensionError – If sphinx_autodoc_typehints is already loaded.