package documentation

Message dataclasses: the payloads that cross the seam, and the ROS2 facts they carry.

A ROS2 message reaches the rest of this package as an instance of a frozen dataclass built here, never as an rclpy object. There is one dataclass per ROS2 message type, built once and cached. It carries its MessageDescription on the ros2 class variable, so Projections, field selection and the publish-time guard read ROS2 facts from a plain value.

The structure is taken by introspection over the imported interface class, not by parsing definition text, which gets four things wrong: fixed-size arrays (double[9] is nine values), fields named like a constant (points beside POINTS), string bounds (string<256>) and float widths (float32 against float64).

Dataclasses built at run time have no static field set, so the types come from stubs beside this module. <package>.py and <package>.pyi are generated from the definition catalog for interface packages without a binary release. from opensmi.ros2.runtime.messages.neo_msgs2 import USBoardV2 type-checks against the stub and resolves through message_dataclass at run time, to the dataclass a Binding of that type holds.

Module industrial_msgs The ROS2 interface package industrial_msgs, as message dataclasses.
Module motoros2_interfaces The ROS2 interface package motoros2_interfaces, as message dataclasses.
Module neo_msgs2 The ROS2 interface package neo_msgs2, as message dataclasses.
Module neo_srvs2 The ROS2 interface package neo_srvs2, as message dataclasses.

From __init__.py:

Class ActionTypes The three message types one ROS2 action carries, resolved together.
Class ArrayType A fixed-size array: exactly length elements, always.
Class FieldDescription One field of a ROS2 message.
Class Message Base of every message dataclass built by message_dataclass.
Class MessageDescription Everything the rest of the package needs to know about one ROS2 message type.
Class NestedType A field whose type is another ROS2 message.
Class PrimitiveType A single number, boolean or character.
Class SequenceType A variable-length sequence, bounded or not.
Class ServiceTypes The two message types one ROS2 service carries, resolved together.
Class StringType A string field, with the bound the interface declared for it.
Function action_types Return the goal, feedback and result message dataclasses of a ROS2 action type.
Function build_message_dataclass Build the frozen dataclass a MessageDescription describes.
Function canonical_type_name Return package/kind/Name, filling in msg for the two-part ROS2 shorthand.
Function element_of Return the scalar type a field holds, unwrapping an array or a sequence.
Function is_byte_container Return whether a field is an array or sequence of bytes, and so renders as bytes.
Function is_single_value Return whether a field is one value: a primitive or a string, neither nested nor repeated.
Function message_dataclass Return the message dataclass for a ROS2 message type, importing the interface package.
Function parse_field_type Model one entry of an interface class's field-type mapping.
Function repeated_message Return the message type one field repeats, or None when it repeats no message.
Function service_types Return the request and response message dataclasses of a ROS2 service type.
Constant ACTION_MEMBERS The three message types a ROS2 action is made of, as the generated class spells them.
Constant SERVICE_MEMBERS The two message types a ROS2 service is made of, as the generated class spells them.
Type Alias ElementType What may appear inside an array or a sequence, and on its own as a scalar field.
Type Alias FieldType Every shape a ROS2 message field can have.
Type Alias PrimitiveName The primitive ROS2 field types this Runtime models, spelled as a .msg file spells them.
Function _annotation Return the Python type a modelled field is annotated and decoded as.
Function _build_from_interface Build and memoise the dataclass for one canonical ROS2 type name.
Function _parse_element Model the scalar type inside an array, inside a sequence, or standing on its own.
Constant _ARRAY Undocumented
Constant _BYTE_LIKE Undocumented
Constant _IDL_PRIMITIVES Undocumented
Constant _KINDS Undocumented
Constant _NESTED Undocumented
Constant _PYTHON_TYPES Undocumented
Constant _SEQUENCE Undocumented
Constant _STRING Undocumented
Constant _THREE_PART Undocumented
Constant _TWO_PART Undocumented
def action_types(type_name: str) -> ActionTypes:

Return the goal, feedback and result message dataclasses of a ROS2 action type.

Synchronous and free of the Runtime, like message_dataclass.

Parameters
type_name:strpackage/action/Name. The msg shorthand is not accepted.
Returns
ActionTypesUndocumented
Raises
InvalidMessageTypeNameErrorWhen the name is not a three-part action name.
MissingInterfacePackageErrorWhen the interface package is not installed.
UnusableMessageTypeErrorWhen the package has no such action.
def build_message_dataclass(description: MessageDescription, *, resolve: Callable[[str], type[Message]] | None = None) -> type[Message]:

Build the frozen dataclass a MessageDescription describes.

A pure function of a value, so a test can hand it a description of a type nobody has installed.

Parameters
description:MessageDescriptionUndocumented
resolve:Callable[[str], type[Message]] | NoneHow to turn a nested field's type name into its dataclass. Defaults to message_dataclass.
Returns
type[Message]Undocumented
def canonical_type_name(type_name: str) -> str:

Return package/kind/Name, filling in msg for the two-part ROS2 shorthand.

Raises
InvalidMessageTypeNameErrorWhen the name has neither shape, or names a kind that is not one of msg, srv or action.
def element_of(field_type: FieldType) -> ElementType:

Return the scalar type a field holds, unwrapping an array or a sequence.

def is_byte_container(field_type: FieldType) -> bool:

Return whether a field is an array or sequence of bytes, and so renders as bytes.

