summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2020-10-03 02:01:12 +0900
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2020-10-04 11:43:57 +0900
commit9a0a0f9ae15d717a5503fc3883159ae6c4210254 (patch)
treeaccb654e376c83feec9dba1dfc63efead9e6ae68
parentd1f8dddeff742ca09dca6ab829410248b919a5c5 (diff)
downloadsphinx-git-9a0a0f9ae15d717a5503fc3883159ae6c4210254.tar.gz
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.
-rw-r--r--CHANGES1
-rw-r--r--sphinx/util/typing.py5
-rw-r--r--tests/test_util_typing.py8
3 files changed, 13 insertions, 1 deletions
diff --git a/CHANGES b/CHANGES
index b82c2e948..e30d42e43 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/typing.py b/sphinx/util/typing.py
index 4dac3b695..8eca67220 100644
--- a/sphinx/util/typing.py
+++ b/sphinx/util/typing.py
@@ -109,7 +109,10 @@ def _stringify_py37(annotation: Any) -> str:
return repr(annotation)
if getattr(annotation, '__args__', None):
- if qualname == 'Union':
+ if not isinstance(annotation.__args__, (list, tuple)):
+ # broken __args__ found
+ pass
+ 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])
diff --git a/tests/test_util_typing.py b/tests/test_util_typing.py
index 932fdbfc0..3f8ddbb37 100644
--- a/tests/test_util_typing.py
+++ b/tests/test_util_typing.py
@@ -32,6 +32,10 @@ class MyList(List[T]):
pass
+class BrokenType:
+ __args__ = int
+
+
def test_stringify():
assert stringify(int) == "int"
assert stringify(str) == "str"
@@ -113,3 +117,7 @@ def test_stringify_type_hints_alias():
MyTuple = Tuple[str, str]
assert stringify(MyStr) == "str"
assert stringify(MyTuple) == "Tuple[str, str]" # type: ignore
+
+
+def test_stringify_broken_type_hints():
+ assert stringify(BrokenType) == 'test_util_typing.BrokenType'