Merge pull request #5535 from tk0miya/5480_unresolvable_forwardref

Fix #5480: autodoc: unable to find type hints for unresolvable Forward references
This commit is contained in:
Takeshi KOMIYA 2018-10-17 01:59:06 +09:00 committed by GitHub
commit 65978c92f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 14 deletions

View File

@ -24,6 +24,7 @@ Bugs fixed
* #5495: csv-table directive with file option in included file is broken (refs: * #5495: csv-table directive with file option in included file is broken (refs:
#4821) #4821)
* #5498: autodoc: unable to find type hints for a ``functools.partial`` * #5498: autodoc: unable to find type hints for a ``functools.partial``
* #5480: autodoc: unable to find type hints for unresolvable Forward references
Testing Testing
-------- --------

View File

@ -358,19 +358,12 @@ class Signature(object):
self.argspec = getargspec(subject) self.argspec = getargspec(subject)
try: try:
if ispartial(subject): self.annotations = typing.get_type_hints(subject) # type: ignore
# get_type_hints() does not support partial objects except Exception:
self.annotations = {} # type: Dict[str, Any] # get_type_hints() does not support some kind of objects like partial,
else: # ForwardRef and so on. For them, it raises an exception. In that case,
self.annotations = typing.get_type_hints(subject) # type: ignore # we try to build annotations from argspec.
except Exception as exc: self.annotations = {}
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 = {}
if bound_method: if bound_method:
# client gives a hint that the subject is a bound method # client gives a hint that the subject is a bound method

View File

@ -232,7 +232,7 @@ def test_Signature_partialmethod():
reason='type annotation test is available on py34 or above') reason='type annotation test is available on py34 or above')
def test_Signature_annotations(): def test_Signature_annotations():
from typing_test_data import ( 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 # Class annotations
sig = inspect.Signature(f0).format_args() sig = inspect.Signature(f0).format_args()
@ -297,6 +297,10 @@ def test_Signature_annotations():
sig = inspect.Signature(f14).format_args() sig = inspect.Signature(f14).format_args()
assert sig == '() -> Any' assert sig == '() -> Any'
# ForwardRef
sig = inspect.Signature(f15).format_args()
assert sig == '(x: Unknown, y: int) -> Any'
# type hints by string # type hints by string
sig = inspect.Signature(Node.children).format_args() sig = inspect.Signature(Node.children).format_args()
if (3, 5, 0) <= sys.version_info < (3, 5, 3): if (3, 5, 0) <= sys.version_info < (3, 5, 3):

View File

@ -76,6 +76,10 @@ def f14() -> Any:
pass pass
def f15(x: "Unknown", y: "int") -> Any:
pass
class Node: class Node:
def __init__(self, parent: Optional['Node']) -> None: def __init__(self, parent: Optional['Node']) -> None:
pass pass