Skip to content

Commit

Permalink
Merge pull request #9464 from Gobot1234/4.x
Browse files Browse the repository at this point in the history
Add support for PEP 585 generics
  • Loading branch information
tk0miya committed Jul 19, 2021
2 parents 362f36d + 451811c commit f9941b9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion sphinx/util/typing.py
Expand Up @@ -310,7 +310,10 @@ def stringify(annotation: Any) -> str:
return INVALID_BUILTIN_CLASSES[annotation]
elif (getattr(annotation, '__module__', None) == 'builtins' and
hasattr(annotation, '__qualname__')):
return annotation.__qualname__
if hasattr(annotation, '__args__'): # PEP 585 generic
return repr(annotation)
else:
return annotation.__qualname__
elif annotation is Ellipsis:
return '...'

Expand Down
12 changes: 12 additions & 0 deletions tests/test_util_typing.py
Expand Up @@ -175,6 +175,18 @@ def test_stringify_type_hints_containers():
assert stringify(Generator[None, None, None]) == "Generator[None, None, None]"


@pytest.mark.skipif(sys.version_info < (3, 9), reason='python 3.9+ is required.')
def test_stringify_type_hints_pep_585():
assert stringify(list[int]) == "list[int]"
assert stringify(list[str]) == "list[str]"
assert stringify(dict[str, float]) == "dict[str, float]"
assert stringify(tuple[str, str, str]) == "tuple[str, str, str]"
assert stringify(tuple[str, ...]) == "tuple[str, ...]"
assert stringify(tuple[()]) == "tuple[()]"
assert stringify(list[dict[str, tuple]]) == "list[dict[str, tuple]]"
assert stringify(type[int]) == "type[int]"


@pytest.mark.skipif(sys.version_info < (3, 9), reason='python 3.9+ is required.')
def test_stringify_Annotated():
from typing import Annotated # type: ignore
Expand Down

0 comments on commit f9941b9

Please sign in to comment.