Skip to content

Commit

Permalink
Merge pull request #5280 from terencehonles/prefer-typing.get_type_hints
Browse files Browse the repository at this point in the history
change format_args to prefer annotations from typing.get_type_hints
  • Loading branch information
tk0miya committed Aug 13, 2018
2 parents 8a23ad1 + da45604 commit 3e82d09
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
9 changes: 7 additions & 2 deletions sphinx/util/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from six.moves import builtins

from sphinx.util import force_decode
from sphinx.util.pycompat import NoneType

if False:
# For type annotation
Expand Down Expand Up @@ -433,8 +434,7 @@ def format_args(self):
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)
Expand Down Expand Up @@ -465,6 +465,8 @@ def format_annotation_new(self, annotation):
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:
Expand Down Expand Up @@ -519,6 +521,9 @@ def format_annotation_old(self, annotation):
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
Expand Down
6 changes: 5 additions & 1 deletion tests/test_util_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ def meth2(self, arg1, arg2):
@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()
Expand Down Expand Up @@ -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):
Expand Down
5 changes: 5 additions & 0 deletions tests/typing_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3e82d09

Please sign in to comment.