Skip to content

try_examples fails if the examples sections is last #291

Closed
@keewis

Description

@keewis

Description

When building the sphinx documentation with sphinx.ext.napoleon and if Examples is the last section, try_examples appears exceed the number of lines in the docstring:

while right_index < len(lines) and not _next_section_pattern.search(
lines[right_index]
):
right_index += 1
if "!! processed by numpydoc !!" in lines[right_index]:
# Sometimes the .. appears on an earlier line than !! processed by numpydoc !!
if not re.search(
r"\.\.\s+\!\! processed by numpy doc \!\!", lines[right_index]
):
while lines[right_index].strip() != "..":
right_index -= 1

If there's no section pattern before the end of the docstring, right_index will be len(lines) in line 394, causing the __getitem__ call to fail with an IndexError. Since the check in line 394 appears to be checking for a special section pattern, it should be fine to just skip the entire block by prepending right_index < len(lines) to the condition. However, that means that new_lines will contain a artificial trailing empty lines, so that would most likely have to be trimmed.

Reproduce

conf.py:

# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

project = 'test'
copyright = '2025, testing'
author = 'testing'

import os
import sys

sys.path.insert(0, os.path.abspath("."))

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = ["jupyterlite_sphinx", "sphinx.ext.autosummary", "sphinx.ext.napoleon"]

templates_path = ['_templates']
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']

global_enable_try_examples = True



# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = 'alabaster'
html_static_path = ['_static']

example.py

#!/usr/bin/env python3

def test(a):
    """
    Parameters
    ----------
    a : int
        first value

    Examples
    --------
    >>> test(1)
    3
    """

    return a * 2 + 1

index.rst:

Welcome to test's documentation!
================================

.. toctree::
   :maxdepth: 2
   :caption: Contents:

.. autosummary::
   :toctree: generated/

   example.test

Expected behavior

No error.

Context

ubuntu 24.04
python 3.12.10 h9e4cc4f_0_cpython conda-forge
jupyterlite-sphinx 0.20.0 pyhd8ed1ab_0 conda-forge

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions