Skip to content

Commit 6ac326e

Browse files
authored
Merge pull request #9522 from Gobot1234/4.x
Recursively resolve PEP 585 builtins in restify
2 parents d788e78 + 596ad55 commit 6ac326e

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

sphinx/util/typing.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,13 @@ def restify(cls: Optional[Type]) -> str:
124124
else:
125125
return ' | '.join(restify(a) for a in cls.__args__)
126126
elif cls.__module__ in ('__builtin__', 'builtins'):
127-
return ':class:`%s`' % cls.__name__
127+
if hasattr(cls, '__args__'):
128+
return ':class:`%s`\\ [%s]' % (
129+
cls.__name__,
130+
', '.join(restify(arg) for arg in cls.__args__),
131+
)
132+
else:
133+
return ':class:`%s`' % cls.__name__
128134
else:
129135
if sys.version_info >= (3, 7): # py37+
130136
return _restify_py37(cls)

tests/test_util_typing.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ def test_restify_type_Literal():
140140
assert restify(Literal[1, "2", "\r"]) == ":obj:`~typing.Literal`\\ [1, '2', '\\r']"
141141

142142

143+
@pytest.mark.skipif(sys.version_info < (3, 9), reason='python 3.9+ is required.')
144+
def test_restify_pep_585():
145+
assert restify(list[str]) == ":class:`list`\\ [:class:`str`]" # type: ignore
146+
assert restify(dict[str, str]) == ":class:`dict`\\ [:class:`str`, :class:`str`]" # type: ignore
147+
assert restify(dict[str, tuple[int, ...]]) == \
148+
":class:`dict`\\ [:class:`str`, :class:`tuple`\\ [:class:`int`, ...]]" # type: ignore
149+
150+
143151
@pytest.mark.skipif(sys.version_info < (3, 10), reason='python 3.10+ is required.')
144152
def test_restify_type_union_operator():
145153
assert restify(int | None) == ":class:`int` | :obj:`None`" # type: ignore

0 commit comments

Comments
 (0)