88 starting with Python 3.7.
99"""
1010
11+ from typing import Optional , Union
12+
13+ from . import typechecking
1114
1215from copy import deepcopy
1316from math import isinf , isnan
@@ -48,28 +51,28 @@ class Message:
4851
4952 def __init__ (
5053 self ,
51- timestamp = 0.0 ,
52- arbitration_id = 0 ,
53- is_extended_id = True ,
54- is_remote_frame = False ,
55- is_error_frame = False ,
56- channel = None ,
57- dlc = None ,
58- data = None ,
59- is_fd = False ,
60- bitrate_switch = False ,
61- error_state_indicator = False ,
62- check = False ,
54+ timestamp : float = 0.0 ,
55+ arbitration_id : int = 0 ,
56+ is_extended_id : bool = True ,
57+ is_remote_frame : bool = False ,
58+ is_error_frame : bool = False ,
59+ channel : Optional [ typechecking . Channel ] = None ,
60+ dlc : Optional [ int ] = None ,
61+ data : Optional [ typechecking . CanData ] = None ,
62+ is_fd : bool = False ,
63+ bitrate_switch : bool = False ,
64+ error_state_indicator : bool = False ,
65+ check : bool = False ,
6366 ):
6467 """
6568 To create a message object, simply provide any of the below attributes
6669 together with additional parameters as keyword arguments to the constructor.
6770
68- :param bool check: By default, the constructor of this class does not strictly check the input.
69- Thus, the caller must prevent the creation of invalid messages or
70- set this parameter to `True`, to raise an Error on invalid inputs.
71- Possible problems include the `dlc` field not matching the length of `data`
72- or creating a message with both `is_remote_frame` and `is_error_frame` set to `True`.
71+ :param check: By default, the constructor of this class does not strictly check the input.
72+ Thus, the caller must prevent the creation of invalid messages or
73+ set this parameter to `True`, to raise an Error on invalid inputs.
74+ Possible problems include the `dlc` field not matching the length of `data`
75+ or creating a message with both `is_remote_frame` and `is_error_frame` set to `True`.
7376
7477 :raises ValueError: iff `check` is set to `True` and one or more arguments were invalid
7578 """
@@ -102,7 +105,7 @@ def __init__(
102105 if check :
103106 self ._check ()
104107
105- def __str__ (self ):
108+ def __str__ (self ) -> str :
106109 field_strings = ["Timestamp: {0:>15.6f}" .format (self .timestamp )]
107110 if self .is_extended_id :
108111 arbitration_id_string = "ID: {0:08x}" .format (self .arbitration_id )
@@ -144,14 +147,14 @@ def __str__(self):
144147
145148 return " " .join (field_strings ).strip ()
146149
147- def __len__ (self ):
150+ def __len__ (self ) -> int :
148151 # return the dlc such that it also works on remote frames
149152 return self .dlc
150153
151- def __bool__ (self ):
154+ def __bool__ (self ) -> bool :
152155 return True
153156
154- def __repr__ (self ):
157+ def __repr__ (self ) -> str :
155158 args = [
156159 "timestamp={}" .format (self .timestamp ),
157160 "arbitration_id={:#x}" .format (self .arbitration_id ),
@@ -177,16 +180,16 @@ def __repr__(self):
177180
178181 return "can.Message({})" .format (", " .join (args ))
179182
180- def __format__ (self , format_spec ) :
183+ def __format__ (self , format_spec : Optional [ str ]) -> str :
181184 if not format_spec :
182185 return self .__str__ ()
183186 else :
184187 raise ValueError ("non empty format_specs are not supported" )
185188
186- def __bytes__ (self ):
189+ def __bytes__ (self ) -> bytes :
187190 return bytes (self .data )
188191
189- def __copy__ (self ):
192+ def __copy__ (self ) -> "Message" :
190193 new = Message (
191194 timestamp = self .timestamp ,
192195 arbitration_id = self .arbitration_id ,
@@ -202,7 +205,7 @@ def __copy__(self):
202205 )
203206 return new
204207
205- def __deepcopy__ (self , memo ) :
208+ def __deepcopy__ (self , memo : dict ) -> "Message" :
206209 new = Message (
207210 timestamp = self .timestamp ,
208211 arbitration_id = self .arbitration_id ,
@@ -278,17 +281,17 @@ def _check(self):
278281 "error state indicator is only allowed for CAN FD frames"
279282 )
280283
281- def equals (self , other , timestamp_delta = 1.0e-6 ):
284+ def equals (
285+ self , other : "Message" , timestamp_delta : Optional [Union [float , int ]] = 1.0e-6
286+ ) -> bool :
282287 """
283288 Compares a given message with this one.
284289
285- :param can.Message other: the message to compare with
290+ :param other: the message to compare with
286291
287- :type timestamp_delta: float or int or None
288292 :param timestamp_delta: the maximum difference at which two timestamps are
289293 still considered equal or None to not compare timestamps
290294
291- :rtype: bool
292295 :return: True iff the given message equals this one
293296 """
294297 # see https://github.com/hardbyte/python-can/pull/413 for a discussion
0 commit comments