Skip to content

Commit

Permalink
Fix #7983: autodoc: Generator type annotation is wrongly rendered in …
Browse files Browse the repository at this point in the history
…py36

This adds a special handler (if-branch) for Generator type to stringify
them correctly.  So far, they have been considered as a kind of Callable.
  • Loading branch information
tk0miya committed Jul 19, 2020
1 parent 5850d6b commit b3b7cbb
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Bugs fixed
* #7901: autodoc: type annotations for overloaded functions are not resolved
* #904: autodoc: An instance attribute cause a crash of autofunction directive
* #1362: autodoc: ``private-members`` option does not work for class attributes
* #7983: autodoc: Generator type annotation is wrongly rendered in py36
* #7839: autosummary: cannot handle umlauts in function names
* #7865: autosummary: Failed to extract summary line when abbreviations found
* #7866: autosummary: Failed to extract correct summary line when docstring
Expand Down
4 changes: 3 additions & 1 deletion sphinx/util/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import sys
import typing
from typing import Any, Callable, Dict, List, Tuple, TypeVar, Union
from typing import Any, Callable, Dict, Generator, List, Tuple, TypeVar, Union

from docutils import nodes
from docutils.parsers.rst.states import Inliner
Expand Down Expand Up @@ -164,6 +164,8 @@ def _stringify_py36(annotation: Any) -> str:
# for Python 3.5.2+
if annotation.__args__ is None or len(annotation.__args__) <= 2: # type: ignore # NOQA
params = annotation.__args__ # type: ignore
elif annotation.__origin__ == Generator: # type: ignore
params = annotation.__args__ # type: ignore
else: # typing.Callable
args = ', '.join(stringify(arg) for arg
in annotation.__args__[:-1]) # type: ignore
Expand Down
5 changes: 4 additions & 1 deletion tests/test_util_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

import sys
from numbers import Integral
from typing import Any, Dict, List, TypeVar, Union, Callable, Tuple, Optional, Generic
from typing import (
Any, Dict, Generator, List, TypeVar, Union, Callable, Tuple, Optional, Generic
)

import pytest

Expand Down Expand Up @@ -48,6 +50,7 @@ def test_stringify_type_hints_containers():
assert stringify(Tuple[str, ...]) == "Tuple[str, ...]"
assert stringify(List[Dict[str, Tuple]]) == "List[Dict[str, Tuple]]"
assert stringify(MyList[Tuple[int, int]]) == "test_util_typing.MyList[Tuple[int, int]]"
assert stringify(Generator[None, None, None]) == "Generator[None, None, None]"


@pytest.mark.skipif(sys.version_info < (3, 9), reason='python 3.9+ is required.')
Expand Down

0 comments on commit b3b7cbb

Please sign in to comment.