Skip to content

method frame_by_id may return None, which is not checked against self #860

@daemonPainter

Description

@daemonPainter

I submit for consideration the following:

    def frame_by_id(self, arbitration_id):  # type: (ArbitrationId) -> typing.Union[Frame, None]
        """Get Frame by its arbitration id.

        :param ArbitrationId arbitration_id: Frame id as canmatrix.ArbitrationId
        :rtype: Frame or None
        """
        hash_name = f"{arbitration_id.id}_{arbitration_id.extended}"
        
        frame = self._frames_dict_id_extend.get(hash_name, None)
        if frame is not None:
            return frame
        for frame in self.frames:
            if frame.arbitration_id == arbitration_id:
                # found ID while ignoring extended or standard
                self._frames_dict_id_extend[hash_name] = frame
                return frame
        return None

which is called later on as

    def encode(self,
               frame_id: ArbitrationId,
               data: typing.Mapping[str, typing.Any]
               ) -> bytes:
        """Return a byte string containing the values from data packed
        according to the frame format.

        :param frame_id: frame id
        :param data: data dictionary
        :return: A byte string of the packed values.
        """
        return self.frame_by_id(frame_id).encode(data)

It may happen that None is returned, therefore resulting in

AttributeError: 'NoneType' object has no attribute 'encode'

which is a situation that should (?) be safeguarded?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions