Skip to content

PEP 585 type hints not rendered correctly #9463

@Gobot1234

Description

@Gobot1234

Describe the bug

If you use a PEP 585 generic as an annotation i.e. list[str], autodoc renders the annotation as list rather than list[str], this behaviour differs from using typing.List[str] which renders as expected.

Fixing this is quite simple as far as I can tell,

elif (getattr(annotation, '__module__', None) == 'builtins' and
hasattr(annotation, '__qualname__')):
return annotation.__qualname__
just needs to check if the annotation has __args__ and if it does, return repr(annotation)

How to Reproduce

def foo() -> list[str]:
	...
.. autofunction:: foo

Expected behavior

An annotation of list[str] to be rendered as list[str]

Your project

https://github.com/Gobot1234/sphinx-test

Screenshots

image

OS

Mac

Python version

Python 3.9.3

Sphinx version

4.1.1

Sphinx extensions

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