module documentation

The Decoder: one live ROS2 message instance becomes one message dataclass.

It runs on the event loop. rclpy objects use numpy.ndarray and array.array containers and nested generated objects, none of which may cross into the server half. Each becomes a plain Python value:

  • a container of bytes (uint8 or byte, fixed or not) becomes bytes;
  • every other container becomes a tuple;
  • a nested message becomes its own message dataclass;
  • a byte scalar, which rclpy hands over as a one-byte bytes, becomes an int.

messages.is_byte_container decides the first case, so the dataclass annotation and the decoded value agree. The per-field plan is cached per message dataclass.

Function decode Convert one live ROS2 message instance into an instance of its message dataclass.
Function _as_bytes Return a container of bytes as bytes.
Function _as_int Return a byte scalar (a one-byte bytes in rclpy) as an integer.
Function _element_decoder Return the decoder for a scalar, or None when the value already needs no work.
Function _field_decoder Return the function that turns one rclpy field value into its plain Python value.
Function _plan Return the field-name and field-decoder pairs for a message dataclass, in field order.
Function _unchanged Return the value as it came (rclpy already hands over a builtin).
def decode(message: Any, message_type: type[Message]) -> Message:

Convert one live ROS2 message instance into an instance of its message dataclass.

def _as_bytes(value: Any) -> bytes:

Return a container of bytes as bytes.

rclpy hands over a numpy.ndarray for a fixed uint8 array, array.array('B') for a uint8 sequence and a list of one-byte bytes for an octet sequence.

def _as_int(value: Any) -> int:

Return a byte scalar (a one-byte bytes in rclpy) as an integer.

def _element_decoder(element: ElementType, nested: type[Message] | None) -> Callable[[Any], Any] | None:

Return the decoder for a scalar, or None when the value already needs no work.

def _field_decoder(field_type: FieldType, nested: type[Message] | None) -> Callable[[Any], Any]:

Return the function that turns one rclpy field value into its plain Python value.

@cache
def _plan(message_type: type[Message]) -> tuple[tuple[str, Callable[[Any], Any]], ...]:

Return the field-name and field-decoder pairs for a message dataclass, in field order.

def _unchanged(value: Any) -> Any:

Return the value as it came (rclpy already hands over a builtin).