Skip to content

Commit

Permalink
Merge pull request #9522 from Gobot1234/4.x
Browse files Browse the repository at this point in the history
Recursively resolve PEP 585 builtins in restify
  • Loading branch information
tk0miya committed Aug 4, 2021
2 parents d788e78 + 596ad55 commit 6ac326e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion sphinx/util/typing.py
Expand Up @@ -124,7 +124,13 @@ def restify(cls: Optional[Type]) -> str:
else:
return ' | '.join(restify(a) for a in cls.__args__)
elif cls.__module__ in ('__builtin__', 'builtins'):
return ':class:`%s`' % cls.__name__
if hasattr(cls, '__args__'):
return ':class:`%s`\\ [%s]' % (
cls.__name__,
', '.join(restify(arg) for arg in cls.__args__),
)
else:
return ':class:`%s`' % cls.__name__
else:
if sys.version_info >= (3, 7): # py37+
return _restify_py37(cls)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_util_typing.py
Expand Up @@ -140,6 +140,14 @@ def test_restify_type_Literal():
assert restify(Literal[1, "2", "\r"]) == ":obj:`~typing.Literal`\\ [1, '2', '\\r']"


@pytest.mark.skipif(sys.version_info < (3, 9), reason='python 3.9+ is required.')
def test_restify_pep_585():
assert restify(list[str]) == ":class:`list`\\ [:class:`str`]" # type: ignore
assert restify(dict[str, str]) == ":class:`dict`\\ [:class:`str`, :class:`str`]" # type: ignore
assert restify(dict[str, tuple[int, ...]]) == \
":class:`dict`\\ [:class:`str`, :class:`tuple`\\ [:class:`int`, ...]]" # type: ignore


@pytest.mark.skipif(sys.version_info < (3, 10), reason='python 3.10+ is required.')
def test_restify_type_union_operator():
assert restify(int | None) == ":class:`int` | :obj:`None`" # type: ignore
Expand Down

0 comments on commit 6ac326e

Please sign in to comment.