Skip to content

Commit

Permalink
Merge pull request #5535 from tk0miya/5480_unresolvable_forwardref
Browse files Browse the repository at this point in the history
Fix #5480: autodoc: unable to find type hints for unresolvable Forward references
  • Loading branch information
tk0miya committed Oct 16, 2018
2 parents be2b86c + 793792f commit 65978c9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -24,6 +24,7 @@ Bugs fixed
* #5495: csv-table directive with file option in included file is broken (refs:
#4821)
* #5498: autodoc: unable to find type hints for a ``functools.partial``
* #5480: autodoc: unable to find type hints for unresolvable Forward references

Testing
--------
Expand Down
19 changes: 6 additions & 13 deletions sphinx/util/inspect.py
Expand Up @@ -358,19 +358,12 @@ def __init__(self, subject, bound_method=False, has_retval=True):
self.argspec = getargspec(subject)

try:
if ispartial(subject):
# get_type_hints() does not support partial objects
self.annotations = {} # type: Dict[str, Any]
else:
self.annotations = typing.get_type_hints(subject) # type: ignore
except Exception as exc:
if (3, 5, 0) <= sys.version_info < (3, 5, 3) and isinstance(exc, AttributeError):
# python 3.5.2 raises ValueError for classmethod-ized partial objects.
self.annotations = {}
else:
logger.warning('Invalid type annotation found on %r. Ignored: %r',
subject, exc)
self.annotations = {}
self.annotations = typing.get_type_hints(subject) # type: ignore
except Exception:
# get_type_hints() does not support some kind of objects like partial,
# ForwardRef and so on. For them, it raises an exception. In that case,
# we try to build annotations from argspec.
self.annotations = {}

if bound_method:
# client gives a hint that the subject is a bound method
Expand Down
6 changes: 5 additions & 1 deletion tests/test_util_inspect.py
Expand Up @@ -232,7 +232,7 @@ def meth2(self, arg1, arg2):
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, f14, Node)
f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14, f15, Node)

# Class annotations
sig = inspect.Signature(f0).format_args()
Expand Down Expand Up @@ -297,6 +297,10 @@ def test_Signature_annotations():
sig = inspect.Signature(f14).format_args()
assert sig == '() -> Any'

# ForwardRef
sig = inspect.Signature(f15).format_args()
assert sig == '(x: Unknown, y: int) -> Any'

# type hints by string
sig = inspect.Signature(Node.children).format_args()
if (3, 5, 0) <= sys.version_info < (3, 5, 3):
Expand Down
4 changes: 4 additions & 0 deletions tests/typing_test_data.py
Expand Up @@ -76,6 +76,10 @@ def f14() -> Any:
pass


def f15(x: "Unknown", y: "int") -> Any:
pass


class Node:
def __init__(self, parent: Optional['Node']) -> None:
pass
Expand Down

0 comments on commit 65978c9

Please sign in to comment.