diff --git a/CHANGELOG.md b/CHANGELOG.md index f93ba75..c48231e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## Fixed - Metadata PDU typing correction. +- More robust `__eq__` implementations which check the type compared against. # [v0.21.0] 2023-11-10 diff --git a/spacepackets/ccsds/spacepacket.py b/spacepackets/ccsds/spacepacket.py index f0355e8..afa0238 100644 --- a/spacepackets/ccsds/spacepacket.py +++ b/spacepackets/ccsds/spacepacket.py @@ -346,8 +346,10 @@ def __repr__(self): f" sec_header_flag={self.sec_header_flag!r}, seq_flags={self.seq_flags!r})" ) - def __eq__(self, other: SpacePacketHeader): - return self.pack() == other.pack() + def __eq__(self, other: object): + if isinstance(other, SpacePacketHeader): + return self.pack() == other.pack() + return False class SpacePacket: @@ -404,12 +406,14 @@ def seq_count(self): def sec_header_flag(self): return self.sp_header.sec_header_flag - def __eq__(self, other: SpacePacket): - return ( - self.sp_header == other.sp_header - and self.sec_header == other.sec_header - and self.user_data == other.user_data - ) + def __eq__(self, other: object): + if isinstance(other, SpacePacket): + return ( + self.sp_header == other.sp_header + and self.sec_header == other.sec_header + and self.user_data == other.user_data + ) + return False def get_space_packet_id_bytes( diff --git a/spacepackets/ecss/tc.py b/spacepackets/ecss/tc.py index 65ac53b..e573180 100644 --- a/spacepackets/ecss/tc.py +++ b/spacepackets/ecss/tc.py @@ -92,8 +92,10 @@ def __repr__(self): f" subservice={self.subservice!r}, ack_flags={self.ack_flags!r} " ) - def __eq__(self, other: PusTcDataFieldHeader): - return self.pack() == other.pack() + def __eq__(self, other: object): + if isinstance(other, PusTcDataFieldHeader): + return self.pack() == other.pack() + return False @classmethod def get_header_size(cls): @@ -232,19 +234,21 @@ def __str__(self): f" {self.apid:#05x}, SSC {self.sp_header.seq_count}" ) - def __eq__(self, other: PusTelecommand): - return ( - self.sp_header == other.sp_header - and self.pus_tc_sec_header == other.pus_tc_sec_header - and self._app_data == other._app_data - ) + def __eq__(self, other: object): + if isinstance(other, PusTelecommand): + return ( + self.sp_header == other.sp_header + and self.pus_tc_sec_header == other.pus_tc_sec_header + and self._app_data == other._app_data + ) + return False def to_space_packet(self) -> SpacePacket: """Retrieve the generic CCSDS space packet representation. This also calculates the CRC16 before converting the PUS TC to a generic Space Packet""" self.calc_crc() user_data = bytearray(self._app_data) - user_data.extend(self._crc16) + user_data.extend(self._crc16) # type: ignore return SpacePacket(self.sp_header, self.pus_tc_sec_header.pack(), user_data) def calc_crc(self): diff --git a/spacepackets/ecss/tm.py b/spacepackets/ecss/tm.py index a876926..69e59f3 100644 --- a/spacepackets/ecss/tm.py +++ b/spacepackets/ecss/tm.py @@ -191,8 +191,10 @@ def __repr__(self): f" pus_version={self.pus_version!r})" ) - def __eq__(self, other: PusTmSecondaryHeader): - return self.pack() == other.pack() + def __eq__(self, other: object): + if isinstance(other, PusTmSecondaryHeader): + return self.pack() == other.pack() + return False @property def header_size(self) -> int: @@ -378,7 +380,7 @@ def to_space_packet(self) -> SpacePacket: before converting the PUS TC to a generic Space Packet""" self.calc_crc() user_data = bytearray(self._source_data) - user_data.extend(self.crc16) + user_data.extend(self.crc16) # type: ignore return SpacePacket( self.space_packet_header, self.pus_tm_sec_header.pack(), user_data ) @@ -397,12 +399,14 @@ def __repr__(self): f" sec_header={self.pus_tm_sec_header!r}, tm_data={self.tm_data!r}" ) - def __eq__(self, other: PusTelemetry): - return ( - self.space_packet_header == other.space_packet_header - and self.pus_tm_sec_header == other.pus_tm_sec_header - and self._source_data == other._source_data - ) + def __eq__(self, other: object): + if isinstance(other, PusTelemetry): + return ( + self.space_packet_header == other.space_packet_header + and self.pus_tm_sec_header == other.pus_tm_sec_header + and self._source_data == other._source_data + ) + return False @property def packet_seq_control(self) -> PacketSeqCtrl: