Skip to content

fix(autodoc): handle IndexError in get_type_comment for empty source#14443

Open
jlaportebot wants to merge 1 commit into
sphinx-doc:masterfrom
jlaportebot:fix/autodoc-indexerror-type-comments
Open

fix(autodoc): handle IndexError in get_type_comment for empty source#14443
jlaportebot wants to merge 1 commit into
sphinx-doc:masterfrom
jlaportebot:fix/autodoc-indexerror-type-comments

Conversation

@jlaportebot
Copy link
Copy Markdown

Summary

Fixes #14345

When inspect.getsource returns whitespace-only source (e.g. "\n"), which can happen on Python 3.14+ for classes with a custom __module__ attribute, ast.parse produces an empty Module body. Accessing module.body[0] then raises IndexError, which propagates as an autodoc warning:

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

Fix

Add IndexError to the existing except SyntaxError clause in get_type_comment() so it gracefully returns None instead of propagating the error.

This is a one-line change that follows the same pattern as the existing OSError/TypeError/SyntaxError handlers — when source parsing fails for any reason, return None and let autodoc fall back to other introspection methods.

Test Plan

  • Verified that get_type_comment with mocked getsource returning "\n" now returns None instead of raising IndexError
  • Verified that get_type_comment with valid source still works correctly
  • Verified that ast.parse("\n").body is [] (confirming the root cause)

When inspect.getsource returns whitespace-only source (e.g. "\n"),
which can happen on Python 3.14+ for classes with a custom __module__
attribute, ast.parse produces an empty Module body. Accessing
module.body[0] then raises IndexError.

Add IndexError to the except clause so get_type_comment gracefully
returns None instead of propagating the error.

Closes sphinx-doc#14345
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants