Skip to content

Commit

Permalink
Fix sphinx-doc#5291: autodoc crashed by ForwardRef types
Browse files Browse the repository at this point in the history
  • Loading branch information
tk0miya committed Aug 15, 2018
1 parent 8d0170e commit 91650d8
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Bugs fixed
font with XeLaTeX/LuaLateX (refs: #5251)
* #5280: autodoc: Fix wrong type annotations for complex typing
* autodoc: Optional types are wrongly rendered
* #5291: autodoc crashed by ForwardRef types


Testing
--------
Expand Down
4 changes: 4 additions & 0 deletions sphinx/util/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,8 @@ def format_annotation_new(self, annotation):
qualname = annotation._name
elif getattr(annotation, '__qualname__', None):
qualname = annotation.__qualname__
elif getattr(annotation, '__forward_arg__', None):
qualname = annotation.__forward_arg__
else:
qualname = self.format_annotation(annotation.__origin__) # ex. Union
elif hasattr(annotation, '__qualname__'):
Expand Down Expand Up @@ -510,6 +512,8 @@ def format_annotation_old(self, annotation):
qualname = annotation._name
elif getattr(annotation, '__qualname__', None):
qualname = annotation.__qualname__
elif getattr(annotation, '__forward_arg__', None):
qualname = annotation.__forward_arg__
else:
qualname = self.format_annotation(annotation.__origin__) # ex. Union
elif hasattr(annotation, '__qualname__'):
Expand Down
3 changes: 3 additions & 0 deletions tests/test_util_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ def test_Signature_annotations():
sig = inspect.Signature(Node.children).format_args()
assert sig == '(self) -> List[typing_test_data.Node]'

sig = inspect.Signature(Node.__init__).format_args()
assert sig == '(self, parent: Optional[Node]) -> None'


def test_safe_getattr_with_default():
class Foo(object):
Expand Down
3 changes: 3 additions & 0 deletions tests/typing_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,8 @@ def f13() -> Optional[str]:


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

def children(self) -> List['Node']:
pass

0 comments on commit 91650d8

Please sign in to comment.