Skip to content

Commit

Permalink
Edited the template to provide better docstring for message definition
Browse files Browse the repository at this point in the history
issue: ros2#196

Transcribed the original message comments in the python docstring
  • Loading branch information
guillaumeautran committed Jun 15, 2023
1 parent f1451cf commit d7872fa
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
58 changes: 54 additions & 4 deletions rosidl_generator_py/resource/_msg.py.em
Original file line number Diff line number Diff line change
Expand Up @@ -189,18 +189,68 @@ for member in message.structure.members:


class @(message.structure.namespaced_type.name)(metaclass=Metaclass_@(message.structure.namespaced_type.name)):
@[if not message.constants]@
"""Message class '@(message.structure.namespaced_type.name)'."""
@[else]@
"""
Message class '@(message.structure.namespaced_type.name)'.
@[if len(message.structure.get_comment_lines()) > 0]@
@[ for comment in message.structure.get_comment_lines()]@
@((" " + comment).rstrip(' ') + "\n")@
@[ end for]@
@[end if]@
@[if message.constants]@
Constants:
@[ for constant_name in [c.name for c in message.constants]]@
@(constant_name)
@[ end for]@
"""
@[end if]@
@[if message.structure.members]@
Fields:
@[for member in message.structure.members]@
@[ if len(message.structure.members) == 1 and member.name == EMPTY_STRUCTURE_REQUIRED_MEMBER_NAME]@
@[ continue]@
@[ end if]@
@{
type_ = member.type
if isinstance(type_, AbstractNestedType):
type_ = type_.value_type
}@
@(member.name) (@
@# the prefix for nested types
@[ if isinstance(member.type, AbstractSequence)]@
sequence<@
@[ end if]@
@# the typename of the non-nested type or the nested basetype
@[ if isinstance(type_, BasicType)]@
@(type_.typename)@
@[ elif isinstance(type_, AbstractGenericString)]@
@
@[ if isinstance(type_, AbstractWString)]@
w@
@[ end if]@
string@
@[ if type_.has_maximum_size()]@
<@(type_.maximum_size)>@
@[ end if]@
@[ elif isinstance(type_, NamespacedType)]@
@('/'.join([type_.namespaces[0], type_.name]))@
@[ end if]@
@# the suffix for nested types
@[ if isinstance(member.type, AbstractSequence)]@
@[ if isinstance(member.type, BoundedSequence)]@
, @(member.type.maximum_size)@
@[ end if]@
>@
@[ elif isinstance(member.type, Array)]@
[@(member.type.size)]@
@[ end if]@
)@(" ".join([':'] + member.get_comment_lines()).strip(' '))@
@[end for]@
@[end if]@
"""

__slots__ = [
@[for member in message.structure.members]@
Expand Down
2 changes: 1 addition & 1 deletion rosidl_generator_py/test/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def test_arrays_of_bounded_strings():
def test_constructor():
msg = Strings(string_value='foo', check_fields=True)

assert'foo' == msg.string_value
assert 'foo' == msg.string_value

with pytest.raises(AssertionError):
Strings(unknown_field='test', check_fields=True)
Expand Down

0 comments on commit d7872fa

Please sign in to comment.