import importlib


def typestr2msgtype(type_str):
    """
    Given a ROS 2 bag message type encoding string, this function will return a
    Type instance of the actual Python message type that can then be used for
    creating instances of the particular message class. If the loader for the
    class type is not on your `sys.path` an exception will be thrown.

    Parameters
    ----------
    type_str : str
      A string encoding of a message type compatible with rosbag2

    Returns
    -------
    The `Type` of the Python message.

    """
    module_components = type_str.split("/")
    type_name = module_components[-1]

    module_str = ".".join(module_components[0:-1])
    module = importlib.import_module(module_str)

    return type(getattr(module, type_name)())