Description
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:
jupyterlite-sphinx/jupyterlite_sphinx/_try_examples.py
Lines 390 to 400 in d2a34cb
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