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.
This commit is contained in:
Takeshi KOMIYA 2020-08-09 23:40:29 +09:00
parent 40bdeb2c16
commit f7431b927c
2 changed files with 9 additions and 0 deletions

View File

@ -37,6 +37,9 @@ Features added
Bugs fixed
----------
* #8084: autodoc: KeyError is raised on documenting an attribute of the broken
class
Testing
--------

View File

@ -1610,6 +1610,9 @@ class DataDocumenter(ModuleLevelDocumenter):
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 @@ class AttributeDocumenter(DocstringStripSignatureMixin, ClassLevelDocumenter):
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]))