# Buggy code
def import_object(
    modname, objpath, objtype="", attrgetter=safe_getattr, warningiserror=False
):
    # type: (str, List[unicode], str, Callable[[Any, unicode], Any], bool) -> Any
    if objpath:
        logger.debug("[autodoc] from %s import %s", modname, ".".join(objpath))
    else:
        logger.debug("[autodoc] import %s", modname)

    try:
        module = import_module(modname, warningiserror=warningiserror)
        logger.debug("[autodoc] => %r", module)
        obj = module
        parent = None
        object_name = None
        for attrname in objpath:
            parent = obj
            logger.debug("[autodoc] getattr(_, %r)", attrname)
            obj = attrgetter(obj, attrname)
            logger.debug("[autodoc] => %r", obj)
            object_name = attrname
        return [module, parent, object_name, obj]
    except (AttributeError, ImportError) as exc:
        if objpath:
            errmsg = "autodoc: failed to import %s %r from module %r" % (
                objtype,
                ".".join(objpath),
                modname,
            )
        else:
            errmsg = "autodoc: failed to import %s %r" % (objtype, modname)

        if isinstance(exc, ImportError):
            # import_module() raises ImportError having real exception obj and
            # traceback
            real_exc, traceback_msg = exc.args
            if isinstance(real_exc, SystemExit):
                errmsg += (
                    "; the module executes module level statement "
                    "and it might call sys.exit()."
                )
            elif isinstance(real_exc, ImportError) and real_exc.args:
                errmsg += "; the following exception was raised:\n%s" % real_exc.args[0]
            else:
                errmsg += "; the following exception was raised:\n%s" % traceback_msg
        else:
            errmsg += (
                "; the following exception was raised:\n%s" % traceback.format_exc()
            )

        if PY2:
            errmsg = errmsg.decode("utf-8")  # type: ignore
        logger.debug(errmsg)
        raise ImportError(errmsg)


# Traceback (ImportError)
WARNING: autodoc: failed to import class 'NestedClass' from module 'module.SomeClass'; the following exception was raised:
Traceback (most recent call last):
File "/Users/brechtm/.pyenv/versions/rinoh-py35/lib/python3.5/site-packages/sphinx/ext/autodoc.py", line 518, in import_object
__import__(self.modname)
ImportError: No module named 'module.SomeClass'

# Fixed code
def import_object(
    modname, objpath, objtype="", attrgetter=safe_getattr, warningiserror=False
):
    # type: (str, List[unicode], str, Callable[[Any, unicode], Any], bool) -> Any
    if objpath:
        logger.debug("[autodoc] from %s import %s", modname, ".".join(objpath))
    else:
        logger.debug("[autodoc] import %s", modname)

    try:
        module = None
        objpath = list(objpath)
        while module is None:
            try:
                module = import_module(modname, warningiserror=warningiserror)
                logger.debug("[autodoc] import %s => %r", modname, module)
            except ImportError:
                logger.debug("[autodoc] import %s => failed", modname)
                if "." in modname:
                    # retry with parent module
                    modname, name = modname.rsplit(".", 1)
                    objpath.insert(0, name)
                else:
                    raise

        obj = module
        parent = None
        object_name = None
        for attrname in objpath:
            parent = obj
            logger.debug("[autodoc] getattr(_, %r)", attrname)
            obj = attrgetter(obj, attrname)
            logger.debug("[autodoc] => %r", obj)
            object_name = attrname
        return [module, parent, object_name, obj]
    except (AttributeError, ImportError) as exc:
        if objpath:
            errmsg = "autodoc: failed to import %s %r from module %r" % (
                objtype,
                ".".join(objpath),
                modname,
            )
        else:
            errmsg = "autodoc: failed to import %s %r" % (objtype, modname)

        if isinstance(exc, ImportError):
            # import_module() raises ImportError having real exception obj and
            # traceback
            real_exc, traceback_msg = exc.args
            if isinstance(real_exc, SystemExit):
                errmsg += (
                    "; the module executes module level statement "
                    "and it might call sys.exit()."
                )
            elif isinstance(real_exc, ImportError) and real_exc.args:
                errmsg += "; the following exception was raised:\n%s" % real_exc.args[0]
            else:
                errmsg += "; the following exception was raised:\n%s" % traceback_msg
        else:
            errmsg += (
                "; the following exception was raised:\n%s" % traceback.format_exc()
            )

        if PY2:
            errmsg = errmsg.decode("utf-8")  # type: ignore
        logger.debug(errmsg)
        raise ImportError(errmsg)