Fix #5322: autodoc: `Any` typehint causes formatting error

This commit is contained in:
Takeshi KOMIYA 2018-08-21 20:05:17 +09:00
parent 2371614227
commit 59a766d7d6
4 changed files with 15 additions and 3 deletions

View File

@ -16,6 +16,8 @@ Features added
Bugs fixed
----------
* #5322: autodoc: ``Any`` typehint causes formatting error
Testing
--------

View File

@ -531,8 +531,10 @@ class Signature(object):
qualname = annotation.__qualname__
elif getattr(annotation, '__forward_arg__', None):
qualname = annotation.__forward_arg__
else:
elif getattr(annotation, '__origin__', None):
qualname = self.format_annotation(annotation.__origin__) # ex. Union
else:
qualname = repr(annotation).replace('typing.', '')
elif hasattr(annotation, '__qualname__'):
qualname = '%s.%s' % (module, annotation.__qualname__)
else:

View File

@ -232,7 +232,7 @@ def test_Signature_partialmethod():
reason='type annotation test is available on py34 or above')
def test_Signature_annotations():
from typing_test_data import (
f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, Node)
f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14, Node)
# Class annotations
sig = inspect.Signature(f0).format_args()
@ -293,6 +293,10 @@ def test_Signature_annotations():
sig = inspect.Signature(f13).format_args()
assert sig == '() -> Optional[str]'
# Any
sig = inspect.Signature(f14).format_args()
assert sig == '() -> Any'
# type hints by string
sig = inspect.Signature(Node.children).format_args()
assert sig == '(self) -> List[typing_test_data.Node]'

View File

@ -1,5 +1,5 @@
from numbers import Integral
from typing import List, TypeVar, Union, Callable, Tuple, Optional
from typing import Any, List, TypeVar, Union, Callable, Tuple, Optional
def f0(x: int, y: Integral) -> None:
@ -72,6 +72,10 @@ def f13() -> Optional[str]:
pass
def f14() -> Any:
pass
class Node:
def __init__(self, parent: Optional['Node']) -> None:
pass