Skip to content

Conversation

@dalcinl
Copy link

@dalcinl dalcinl commented Dec 12, 2025

Purpose

The new implementation of autodoc expects the target of autotype to be a proper type statement as introduced in
Python 3.12. Legacy type aliases are deprecated since 3.12 in favor of type statements. However, they are still supported and according to Python docs, there are no current plans for removal.

Without the first commit in this PR, Python code like the following

from typing import TypeAlias
IntOrStr: TypeAlias = int | str

cannot be documented with the autotype directive:

.. autotype:: IntOrStr

as this will lead to a runtime failure.

Traceback
=========

      File "<rootdir>/sphinx/sphinx/ext/autodoc/_dynamic/_loader.py", line 469, in _make_props_from_imported_object
        obj.__value__, render_mode, short_literals=short_literals
        ^^^^^^^^^^^^^
    AttributeError: 'typing.Union' object has no attribute '__value__'. Did you mean: '__le__'?

@AA-Turner
Copy link
Member

@dalcinl please can you split this into two PRs?

A

Example:

```python
from typing import TypeAlias
IntOrStr: TypeAlias = int | str
```

```rst
.. autotype:: IntOrStr
```
@dalcinl dalcinl changed the title Fix autodoc and enhance autosummary Fix autodoc Dec 14, 2025
@dalcinl
Copy link
Author

dalcinl commented Dec 14, 2025

@AA-Turner Done. This PR is now for the autodoc fix exclusively, which is IMHO the most pressing one. The fix for autosummary is #14174.

@AA-Turner AA-Turner added type:enhancement enhance or introduce a new feature extensions:autodoc labels Dec 14, 2025
@AA-Turner AA-Turner changed the title Fix autodoc autodoc: Add support for typing.TypeAlias declarations Dec 14, 2025
@AA-Turner
Copy link
Member

I think this is a new feature, rather than a bug fix. Recategorised accordingly.

A

@dalcinl
Copy link
Author

dalcinl commented Dec 14, 2025

Can we still get this change in the next patch release 9.0.5?

@AA-Turner
Copy link
Member

The next version of Sphinx will be 9.1, we've closed patch releases for the 9.0 series.

A

@AA-Turner AA-Turner changed the title autodoc: Add support for typing.TypeAlias declarations autodoc: Add support for typing.TypeAlias declarations Dec 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

extensions:autodoc type:enhancement enhance or introduce a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants