✨ feat(stubs): resolve type hints from .pyi stub files (#161)#630
Merged
gaborbernat merged 1 commit intotox-dev:mainfrom Feb 24, 2026
Merged
✨ feat(stubs): resolve type hints from .pyi stub files (#161)#630gaborbernat merged 1 commit intotox-dev:mainfrom
gaborbernat merged 1 commit intotox-dev:mainfrom
Conversation
d22d9e5 to
e9b2bb3
Compare
C extensions and Cython modules ship .pyi stubs for type checkers but sphinx-autodoc-typehints had no way to read them. Neither typing.get_type_hints() nor inspect.getsource() works on these modules, leaving their docs without type information. Added a third fallback in get_all_type_hints() that locates sibling .pyi files, parses them with ast.parse(), and extracts annotation strings by matching qualified names through the AST. The parsed trees are cached to avoid re-parsing across multiple objects in the same module.
e9b2bb3 to
e7a533b
Compare
Collaborator
|
This is a landmark feature. How soon can this be included in a release? |
Member
Author
|
Will cut a release before end of the week. |
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.
C extensions, Cython projects, and other compiled modules ship
.pyistub files for static type checkers, but sphinx-autodoc-typehints had no way to read them.typing.get_type_hints()only works with runtime__annotations__, andinspect.getsource()fails on compiled modules, so both existing resolution paths return nothing. This left these projects with no type information in their Sphinx docs.This PR adds a third fallback step in
get_all_type_hints()that locates sibling.pyifiles viainspect.getfile(), parses them withast.parse(), and extracts annotation strings by matching__qualname__through the AST tree. Parsed stubs are cached at module level to avoid re-parsing when processing multiple objects from the same module. The resolution priority remains: runtime annotations > type comments > stub annotations, so existing behavior is fully preserved.Closes #161