From f7431b927cab199682a44b11849f347f692f6eca Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sun, 9 Aug 2020 23:40:29 +0900 Subject: [PATCH] Fix #8084: autodoc: KeyError is raised on documenting a broken attribute ``typing.get_type_hints()`` raises KeyError when a class having invalid __module__ was given. This handles the exception not to crash on build documents. --- CHANGES | 3 +++ sphinx/ext/autodoc/__init__.py | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/CHANGES b/CHANGES index 5f645efa84..3724c65cab 100644 --- a/CHANGES +++ b/CHANGES @@ -37,6 +37,9 @@ Features added Bugs fixed ---------- +* #8084: autodoc: KeyError is raised on documenting an attribute of the broken + class + Testing -------- diff --git a/sphinx/ext/autodoc/__init__.py b/sphinx/ext/autodoc/__init__.py index 0a64b56e2b..47f6bcb250 100644 --- a/sphinx/ext/autodoc/__init__.py +++ b/sphinx/ext/autodoc/__init__.py @@ -1610,6 +1610,9 @@ def add_directive_header(self, sig: str) -> None: annotations = get_type_hints(self.parent) except TypeError: annotations = {} + except KeyError: + # a broken class found (refs: https://github.com/sphinx-doc/sphinx/issues/8084) + annotations = {} if self.objpath[-1] in annotations: objrepr = stringify_typehint(annotations.get(self.objpath[-1])) @@ -1980,6 +1983,9 @@ def add_directive_header(self, sig: str) -> None: annotations = get_type_hints(self.parent) except TypeError: annotations = {} + except KeyError: + # a broken class found (refs: https://github.com/sphinx-doc/sphinx/issues/8084) + annotations = {} if self.objpath[-1] in annotations: objrepr = stringify_typehint(annotations.get(self.objpath[-1]))