Shared by the annotation and the Decoder, which must agree on which fields these are.

def is_single_value(field_type: FieldType) -> bool:

Return whether a field is one value: a primitive or a string, neither nested nor repeated.

What a key may be read from, and what a pair's two halves have to be.

def message_dataclass(type_name: str) -> type[Message]:

Return the message dataclass for a ROS2 message type, importing the interface package.

Synchronous and free of the Runtime: a Binding declares its type while a Component is being constructed, before any event loop exists, and a missing interface package must fail there.

The result is cached, and two Bindings declaring the same type get the same dataclass, so isinstance checks between them hold.

Parameters
type_name:strpackage/kind/Name, or package/Name for a message.
Returns
type[Message]Undocumented
Raises
InvalidMessageTypeNameErrorWhen the name has the wrong shape.
MissingInterfacePackageErrorWhen the interface package is not installed.
UnusableMessageTypeErrorWhen the package has no such message.
MessageTypeErrorWhen a field has a type this Runtime does not model.
def parse_field_type(text: str, *, nested_type_name: str | None = None) -> FieldType:

Model one entry of an interface class's field-type mapping.

A pure function, so the type model needs no ROS2 installation to test.

Parameters
text:strThe type string, as the interface class reports it (double, double[9], string<256>, sequence<uint8>, sequence<double, 3>, geometry_msgs/Point).
nested_type_name:str | NoneThe fully qualified name to give a nested type. Without it the package/Name form is read as a message type, which is wrong only for the generated wrapper types of a service or an action.
Returns
FieldTypeUndocumented
Raises
MessageTypeErrorWhen the string is a form this Runtime does not model.
def repeated_message(message_type: type[Message], field_name: str) -> type[Message] | None:

Return the message type one field repeats, or None when it repeats no message.

The field must exist, be an array or a sequence, and hold a message. The Keyed Binding and the recipe that turns such a field into a standard value both ask this.

def service_types(type_name: str) -> ServiceTypes:

Return the request and response message dataclasses of a ROS2 service type.

Synchronous and free of the Runtime, like action_types.

Parameters
type_name:strpackage/srv/Name. The msg shorthand is not accepted.
Returns
ServiceTypesUndocumented
Raises
InvalidMessageTypeNameErrorWhen the name is not a three-part service name.
MissingInterfacePackageErrorWhen the interface package is not installed.
UnusableMessageTypeErrorWhen the package has no such service.
ACTION_MEMBERS: tuple[str, ...] =

The three message types a ROS2 action is made of, as the generated class spells them.

They are ordinary message types (example_interfaces/action/Fibonacci_Goal).

Value
('Goal', 'Feedback', 'Result')
SERVICE_MEMBERS: tuple[str, ...] =

The two message types a ROS2 service is made of, as the generated class spells them.

A service package exports its members (std_srvs.srv.SetBool_Request), while an action's goal is only reachable as Fibonacci.Goal. The _Event member Jazzy adds is not listed.

Value
('Request', 'Response')
ElementType: TypeAlias =

What may appear inside an array or a sequence, and on its own as a scalar field.

Value
PrimitiveType | StringType | NestedType
FieldType: TypeAlias =

Every shape a ROS2 message field can have.

Value
ElementType | ArrayType | SequenceType
PrimitiveName: TypeAlias =

The primitive ROS2 field types this Runtime models, spelled as a .msg file spells them.

Value
Literal['bool',
        'byte',
        'char',
        'wchar',
        'float32',
        'float64',
        'int8',
...
def _annotation(field_type: FieldType, nested: type[Message] | None) -> object:

Return the Python type a modelled field is annotated and decoded as.

@cache
def _build_from_interface(canonical: str) -> type[Message]:

Build and memoise the dataclass for one canonical ROS2 type name.

def _parse_element(text: str, nested_type_name: str | None) -> ElementType:

Model the scalar type inside an array, inside a sequence, or standing on its own.

_ARRAY =

Undocumented

Value
re.compile(r'^(?P<element>.+)\[(?P<length>\d+)\]$')
_BYTE_LIKE: frozenset[PrimitiveName] =

Undocumented

Value
frozenset(set(['byte', 'uint8']))
_IDL_PRIMITIVES: Mapping[str, PrimitiveName] =

Undocumented

Value
{'boolean': 'bool',
 'octet': 'byte',
 'char': 'char',
 'wchar': 'wchar',
 'float': 'float32',
 'double': 'float64',
 'int8': 'int8',
...
_KINDS: tuple[str, ...] =

Undocumented

Value
('msg', 'srv', 'action')
_NESTED =

Undocumented

Value
re.compile(r'^(?P<package>[A-Za-z_]\w*)/(?P<name>[A-Za-z_]\w*)$')
_PYTHON_TYPES: Mapping[PrimitiveName, type] =

Undocumented

Value
{'bool': bool,
 'byte': int,
 'char': str,
 'wchar': str,
 'float32': float,
 'float64': float,
 'int8': int,
...
_SEQUENCE =

Undocumented

Value
re.compile(r'^sequence<(?P<element>.+?)(?:, (?P<bound>\d+))?>$')
_STRING =

Undocumented

Value
re.compile(r'^(?P<wide>w?)string(?:<(?P<bound>\d+)>)?$')
_THREE_PART: int =

Undocumented

Value
3
_TWO_PART: int =

Undocumented

Value
2