# Buggy code
def add_directive_header(self, sig: str) -> None:
    super().add_directive_header(sig)
    sourcename = self.get_sourcename()
    if not self.options.annotation:
        # obtain type annotation for this attribute
        try:
            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]))
            self.add_line("   :type: " + objrepr, sourcename)
        else:
            key = (".".join(self.objpath[:-1]), self.objpath[-1])
            if self.analyzer and key in self.analyzer.annotations:
                self.add_line("   :type: " + self.analyzer.annotations[key], sourcename)

        # data descriptors do not have useful values
        if not self._datadescriptor:
            try:
                if self.object is INSTANCEATTR:
                    pass
                else:
                    objrepr = object_description(self.object)
                    self.add_line("   :value: " + objrepr, sourcename)
            except ValueError:
                pass
    elif self.options.annotation is SUPPRESS:
        pass
    else:
        self.add_line("   :annotation: %s" % self.options.annotation, sourcename)


# Traceback (AttributeError)
# Sphinx version: 3.2.0
# Python version: 3.5.2 (CPython)
# Docutils version: 0.16 release
# Jinja2 version: 2.11.2
# Last messages:
#   reading sources... [ 14%] api/foobar.cli
#
#   reading sources... [ 18%] api/foobar.cli.retry_failed_orders
#
#   reading sources... [ 22%] api/foobar.config
#
#   reading sources... [ 25%] api/foobar.const
#
#   reading sources... [ 29%] api/foobar.exc
#
# Loaded extensions:
#   sphinxcontrib.devhelp (1.0.2) from /home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinxcontrib/devhelp/__init__.py
#   sphinx.ext.autodoc.type_comment (3.2.0) from /home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinx/ext/autodoc/type_comment.py
#   sphinx.ext.autodoc (3.2.0) from /home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinx/ext/autodoc/__init__.py
#   sphinxcontrib.qthelp (1.0.3) from /home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinxcontrib/qthelp/__init__.py
#   sphinx.ext.extlinks (3.2.0) from /home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinx/ext/extlinks.py
#   sphinxcontrib.serializinghtml (1.1.4) from /home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinxcontrib/serializinghtml/__init__.py
#   alabaster (0.7.12) from /home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/alabaster/__init__.py
#   sphinx.ext.mathjax (3.2.0) from /home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinx/ext/mathjax.py
#   sphinxcontrib.applehelp (1.0.2) from /home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinxcontrib/applehelp/__init__.py
#   sphinxcontrib.htmlhelp (1.0.3) from /home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinxcontrib/htmlhelp/__init__.py
Traceback (most recent call last):
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinx/cmd/build.py", line 280, in build_main
app.build(args.force_all, filenames)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinx/application.py", line 348, in build
self.builder.build_update()
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinx/builders/__init__.py", line 299, in build_update
len(to_build))
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinx/builders/__init__.py", line 311, in build
updated_docnames = set(self.read())
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinx/builders/__init__.py", line 418, in read
self._read_serial(docnames)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinx/builders/__init__.py", line 439, in _read_serial
self.read_doc(docname)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinx/builders/__init__.py", line 479, in read_doc
doctree = read_doc(self.app, self.env, self.env.doc2path(docname))
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinx/io.py", line 223, in read_doc
pub.publish()
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/docutils/core.py", line 218, in publish
self.settings)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinx/io.py", line 128, in read
self.parse()
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/docutils/readers/__init__.py", line 77, in parse
self.parser.parse(self.input, document)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinx/parsers.py", line 102, in parse
self.statemachine.run(inputlines, document, inliner=self.inliner)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/docutils/parsers/rst/states.py", line 171, in run
input_source=document['source'])
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/docutils/statemachine.py", line 242, in run
context, state, transitions)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/docutils/statemachine.py", line 459, in check_line
return method(match, context, next_state)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/docutils/parsers/rst/states.py", line 2769, in underline
self.section(title, source, style, lineno - 1, messages)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/docutils/parsers/rst/states.py", line 327, in section
self.new_subsection(title, lineno, messages)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/docutils/parsers/rst/states.py", line 395, in new_subsection
node=section_node, match_titles=True)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/docutils/parsers/rst/states.py", line 282, in nested_parse
node=node, match_titles=match_titles)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/docutils/parsers/rst/states.py", line 196, in run
results = StateMachineWS.run(self, input_lines, input_offset)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/docutils/statemachine.py", line 242, in run
context, state, transitions)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/docutils/statemachine.py", line 459, in check_line
return method(match, context, next_state)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/docutils/parsers/rst/states.py", line 2342, in explicit_markup
nodelist, blank_finish = self.explicit_construct(match)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/docutils/parsers/rst/states.py", line 2354, in explicit_construct
return method(self, expmatch)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/docutils/parsers/rst/states.py", line 2097, in directive
directive_class, match, type_name, option_presets)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/docutils/parsers/rst/states.py", line 2146, in run_directive
result = directive_instance.run()
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinx/ext/autodoc/directive.py", line 146, in run
documenter.generate(more_content=self.content)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinx/ext/autodoc/__init__.py", line 894, in generate
self.document_members(all_members)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinx/ext/autodoc/__init__.py", line 777, in document_members
check_module=members_check_module and not isattr)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinx/ext/autodoc/__init__.py", line 1570, in generate
all_members=all_members)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinx/ext/autodoc/__init__.py", line 894, in generate
self.document_members(all_members)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinx/ext/autodoc/__init__.py", line 1559, in document_members
super().document_members(all_members)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinx/ext/autodoc/__init__.py", line 777, in document_members
check_module=members_check_module and not isattr)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinx/ext/autodoc/__init__.py", line 884, in generate
self.add_directive_header(sig)
File "/home/users/frank/workspace/foobar/env/lib/python3.5/site-packages/sphinx/ext/autodoc/__init__.py", line 1980, in add_directive_header
annotations = get_type_hints(self.parent)
File "/usr/lib/python3.5/typing.py", line 1177, in get_type_hints
defaults = _get_defaults(obj)
File "/usr/lib/python3.5/typing.py", line 1134, in _get_defaults
code = func.__code__
AttributeError: type object 'InvalidCastOption' has no attribute '__code__'

# Fixed code
def add_directive_header(self, sig: str) -> None:
    super().add_directive_header(sig)
    sourcename = self.get_sourcename()
    if not self.options.annotation:
        # obtain type annotation for this attribute
        try:
            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 = {}
        except AttributeError:
            # AttributeError is raised on 3.5.2 (fixed by 3.5.3)
            annotations = {}

        if self.objpath[-1] in annotations:
            objrepr = stringify_typehint(annotations.get(self.objpath[-1]))
            self.add_line("   :type: " + objrepr, sourcename)
        else:
            key = (".".join(self.objpath[:-1]), self.objpath[-1])
            if self.analyzer and key in self.analyzer.annotations:
                self.add_line("   :type: " + self.analyzer.annotations[key], sourcename)

        # data descriptors do not have useful values
        if not self._datadescriptor:
            try:
                if self.object is INSTANCEATTR:
                    pass
                else:
                    objrepr = object_description(self.object)
                    self.add_line("   :value: " + objrepr, sourcename)
            except ValueError:
                pass
    elif self.options.annotation is SUPPRESS:
        pass
    else:
        self.add_line("   :annotation: %s" % self.options.annotation, sourcename)