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 |
Refuse a payload rclpy would abort the process over, or put wrong on the wire. |
| Constant | INTEGER |
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 |
Refuse one value, naming the field, what ROS2 wants there and what arrived. |
| Function | _require |
Refuse one scalar value: a number, a string, or a whole nested message. |
| Function | _require |
Refuse one field's value, whatever shape the field has. |
| Function | _require |
Refuse a container that is not the length its ROS2 field declares. |
| Function | _require |
Refuse a nested field that does not hold the message dataclass its type says it does. |
| Function | _require |
Refuse a number, a boolean or a character that is not exactly what its field holds. |
| Function | _require |
Refuse a value whose Python type is not exactly one of the ones a field accepts. |
| Function | _require |
Refuse a length past a bound the ROS2 definition declared, if it declared one. |
| Constant | _SCALAR |
Undocumented |
Refuse a payload rclpy would abort the process over, or put wrong on the wire.
| Parameters | |
message:Message | The payload, as an instance of the dataclass built for its ROS2 type. |
path:str | The field path of this message, used in nested refusals. Empty at the root. |
| Raises | |
FieldTypeMismatchError | When a value is not the Python type its ROS2 field is. |
FieldValueOutOfRangeError | When a value does not fit its integer width, or exceeds a length its ROS2 field declares. |
What each ROS2 integer type holds. rclpy checks none of these at the boundary that matters.
| 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.
object, field_type: FieldType, nested: type[ Message] | None, path: str, type_name: str):
¶
Refuse one field's value, whatever shape the field has.
object, field_type: ArrayType | SequenceType, path: str, type_name: str):
¶
Refuse a container that is not the length its ROS2 field declares.
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.
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.