Skip to content
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Features added

.. _`<search>`: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/search

* #11773: Added metadata information to rendering of `~typing.Annotated` types.

Bugs fixed
----------

Expand Down
3 changes: 2 additions & 1 deletion sphinx/util/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ def format_literal_arg(arg):
args = ', '.join(map(format_literal_arg, annotation_args))
return f'{module_prefix}Literal[{args}]'
elif str(annotation).startswith('typing.Annotated'): # for py39+
return stringify_annotation(annotation_args[0], mode)
meta = [stringify_annotation(meta, mode) for meta in annotation.__metadata__]
return stringify_annotation(annotation_args[0], mode) + f"[{', '.join(meta)}]"
elif all(is_system_TypeVar(a) for a in annotation_args):
# Suppress arguments if all system defined TypeVars (ex. Dict[KT, VT])
return module_prefix + qualname
Expand Down
4 changes: 2 additions & 2 deletions tests/test_util_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,8 @@ def test_stringify_type_hints_pep_585():

def test_stringify_Annotated():
from typing import Annotated # type: ignore[attr-defined]
assert stringify_annotation(Annotated[str, "foo", "bar"], 'fully-qualified-except-typing') == "str"
assert stringify_annotation(Annotated[str, "foo", "bar"], "smart") == "str"
assert stringify_annotation(Annotated[str, "foo", "bar"], 'fully-qualified-except-typing') == "str[foo, bar]"
assert stringify_annotation(Annotated[str, "foo", "bar"], "smart") == "str[foo, bar]"


def test_stringify_type_hints_string():
Expand Down