[BUG] Napoleon with Markdown builder misrenders type hints like Optional[str] in docstring “Args” sections
Describe the Bug
When using Sphinx with the Napoleon extension and Markdown builder, type annotations in Google-style docstrings that include square brackets (e.g., Optional[str], (Optional [Mapping [str , Any ] ])) are rendered incorrectly in the generated Markdown output.
To Reproduce
-
Create a Python file
test.pywith the following contents:from typing import Optional
def example(name: Optional[str]): """Example function.
Args:
name (Optional[str]): Optional name parameter.
params Optional[Mapping[str, Any]] Optional query params.
"""
pass
2. Install required packages:
```bash
pip install sphinx sphinx-markdown-builder
-
Generate Sphinx scaffolding:
sphinx-apidoc -o docs . -a --extensions sphinx_markdown_builder --full -
Update
docs/conf.pyto include the following configuration:extensions = [ 'sphinx.ext.autodoc', 'sphinx_markdown_builder', 'sphinx.ext.napoleon', ] napoleon_google_docstring = True napoleon_use_ivar = True html_theme = 'alabaster' html_static_path = ['_static'] -
Build the documentation:
cd docs make markdown -
Open the generated Markdown file:
docs/_build/markdown/test.md
Observed Output
The parameter type renders incorrectly, for example:
* **Parameters:**
* **name** (*Optional* *[**str* *]*): Optional name parameter.
* **params** (*Optional* *[**Mapping* *[**str* *,* *Any* *]* *]*): Optional query params
When previewed in a Markdown viewer, it appears as:
Parameters:
name (Optional *[*str ]) – Optional name parameter.
params (Optional *[*Mapping *[*str , Any ] ]) – Optional query parameters.
The output is correctly dispayed when built through html
Expected Behavior
Type hints should render as literal text or inline code, preserving square brackets and capitalization correctly, e.g.:
- **name** (Optional[`str`]): Optional name parameter.
Adding backticks (`Optional[str]`) fixes the rendering issue manually.
Environment
- OS: Ubuntu
- sphinx-markdown-builder: 0.6.8
Additional Context
- The issue does not occur when
sphinx.ext.napoleonis disabled.