import importlib


def find_type_object(type_name):
    """Try to find the type object given the name of the type.

    Deals with cases such as `argparse.ArgumentParser` where a module needs to
    be imported before we can find the correct type object. If the type object
    cannot be found, a `NameError` is raised.

    Parameters
    ----------
    type_name : str
        The name of the type to search for.

    Returns
    -------
    type_object : type
        The type object corresponding to the given type name.
    """
    if '.' in type_name:
        # Full module name specified. Try to import it.
        module_name, object_name = type_name.rsplit('.', maxsplit=1)
        try:
            module = importlib.import_module(module_name)
        except ModuleNotFoundError:
            raise NameError(f'Type checker could not import `{module_name}`.')
        if object_name not in module.__dict__:
            raise NameError(f'Type checker could not find `{type_name}`.')
        return module.__dict__[object_name]
    else:
        # No module name specified. Assume it's a python buildin type.
        return eval(type_name)