module documentation

The type-mismatch guard: what may be put into a ROS2 message, and what aborts the process.

A ROS2 message field given a value of the wrong Python type does not raise in rclpy. The C conversion that runs when the message is sent dies on Assertion 'PyFloat_Check(field)' failed, a SIGABRT that takes down every Component and the OPC UA endpoint (pinned by a Jazzy test). The check therefore uses exact types, not isinstance: 1 is not a float64 and True is not an int32.

A value out of its field's range is refused too: 2**31 into an int32 goes onto the wire wrong. Declared bounds (string<256>, sequence<double, 3>, double[9]) are enforced here as well, since OPC UA advertises a bound and enforces none.

The guard is a pure function of a message dataclass. The Runtime calls it on every goal it sends and every message it publishes; the server half calls it at declaration time on the initial values, so defaults that could never be sent fail at init.

Function require_sendable Refuse a payload rclpy would abort the process over, or put wrong on the wire.
Constant INTEGER_RANGES What each ROS2 integer type holds. rclpy checks none of these at the boundary that matters.
Function _joined Return one field path below another, with the separator every path here uses.
Function _named Return how a refusal names the ROS2 type a value was supposed to be.
Function _refuse_the_type Refuse one value, naming the field, what ROS2 wants there and what arrived.
Function _require_element Refuse one scalar value: a number, a string, or a whole nested message.
Function _require_field Refuse one field's value, whatever shape the field has.
Function _require_length Refuse a container that is not the length its ROS2 field declares.
Function _require_message Refuse a nested field that does not hold the message dataclass its type says it does.
Function _require_primitive Refuse a number, a boolean or a character that is not exactly what its field holds.
Function _require_type Refuse a value whose Python type is not exactly one of the ones a field accepts.
Function _require_within Refuse a length past a bound the ROS2 definition declared, if it declared one.
Constant _SCALAR_TYPES Undocumented
def require_sendable(message: Message, *, path: str = ''):

Refuse a payload rclpy would abort the process over, or put wrong on the wire.

Parameters
message:MessageThe payload, as an instance of the dataclass built for its ROS2 type.
path:strThe field path of this message, used in nested refusals. Empty at the root.
Raises
FieldTypeMismatchErrorWhen a value is not the Python type its ROS2 field is.
FieldValueOutOfRangeErrorWhen a value does not fit its integer width, or exceeds a length its ROS2 field declares.
INTEGER_RANGES: Mapping[str, tuple[int, int]] =

What each ROS2 integer type holds. rclpy checks none of these at the boundary that matters.

Value
{'byte': (0, 255),
 'int8': (-2 ** 7, 2 ** 7 - 1),
 'int16': (-2 ** 15, 2 ** 15 - 1),
 'int32': (-2 ** 31, 2 ** 31 - 1),
 'int64': (-2 ** 63, 2 ** 63 - 1),
 'uint8': (0, 2 ** 8 - 1),
 'uint16': (0, 2 ** 16 - 1),
...
def _joined(path: str, name: str) -> str:

Return one field path below another, with the separator every path here uses.

def _named(element: ElementType) -> str:

Return how a refusal names the ROS2 type a value was supposed to be.

def _refuse_the_type(value: object, named: str, path: str, type_name: str):

Refuse one value, naming the field, what ROS2 wants there and what arrived.

def _require_element(value: object, element: ElementType, nested: type[Message] | None, path: str, type_name: str):

Refuse one scalar value: a number, a string, or a whole nested message.

def _require_field(value: object, field_type: FieldType, nested: type[Message] | None, path: str, type_name: str):

Refuse one field's value, whatever shape the field has.

def _require_length(value: object, field_type: ArrayType | SequenceType, path: str, type_name: str):

Refuse a container that is not the length its ROS2 field declares.

def _require_message(value: object, element: NestedType, nested: type[Message] | None, path: str, type_name: str):

Refuse a nested field that does not hold the message dataclass its type says it does.

def _require_primitive(value: object, name: PrimitiveName, path: str, type_name: str):

Refuse a number, a boolean or a character that is not exactly what its field holds.

def _require_type(value: object, expected: tuple[type, ...], named: str, path: str, type_name: str):

Refuse a value whose Python type is not exactly one of the ones a field accepts.

def _require_within(length: int, bound: int | None, what: str, path: str, type_name: str):

Refuse a length past a bound the ROS2 definition declared, if it declared one.

_SCALAR_TYPES: Mapping[str, type] =

Undocumented

Value
{'bool': bool, 'char': str, 'wchar': str, 'float32': float, 'float64': float}