Skip to content

Commit 44d27df

Browse files
authored
Start migration of Message init to use is_extended_id (#440)
* Start migration of Message init to use is_extended_id * Update deprecation versions in message.py #424
1 parent 92ae4f3 commit 44d27df

2 files changed

Lines changed: 20 additions & 10 deletions

File tree

can/message.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ class Message(object):
4343
"is_fd",
4444
"bitrate_switch",
4545
"error_state_indicator",
46-
"__weakref__", # support weak references to messages
47-
"_dict" # see __getattr__
46+
"__weakref__", # support weak references to messages
47+
"_dict" # see __getattr__
4848
)
4949

5050
def __getattr__(self, key):
5151
# TODO keep this for a version, in order to not break old code
5252
# this entire method (as well as the _dict attribute in __slots__ and the __setattr__ method)
53-
# can be removed in 3.0
53+
# can be removed in 4.0
5454
# this method is only called if the attribute was not found elsewhere, like in __slots__
5555
try:
5656
warnings.warn("Custom attributes of messages are deprecated and will be removed in the next major version", DeprecationWarning)
@@ -68,20 +68,21 @@ def __setattr__(self, key, value):
6868

6969
@property
7070
def id_type(self):
71-
# TODO remove in 3.0
71+
# TODO remove in 4.0
7272
warnings.warn("Message.id_type is deprecated, use is_extended_id", DeprecationWarning)
7373
return self.is_extended_id
7474

7575
@id_type.setter
7676
def id_type(self, value):
77-
# TODO remove in 3.0
77+
# TODO remove in 4.0
7878
warnings.warn("Message.id_type is deprecated, use is_extended_id", DeprecationWarning)
7979
self.is_extended_id = value
8080

81-
def __init__(self, timestamp=0.0, arbitration_id=0, extended_id=True,
81+
def __init__(self, timestamp=0.0, arbitration_id=0, is_extended_id=None,
8282
is_remote_frame=False, is_error_frame=False, channel=None,
8383
dlc=None, data=None,
8484
is_fd=False, bitrate_switch=False, error_state_indicator=False,
85+
extended_id=True,
8586
check=False):
8687
"""
8788
To create a message object, simply provide any of the below attributes
@@ -99,7 +100,13 @@ def __init__(self, timestamp=0.0, arbitration_id=0, extended_id=True,
99100

100101
self.timestamp = timestamp
101102
self.arbitration_id = arbitration_id
102-
self.is_extended_id = extended_id
103+
if is_extended_id is not None:
104+
self.is_extended_id = is_extended_id
105+
else:
106+
if not extended_id:
107+
# Passed extended_id=False (default argument is True) so we warn to update
108+
warnings.warn("extended_id is a deprecated parameter, use is_extended_id", DeprecationWarning)
109+
self.is_extended_id = extended_id
103110

104111
self.is_remote_frame = is_remote_frame
105112
self.is_error_frame = is_error_frame
@@ -152,7 +159,7 @@ def __str__(self):
152159
if self.data is not None:
153160
for index in range(0, min(self.dlc, len(self.data))):
154161
data_strings.append("{0:02x}".format(self.data[index]))
155-
if data_strings: # if not empty
162+
if data_strings: # if not empty
156163
field_strings.append(" ".join(data_strings).ljust(24, " "))
157164
else:
158165
field_strings.append(" " * 24)

doc/message.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,18 @@ Message
103103
:type: bool
104104

105105
This flag controls the size of the :attr:`~can.Message.arbitration_id` field.
106+
Previously this was exposed as `id_type`.
106107

107108
>>> print(Message(extended_id=False))
108109
Timestamp: 0.000000 ID: 0000 S DLC: 0
109110
>>> print(Message(extended_id=True))
110111
Timestamp: 0.000000 ID: 00000000 X DLC: 0
111112

112113

113-
Previously this was exposed as `id_type`.
114-
Please use `is_extended_id` from now on.
114+
.. note::
115+
116+
The :meth:`Message.__init__` argument ``extended_id`` has been deprecated in favor of
117+
``is_extended_id``, but will continue to work for the ``3.x`` release series.
115118

116119

117120
.. attribute:: is_error_frame

0 commit comments

Comments
 (0)