Skip to content

Commit

Permalink
Do not emit type arguments twice
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-wieser committed Apr 27, 2020
1 parent 21ca437 commit 4637272
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
9 changes: 7 additions & 2 deletions sphinx/util/typing.py
Expand Up @@ -75,8 +75,13 @@ def _stringify_py37(annotation: Any) -> str:
qualname = stringify(annotation.__origin__) # ex. Union
elif hasattr(annotation, '__qualname__'):
qualname = '%s.%s' % (module, annotation.__qualname__)
elif hasattr(annotation, '__origin__'):
# instantiated generic provided by a user
qualname = stringify(annotation.__origin__)
else:
qualname = repr(annotation)
# we weren't able to extract the base type, appending arguments would
# only make them appear twice
return repr(annotation)

if getattr(annotation, '__args__', None):
if qualname == 'Union':
Expand All @@ -91,7 +96,7 @@ def _stringify_py37(annotation: Any) -> str:
return '%s[[%s], %s]' % (qualname, args, returns)
elif str(annotation).startswith('typing.Annotated'): # for py39+
return stringify(annotation.__args__[0])
elif annotation._special:
elif getattr(annotation, '_special', False):
return qualname
else:
args = ', '.join(stringify(a) for a in annotation.__args__)
Expand Down
8 changes: 7 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, List, TypeVar, Union, Callable, Tuple, Optional, Generic

import pytest

Expand All @@ -24,6 +24,11 @@ class MyClass1:
class MyClass2(MyClass1):
__qualname__ = '<MyClass2>'

T = TypeVar('T')

class MyList(List[T]):
pass


def test_stringify():
assert stringify(int) == "int"
Expand All @@ -42,6 +47,7 @@ def test_stringify_type_hints_containers():
assert stringify(Tuple[str, str, str]) == "Tuple[str, str, str]"
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]]"


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

0 comments on commit 4637272

Please sign in to comment.