Skip to content

Commit

Permalink
Fix sphinx-doc#8157: autodoc: TypeError is raised when annotation has…
Browse files Browse the repository at this point in the history
… invalid __args__

Typically, the __args__ attribute of type annotations is a tuple
containing arguments for the types (ex. The __args__ of `List[int]` is
`(int,)`).  But some kind of types has non tuple __args__ attribute.
For example, `nptyping.NDArray` is one of them.

This fixes the TypeError when the invalid __args__ attribute found.
  • Loading branch information
tk0miya committed Oct 2, 2020
1 parent 1ff1f3c commit 674b7f0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -34,6 +34,7 @@ Bugs fixed
by string not ending with blank lines
* #8142: autodoc: Wrong constructor signature for the class derived from
typing.Generic
* #8157: autodoc: TypeError is raised when annotation has invalid __args__
* #8192: napoleon: description is disappeared when it contains inline literals
* #8142: napoleon: Potential of regex denial of service in google style docs
* #8169: LaTeX: pxjahyper loaded even when latex_engine is not platex
Expand Down
6 changes: 6 additions & 0 deletions sphinx/util/inspect.py
Expand Up @@ -9,6 +9,7 @@
"""

import builtins
import collections.abc
import contextlib
import enum
import inspect
Expand Down Expand Up @@ -302,6 +303,11 @@ def iscoroutinefunction(obj: Any) -> bool:
return False


def isiterable(obj: Any) -> bool:
"""Check if the object is iterable."""
return isinstance(obj, collections.abc.Iterable)


def isproperty(obj: Any) -> bool:
"""Check if the object is property."""
if sys.version_info > (3, 8):
Expand Down
7 changes: 6 additions & 1 deletion sphinx/util/typing.py
Expand Up @@ -15,6 +15,8 @@
from docutils import nodes
from docutils.parsers.rst.states import Inliner

from sphinx.util.inspect import isiterable


if sys.version_info > (3, 7):
from typing import ForwardRef
Expand Down Expand Up @@ -105,7 +107,10 @@ def _stringify_py37(annotation: Any) -> str:
return repr(annotation)

if getattr(annotation, '__args__', None):
if qualname == 'Union':
if not isiterable(annotation.__args__):
# broken __args__ found
qualname = repr(annotation)
elif qualname == 'Union':
if len(annotation.__args__) > 1 and annotation.__args__[-1] is NoneType:
if len(annotation.__args__) > 2:
args = ', '.join(stringify(a) for a in annotation.__args__[:-1])
Expand Down

0 comments on commit 674b7f0

Please sign in to comment.