diff --git a/python/mcap-ros2-support/mcap_ros2/__init__.py b/python/mcap-ros2-support/mcap_ros2/__init__.py index dd9b22cccc..722515271f 100644 --- a/python/mcap-ros2-support/mcap_ros2/__init__.py +++ b/python/mcap-ros2-support/mcap_ros2/__init__.py @@ -1 +1 @@ -__version__ = "0.5.1" +__version__ = "0.5.2" diff --git a/python/mcap-ros2-support/mcap_ros2/_dynamic.py b/python/mcap-ros2-support/mcap_ros2/_dynamic.py index 22d925caaf..074973932d 100644 --- a/python/mcap-ros2-support/mcap_ros2/_dynamic.py +++ b/python/mcap-ros2-support/mcap_ros2/_dynamic.py @@ -259,6 +259,8 @@ def _read_complex_type( "__slots__": [field.name for field in msgdef.fields], "__repr__": __repr__, "__str__": __repr__, + "__eq__": __eq__, + "__ne__": __ne__, "_type": str(msgdef.base_type), "_full_text": str(msgdef), }, @@ -605,3 +607,25 @@ def _coerce_values( def __repr__(self: Any) -> str: fields = ", ".join(f"{field}={getattr(self, field)}" for field in self.__slots__) return f"{self.__name__}({fields})" + + +def __eq__(self: Any, other: Any) -> bool: + if not isinstance(other, type(self)): + return False + + if ( + not hasattr(self, "__slots__") + or not hasattr(other, "__slots__") + or len(self.__slots__) != len(other.__slots__) + ): + return False + + for attr in self.__slots__: + if getattr(self, attr) != getattr(other, attr): + return False + + return True + + +def __ne__(self: Any, other: Any) -> bool: + return not __eq__(self, other) diff --git a/python/mcap-ros2-support/tests/test_ros2_decoder.py b/python/mcap-ros2-support/tests/test_ros2_decoder.py index 1179121fed..f02086636f 100644 --- a/python/mcap-ros2-support/tests/test_ros2_decoder.py +++ b/python/mcap-ros2-support/tests/test_ros2_decoder.py @@ -24,3 +24,16 @@ def test_ros2_decoder(): assert ros_msg._full_text == "# std_msgs/Empty" count += 1 assert count == 10 + + +def test_ros2_decoder_msg_eq(): + with generate_sample_data() as m: + reader = make_reader(m, decoder_factories=[DecoderFactory()]) + + decoded_messages = reader.iter_decoded_messages("/chatter") + _, _, _, msg0 = next(decoded_messages) + _, _, _, msg1 = next(decoded_messages) + assert msg0.data == "string message 0" + assert msg1.data == "string message 1" + assert msg0 == msg0 and msg1 == msg1 + assert msg0 != msg1 and msg1 != msg0