Merge pull request #9522 from Gobot1234/4.x

Recursively resolve PEP 585 builtins in restify
This commit is contained in:
Takeshi KOMIYA 2021-08-05 01:52:52 +09:00 committed by GitHub
commit 6ac326e019
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -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)

View File

@ -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