Skip to content

Conversation

@jbms
Copy link
Contributor

@jbms jbms commented Oct 6, 2025

Fixes #13934

This commit ensures that the __annotations__ dict of a class is updated only with type comment-derived annotations for direct members; the type comment-derived annotations for parent classes are added to those parent classes own __annotations__ dicts. The inherited member annotations are still found by typing.get_type_hints.

This commit also improves the efficiency of incorporating the annotations found by the ModuleAnalyzer. Previously, each call to _load_object_by_name for a data member or attribute would result in separately iterating over all attributes (including within classes) found by ModuleAnalyzer for the containing module, and in the case of classes, the containing modules of all parent classes. For modules with many members this cost could likely be significant. With this commit, at least for the purpose of updating __annotations__ dicts, each module's attributes only processed a single time in total.

Fixes sphinx-doc#13934

This commit ensures that the `__annotations__` dict of a class is
updated only with type comment-derived annotations for direct members;
the type comment-derived annotations for parent classes are added to
those parent classes own `__annotations__` dicts.  The inherited
member annotations are still found by `typing.get_type_hints`.

This commit also improves the efficiency of incorporating the
annotations found by the ModuleAnalyzer.  Previously, each call to
_load_object_by_name for a data member or attribute would result in
separately iterating over all attributes (including within classes)
found by ModuleAnalyzer for the containing module, and in the case of
classes, the containing modules of all parent classes.  For modules
with many members this cost could likely be significant.  With this
commit, at least for the purpose of updating `__annotations__` dicts,
each module's attributes only processed a single time in total.
@AA-Turner AA-Turner merged commit f3e8999 into sphinx-doc:master Oct 6, 2025
34 checks passed
@AA-Turner
Copy link
Member

Thanks @jbms!

A

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 4, 2025
@AA-Turner AA-Turner added this to the 9.0.0 milestone Nov 25, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

autodoc adds parent class members to __annotations__ dict

2 participants