From ec1a563cee95435aad6758b71a0f4eb297ef2993 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sat, 3 Oct 2020 02:01:12 +0900 Subject: [PATCH] Fix #8157: autodoc: TypeError is raised when annotation has 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. --- CHANGES | 1 + sphinx/util/inspect.py | 6 ++++++ sphinx/util/typing.py | 7 ++++++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index 22876d43b1c..126dd2fffd3 100644 --- a/CHANGES +++ b/CHANGES @@ -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 diff --git a/sphinx/util/inspect.py b/sphinx/util/inspect.py index 37997e6b275..228efd98106 100644 --- a/sphinx/util/inspect.py +++ b/sphinx/util/inspect.py @@ -9,6 +9,7 @@ """ import builtins +import collections.abc import contextlib import enum import inspect @@ -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): diff --git a/sphinx/util/typing.py b/sphinx/util/typing.py index d71ca1b2d66..4f11a381839 100644 --- a/sphinx/util/typing.py +++ b/sphinx/util/typing.py @@ -105,7 +105,12 @@ def _stringify_py37(annotation: Any) -> str: return repr(annotation) if getattr(annotation, '__args__', None): - if qualname == 'Union': + from sphinx.util.inspect import isiterable + + 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])