def msg2typestr(msg):
    """
    Introspects the message type from the passed in `msg` and encodes it as a
    string in the format required by rosbag2.

    Parameters
    ----------
    msg : Message
      A ROS 2 message (deserialized) whose type we need to introspect

    Returns
    -------
    A string encoding of the message type suitable for serialization to a ROS 2
    bag.

    """
    mod_components = msg.__module__.split(".")
    mod_components[-1] = msg.__class__.__name__
    return "/".join(mod_components)