Skip to content

Autodoc "list index out of range" error on class with custom __module__ #14345

@bluetech

Description

@bluetech

Describe the bug

In pytest we have class (edited for clarity)

# Defined in `_pytest.warning_types`, re-exported from `pytest`.
class PytestUnraisableExceptionWarning(PytestWarning):
    __module__ = "pytest"

When building the docs on Python 3.14, we get a warning (which leads to an error due to -Werror):

error while formatting arguments for pytest.PytestUnraisableExceptionWarning: list index out of range [autodoc]

From what I can see, this happens in get_type_comment. It runs inspect.getsource on PytestUnraisableExceptionWarning, and while in Python 3.13 it used to raise OSError due to the "wrong" __module__, in Python 3.14 it seems like it just returns "\n". Then Sphinx runs ast.parse("\n"), which returns an empty Module and sphinx then accesses module.body[0] which fails. So adding an IndexError to the try-catch fixes the problem (what we'll do as a workaround).

How to Reproduce

A bit short of time to produce an MRE, sorry. I can do it later if needed.

The steps to reproduce are:

Environment Information

Platform:              linux; (Linux-6.18.13-arch1-1-x86_64-with-glibc2.43)
Python version:        3.14.3 (main, Feb 13 2026, 15:31:44) [GCC 15.2.1 20260209])
Python implementation: CPython
Sphinx version:        9.1.0
Docutils version:      0.22.4
Jinja2 version:        3.1.6
Pygments version:      2.19.2

Sphinx extensions

sphinx-basic-ng                1.0.0b2
sphinx-inline-tabs             2025.12.21.14
sphinx-issues                  6.0.0
sphinx-removed-in              0.2.3
sphinxcontrib-applehelp        2.0.0
sphinxcontrib-devhelp          2.0.0
sphinxcontrib-htmlhelp         2.1.0
sphinxcontrib-jsmath           1.0.1
sphinxcontrib-qthelp           2.0.0
sphinxcontrib-serializinghtml  2.0.0
sphinxcontrib-svg2pdfconverter 2.1.0
sphinxcontrib-towncrier        0.5.0a0
sphinxcontrib-trio             1.2.0



extensions = [
    "pygments_pytest",
    "sphinx.ext.autodoc",
    "sphinx.ext.autosummary",
    "sphinx.ext.intersphinx",
    "sphinx.ext.todo",
    "sphinx.ext.viewcode",
    "sphinx_removed_in",
    "sphinx_inline_tabs",
    "sphinxcontrib_trio",
    "sphinxcontrib.towncrier.ext",
    "sphinx_issues",
]

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions