-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve array support #8
Improve array support #8
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much! 😃
I left some comments, please take a look!
FYI: I made a suggestion patch to this PR: 70dad0e |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fantastic! I agee with all of that. I've made those changes.
Co-authored-by: Yuki Igarashi <[email protected]>
type_annotation = to_type_annotation( | ||
current_namespace, defined_classes, type_.value_type | ||
) | ||
return Annotation( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I read the specification.
https://design.ros2.org/articles/idl_interface_definition.html
According to the Type Mapping
section on the document, shouldn't we check type_annotation.getter in SPECIAL_NESTED_BASIC_TYPES
in the same way as AbstractSequence
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure... I've tested this against most of the standard ros interfaces (eg sensor_msgs) and I think everything looked correct.
It could be prudent to write some tests which colcon build
a bunch of messages and make assertions about the pyi
code it generates but I'm not sure I'll have time to do that right now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I referred to the rosidl_python
implementation, and it actually follows the specification.
I think the member initialization part is helpful to understand this behavior:
https://github.com/ros2/rosidl_python/blob/6675823dd6123360912e233a830d1edab89c7651/rosidl_generator_py/resource/_msg.py.em#L320-L332
I've also tested with the following message definition and got the same result that I expected.
Definition
int64[] integer_array
int64[4] integer_fixed_array
std_msgs/Header[] header_array
std_msgs/Header[4] header_fixed_array
integer_array
@integer_array.setter
def integer_array(self, value):
if isinstance(value, array.array):
assert value.typecode == 'q', \
"The 'integer_array' array.array() must have the type code of 'q'"
self._integer_array = value
return
if __debug__:
from collections.abc import Sequence
from collections.abc import Set
from collections import UserList
from collections import UserString
assert \
((isinstance(value, Sequence) or
isinstance(value, Set) or
isinstance(value, UserList)) and
not isinstance(value, str) and
not isinstance(value, UserString) and
all(isinstance(v, int) for v in value) and
all(val >= -9223372036854775808 and val < 9223372036854775808 for val in value)), \
"The 'integer_array' field must be a set or sequence and each value of type 'int' and each integer in [-9223372036854775808, 9223372036854775807]"
self._integer_array = array.array('q', value)
integer_fixed_array
@integer_fixed_array.setter
def integer_fixed_array(self, value):
if isinstance(value, numpy.ndarray):
assert value.dtype == numpy.int64, \
"The 'integer_fixed_array' numpy.ndarray() must have the dtype of 'numpy.int64'"
assert value.size == 4, \
"The 'integer_fixed_array' numpy.ndarray() must have a size of 4"
self._integer_fixed_array = value
return
if __debug__:
from collections.abc import Sequence
from collections.abc import Set
from collections import UserList
from collections import UserString
assert \
((isinstance(value, Sequence) or
isinstance(value, Set) or
isinstance(value, UserList)) and
not isinstance(value, str) and
not isinstance(value, UserString) and
len(value) == 4 and
all(isinstance(v, int) for v in value) and
all(val >= -9223372036854775808 and val < 9223372036854775808 for val in value)), \
"The 'integer_fixed_array' field must be a set or sequence with length 4 and each value of type 'int' and each integer in [-9223372036854775808, 9223372036854775807]"
self._integer_fixed_array = numpy.array(value, dtype=numpy.int64)
header_array
@header_array.setter
def header_array(self, value):
if __debug__:
from std_msgs.msg import Header
from collections.abc import Sequence
from collections.abc import Set
from collections import UserList
from collections import UserString
assert \
((isinstance(value, Sequence) or
isinstance(value, Set) or
isinstance(value, UserList)) and
not isinstance(value, str) and
not isinstance(value, UserString) and
all(isinstance(v, Header) for v in value) and
True), \
"The 'header_array' field must be a set or sequence and each value of type 'Header'"
self._header_array = value
header_fixed_array
@header_fixed_array.setter
def header_fixed_array(self, value):
if __debug__:
from std_msgs.msg import Header
from collections.abc import Sequence
from collections.abc import Set
from collections import UserList
from collections import UserString
assert \
((isinstance(value, Sequence) or
isinstance(value, Set) or
isinstance(value, UserList)) and
not isinstance(value, str) and
not isinstance(value, UserString) and
len(value) == 4 and
all(isinstance(v, Header) for v in value) and
True), \
"The 'header_fixed_array' field must be a set or sequence with length 4 and each value of type 'Header'"
self._header_fixed_array = value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MrBlenny I also encountered the same issue as you 😅
Actually, this PR is great, and It would be wonderful to merge this PR soon.
If you don't have time to update this PR, please let me know. I am happy to help you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the update!
Sorry, I left another comment 🙇♂️
@MrBlenny I left a comment. Let me kindly ping you just in case... |
Sorry @bonprosoft |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Thank you!
Thanks so much for your work here. This PR aims to improve support for arrays and sequences.
Example 1 - array.array[?]:
sensor_msgs/Image.msg
has propertyuint8[] data
This generates the following in
_image.py
As such, the
pyi
should be:Example 2 - np.ndarray:
sensor_msgs/Imu.msg
has propertyfloat64[9] orientation_covariance
This generates the following in
_imu.py
As such, the
pyi
should be:This PR corrects the
pyi
to be as described above. Note thatarray
andnumpy
is now imported into EVERYpyi
file, even if it is not used.