# Buggy code
def ismock(subject: Any) -> bool:
    """Check if the object is mocked."""
    # check the object has '__sphinx_mock__' attribute
    if not hasattr(subject, "__sphinx_mock__"):
        return False

    # check the object is mocked module
    if isinstance(subject, _MockModule):
        return True

    try:
        # check the object is mocked object
        __mro__ = safe_getattr(type(subject), "__mro__", [])
        if len(__mro__) > 2 and __mro__[1] is _MockObject:
            return True
    except AttributeError:
        pass

    return False


# Traceback (RuntimeError)
Running Sphinx v3.4.2
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 1 source files that are out of date
updating environment: [new config] 1 added, 0 changed, 0 removed
reading sources... [100%] index
Traceback (most recent call last):
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/sphinx/cmd/build.py", line 280, in build_main
app.build(args.force_all, filenames)
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/sphinx/application.py", line 352, in build
self.builder.build_update()
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/sphinx/builders/__init__.py", line 298, in build_update
len(to_build))
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/sphinx/builders/__init__.py", line 310, in build
updated_docnames = set(self.read())
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/sphinx/builders/__init__.py", line 417, in read
self._read_serial(docnames)
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/sphinx/builders/__init__.py", line 438, in _read_serial
self.read_doc(docname)
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/sphinx/builders/__init__.py", line 478, in read_doc
doctree = read_doc(self.app, self.env, self.env.doc2path(docname))
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/sphinx/io.py", line 221, in read_doc
pub.publish()
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/docutils/core.py", line 218, in publish
self.settings)
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/sphinx/io.py", line 126, in read
self.parse()
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/docutils/readers/__init__.py", line 77, in parse
self.parser.parse(self.input, document)
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/sphinx/parsers.py", line 104, in parse
self.statemachine.run(inputlines, document, inliner=self.inliner)
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/docutils/parsers/rst/states.py", line 171, in run
input_source=document['source'])
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/docutils/statemachine.py", line 242, in run
context, state, transitions)
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/docutils/statemachine.py", line 459, in check_line
return method(match, context, next_state)
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/docutils/parsers/rst/states.py", line 2342, in explicit_markup
nodelist, blank_finish = self.explicit_construct(match)
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/docutils/parsers/rst/states.py", line 2354, in explicit_construct
return method(self, expmatch)
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/docutils/parsers/rst/states.py", line 2097, in directive
directive_class, match, type_name, option_presets)
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/docutils/parsers/rst/states.py", line 2146, in run_directive
result = directive_instance.run()
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/sphinx/ext/autodoc/directive.py", line 146, in run
documenter.generate(more_content=self.content)
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/sphinx/ext/autodoc/__init__.py", line 959, in generate
self.document_members(all_members)
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/sphinx/ext/autodoc/__init__.py", line 821, in document_members
for (mname, member, isattr) in self.filter_members(members, want_all):
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/sphinx/ext/autodoc/__init__.py", line 734, in filter_members
if ismock(member):
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/sphinx/ext/autodoc/mock.py", line 156, in ismock
if not hasattr(subject, '__sphinx_mock__'):
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/werkzeug/local.py", line 347, in __getattr__
return getattr(self._get_current_object(), name)
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/werkzeug/local.py", line 306, in _get_current_object
return self.__local()
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/flask/globals.py", line 38, in _lookup_req_object
raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request.  Consult the documentation on testing for
information about how to avoid this problem.

Exception occurred:
File "/home/users/malbert/tmp/sphinx-bug/env/lib/python3.6/site-packages/flask/globals.py", line 38, in _lookup_req_object
raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request.  Consult the documentation on testing for
information about how to avoid this problem.
The full traceback has been saved in /tmp/sphinx-err-jzo0zc61.log, if you want to report the issue to the developers.
Please also report this if it was a user error, so that a better error message can be provided next time.
A bug report can be filed in the tracker at <https://github.com/sphinx-doc/sphinx/issues>. Thanks!

# Fixed code
def ismock(subject: Any) -> bool:
    """Check if the object is mocked."""
    # check the object has '__sphinx_mock__' attribute
    try:
        if safe_getattr(subject, "__sphinx_mock__", None) is None:
            return False
    except AttributeError:
        return False

    # check the object is mocked module
    if isinstance(subject, _MockModule):
        return True

    try:
        # check the object is mocked object
        __mro__ = safe_getattr(type(subject), "__mro__", [])
        if len(__mro__) > 2 and __mro__[1] is _MockObject:
            return True
    except AttributeError:
        pass

    return False