Merge pull request #5280 from terencehonles/prefer-typing.get_type_hints

change format_args to prefer annotations from typing.get_type_hints
This commit is contained in:
Takeshi KOMIYA
2018-08-13 21:20:09 +09:00
committed by GitHub
3 changed files with 17 additions and 3 deletions

View File

@@ -20,6 +20,7 @@ from six import PY2, PY3, StringIO, binary_type, string_types, itervalues
from six.moves import builtins
from sphinx.util import force_decode
from sphinx.util.pycompat import NoneType
if False:
# For type annotation
@@ -433,8 +434,7 @@ class Signature(object):
if PY2 or self.return_annotation is inspect.Parameter.empty:
return '(%s)' % ', '.join(args)
else:
if isinstance(self.return_annotation, string_types) and \
'return' in self.annotations:
if 'return' in self.annotations:
annotation = self.format_annotation(self.annotations['return'])
else:
annotation = self.format_annotation(self.return_annotation)
@@ -465,6 +465,8 @@ class Signature(object):
return annotation.__name__
elif not annotation:
return repr(annotation)
elif annotation is NoneType: # type: ignore
return 'None'
elif module == 'builtins':
return annotation.__qualname__
elif annotation is Ellipsis:
@@ -519,6 +521,9 @@ class Signature(object):
else:
qualified_name = (annotation.__module__ + '.' + annotation.__qualname__) # type: ignore # NOQA
if annotation is NoneType: # type: ignore
return 'None'
if annotation.__module__ == 'builtins':
return annotation.__qualname__ # type: ignore
elif (hasattr(typing, 'TupleMeta') and

View File

@@ -231,7 +231,8 @@ def test_Signature_partialmethod():
@pytest.mark.skipif(sys.version_info < (3, 5),
reason='type annotation test is available on py35 or above')
def test_Signature_annotations():
from typing_test_data import f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12
from typing_test_data import (
f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, Node)
# Class annotations
sig = inspect.Signature(f0).format_args()
@@ -288,6 +289,9 @@ def test_Signature_annotations():
sig = inspect.Signature(f12).format_args()
assert sig == '() -> Tuple[int, str, int]'
sig = inspect.Signature(Node.children).format_args()
assert sig == '(self) -> List[typing_test_data.Node]'
def test_safe_getattr_with_default():
class Foo(object):

View File

@@ -66,3 +66,8 @@ def f11(x: CustomAnnotation(), y: 123) -> None:
def f12() -> Tuple[int, str, int]:
pass
class Node:
def children(self) -> List['Node']:
pass