sphinx-markdown-builder icon indicating copy to clipboard operation
sphinx-markdown-builder copied to clipboard

[BUG] Napoleon with Markdown builder misrenders type hints like Optional[str] in docstring “Args” sections

Open ayeshasaeed665 opened this issue 2 months ago • 0 comments

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

  1. Create a Python file test.py with 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
  1. Generate Sphinx scaffolding:

    sphinx-apidoc -o docs . -a --extensions sphinx_markdown_builder --full
    
  2. Update docs/conf.py to 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']
    
  3. Build the documentation:

    cd docs
    make markdown
    
  4. 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

Image

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.napoleon is disabled.

ayeshasaeed665 avatar Oct 17 '25 16:10 ayeshasaeed665