Skip to content

Comments

autodoc: preserve docstrings for type aliases (fix) [swev-id: sphinx-doc__sphinx-9229]#102

Open
rowan-stein wants to merge 2 commits intosphinx-doc__sphinx-9229from
fix/type-alias-docstrings-9229
Open

autodoc: preserve docstrings for type aliases (fix) [swev-id: sphinx-doc__sphinx-9229]#102
rowan-stein wants to merge 2 commits intosphinx-doc__sphinx-9229from
fix/type-alias-docstrings-9229

Conversation

@rowan-stein
Copy link
Collaborator

Summary
Preserve module-level docstrings for type aliases documented with next-line triple-quoted strings. Previously, aliases to class/type objects (e.g., int) were documented only as “alias of …”, ignoring the alias docstrings.

Related Issue: #101

Reproduction steps

  1. Minimal project (extensions = ['sphinx.ext.autodoc']) with module:
    • SimpleInt = int followed by a next-line docstring
    • MyInt: TypeAlias = int followed by a next-line docstring
    • IntListOrUnion: TypeAlias = Union[List[int], int] followed by a next-line docstring
  2. Build with .. automodule:: using :members: and :undoc-members:.

Observed behavior (before fix)

  • SimpleInt: only “alias of int” (alias docstring ignored)
  • MyInt: only “alias of int” (alias docstring ignored)
  • IntListOrUnion: shows alias docstring and “alias of Union[List[int], int]”
  • No stack trace; this is a rendering/content inconsistency.

Root cause

  • Autodoc chooses ClassDocumenter for aliases to class/type objects and sets doc_as_attr, but it does not merge module-level alias docstrings from ModuleAnalyzer. Aliases to typing expressions use DataDocumenter, which preserves module-level docstrings via attr_docs.

Fix

  • Bias selection toward DataDocumenter for module-level aliases when a module-level alias docstring exists or a TypeAlias annotation is present and the alias points to a type/class with a different __name__. This preserves alias docstrings.
  • Defensive: when ClassDocumenter still handles such aliases under doc_as_attr, merge the module-level alias docstring retrieved from ModuleAnalyzer before appending “alias of …”.

Tests

  • Added tests/roots/test-ext-autodoc/target/type_alias_docstrings.py with aliases covering annotated, unannotated, complex typing expressions, and docstring-free cases.
  • Added assertions in tests/test_ext_autodoc_automodule.py::test_type_alias_docstrings verifying that all alias docstrings are rendered and “alias of …” lines are included.

Local test environment notes

  • Used Python 3.10 in a virtual environment for local testing.
  • Local tests passed (subset focused on autodoc tests).

Expected outcome

  • Docstrings for aliases to builtin types, typing expressions, and user-defined classes are rendered alongside “alias of …”.
  • No regressions to existing autodoc behavior.

@rowan-stein rowan-stein requested a review from a team December 27, 2025 21:48
Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tightening up the alias handling and adding coverage. One blocking issue remains: importing TypeAlias directly from typing breaks on the Python releases we still support (py36–py39). Please guard the import just like you already do for TypeAliasType (try typing, fall back to typing_extensions, otherwise None) so sphinx.ext.autodoc keeps importing on those runtimes.

Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import guard looks good now. Thanks for following up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants