Skip to content

sphinx.ext.autodoc does not render type annotations with parameterized builtin types correctly #9624

@smite

Description

@smite

Describe the bug

When sphinx.ext.autodoc encounters a type annotation with a parameterized builtin type, e.g. list[int], it does not render the parameterization at all.

The generic parameterized types from typing (e.g. List[int])work as expected, and I would expect to get the same result using the corresponding builtin types from Python 3.9 onwards, as per https://www.python.org/dev/peps/pep-0585/.

How to Reproduce

Assume a Python project with the following structure:

├── docs
│   ├── conf.py
│   └── index.rst
└── src
    └── xxx.py

conf.py

import sys

sys.path.insert(0, 'src')
needs_sphinx = '4.1'
extensions = ['sphinx.ext.autodoc']

index.rst

.. autofunction:: xxx.func
.. autofunction:: xxx.Func

xxx.py

from typing import Dict, Set, Type

class A: pass

def func(a: set[type[A]]) -> dict[type[A], int]:
    """test function."""
    pass

def Func(a: Set[Type[A]]) -> Dict[Type[A], int]:
    """Test function."""
    pass

When I run sphinx-build docs output it builds me an output/index.html which, rendered, looks like

xxx.func(a: set) → dict
    test function.

xxx.Func(a: Set[Type[xxx.A]]) → Dict[Type[xxx.A], int]
    Test function.

Expected behavior

What I expect to get:

xxx.func(a: set[type[xxx.A]]) → dict[type[xxx.A], int]
    test function.

xxx.Func(a: Set[Type[xxx.A]]) → Dict[Type[xxx.A], int]
    Test function.

Your project

How can I attach a file?

Screenshots

No response

OS

Ubuntu 20.04

Python version

3.9.6

Sphinx version

4.1.2

Sphinx extensions

sphinx.ext.autodoc

Extra tools

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions