Skip to content

Commit

Permalink
Fix sphinx-doc#8559: AttributeError is raised when using ForwardRef
Browse files Browse the repository at this point in the history
The restify() helper crashes when ForwardRef is passed.
  • Loading branch information
tk0miya committed Dec 21, 2020
1 parent 323b136 commit 423e7ab
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Expand Up @@ -16,6 +16,9 @@ Features added
Bugs fixed
----------

* #8559: autodoc: AttributeError is raised when using forward-reference type
annotations

Testing
--------

Expand Down
2 changes: 2 additions & 0 deletions sphinx/util/typing.py
Expand Up @@ -153,6 +153,8 @@ def _restify_py37(cls: Optional["Type"]) -> str:
return ':obj:`%s`' % cls._name
else:
return ':obj:`%s.%s`' % (cls.__module__, cls._name)
elif isinstance(cls, ForwardRef):
return ':class:`%s`' % cls.__forward_arg__
else:
# not a class (ex. TypeVar)
return ':obj:`%s.%s`' % (cls.__module__, cls.__name__)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_util_typing.py
Expand Up @@ -109,6 +109,12 @@ def test_restify_type_hints_alias():
assert restify(MyTuple) == ":class:`Tuple`\\ [:class:`str`, :class:`str`]" # type: ignore


@pytest.mark.skipif(sys.version_info < (3, 7), reason='python 3.7+ is required.')
def test_restify_type_ForwardRef():
from typing import ForwardRef # type: ignore
assert restify(ForwardRef("myint")) == ":class:`myint`"


def test_restify_broken_type_hints():
assert restify(BrokenType) == ':class:`tests.test_util_typing.BrokenType`'

Expand Down

0 comments on commit 423e7ab

Please sign in to comment.