Skip to content

Commit

Permalink
Fix #8084: autodoc: KeyError is raised on documenting a broken attribute
Browse files Browse the repository at this point in the history
``typing.get_type_hints()`` raises KeyError when a class having invalid
__module__ was given.  This handles the exception not to crash on build
documents.
  • Loading branch information
tk0miya committed Aug 9, 2020
1 parent 40bdeb2 commit f7431b9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Expand Up @@ -37,6 +37,9 @@ Features added
Bugs fixed
----------

* #8084: autodoc: KeyError is raised on documenting an attribute of the broken
class

Testing
--------

Expand Down
6 changes: 6 additions & 0 deletions sphinx/ext/autodoc/__init__.py
Expand Up @@ -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]))
Expand Down Expand Up @@ -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]))
Expand Down

0 comments on commit f7431b9

Please sign in to comment.