fix(autodoc): handle IndexError in get_type_comment for empty source#14443
Open
jlaportebot wants to merge 1 commit into
Open
fix(autodoc): handle IndexError in get_type_comment for empty source#14443jlaportebot wants to merge 1 commit into
jlaportebot wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #14345
When
inspect.getsourcereturns whitespace-only source (e.g."\n"), which can happen on Python 3.14+ for classes with a custom__module__attribute,ast.parseproduces an emptyModulebody. Accessingmodule.body[0]then raisesIndexError, which propagates as an autodoc warning:Fix
Add
IndexErrorto the existingexcept SyntaxErrorclause inget_type_comment()so it gracefully returnsNoneinstead of propagating the error.This is a one-line change that follows the same pattern as the existing
OSError/TypeError/SyntaxErrorhandlers — when source parsing fails for any reason, returnNoneand let autodoc fall back to other introspection methods.Test Plan
get_type_commentwith mockedgetsourcereturning"\n"now returnsNoneinstead of raisingIndexErrorget_type_commentwith valid source still works correctlyast.parse("\n").bodyis[](confirming the root cause)