Skip to content

Commit

Permalink
Fix sphinx-doc#7567: autodoc: parametrized types are shown twice for …
Browse files Browse the repository at this point in the history
…generic types
  • Loading branch information
tk0miya committed Apr 27, 2020
1 parent 7a40ed0 commit 16cd886
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -67,6 +67,7 @@ Bugs fixed

* #6703: autodoc: incremental build does not work for imported objects
* #7564: autodoc: annotations not to be shown for descriptors
* #7567: autodoc: parametrized types are shown twice for generic types

Testing
--------
Expand Down
5 changes: 4 additions & 1 deletion sphinx/util/typing.py
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, Generic, List, Tuple, TypeVar, Union

from docutils import nodes
from docutils.parsers.rst.states import Inliner
Expand Down Expand Up @@ -93,6 +93,9 @@ def _stringify_py37(annotation: Any) -> str:
return stringify(annotation.__args__[0])
elif annotation._special:
return qualname
elif issubclass(annotation.__origin__, Generic): # type: ignore
# subclasses of Generic already contains the types for elements in its name
return qualname
else:
args = ', '.join(stringify(a) for a in annotation.__args__)
return '%s[%s]' % (qualname, args)
Expand Down
12 changes: 11 additions & 1 deletion tests/test_util_typing.py
Expand Up @@ -10,7 +10,7 @@

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

import pytest

Expand Down Expand Up @@ -94,6 +94,16 @@ def test_stringify_type_hints_typevars():
assert stringify(List[T]) == "List[T]"


def test_stringify_type_hints_Generic():
T = TypeVar('T')

class Foo(Generic[T]):
pass

expected = "test_util_typing.test_stringify_type_hints_Generic.<locals>.Foo[int]"
assert stringify(Foo[int]) == expected


def test_stringify_type_hints_custom_class():
assert stringify(MyClass1) == "test_util_typing.MyClass1"
assert stringify(MyClass2) == "test_util_typing.<MyClass2>"
Expand Down

0 comments on commit 16cd886

Please sign in to comment.