Skip to content

Comments

✨ feat(stubs): resolve type hints from .pyi stub files (#161)#630

Merged
gaborbernat merged 1 commit intotox-dev:mainfrom
gaborbernat:feat/238-warning-context
Feb 24, 2026
Merged

✨ feat(stubs): resolve type hints from .pyi stub files (#161)#630
gaborbernat merged 1 commit intotox-dev:mainfrom
gaborbernat:feat/238-warning-context

Conversation

@gaborbernat
Copy link
Member

@gaborbernat gaborbernat commented Feb 24, 2026

C extensions, Cython projects, and other compiled modules ship .pyi stub files for static type checkers, but sphinx-autodoc-typehints had no way to read them. typing.get_type_hints() only works with runtime __annotations__, and inspect.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 .pyi files via inspect.getfile(), parses them with ast.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

@gaborbernat gaborbernat force-pushed the feat/238-warning-context branch 3 times, most recently from d22d9e5 to e9b2bb3 Compare February 24, 2026 17:55
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.
@gaborbernat gaborbernat force-pushed the feat/238-warning-context branch from e9b2bb3 to e7a533b Compare February 24, 2026 18:00
@gaborbernat gaborbernat merged commit bcde385 into tox-dev:main Feb 24, 2026
8 checks passed
@agronholm
Copy link
Collaborator

This is a landmark feature. How soon can this be included in a release?

@gaborbernat
Copy link
Member Author

Will cut a release before end of the week.

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.

support stub files explicitly

2 participants