Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions rosidl_generator_py/resource/_msg.py.em
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,17 @@ if isinstance(member.type, (Array, AbstractSequence)):

@@@(member.name).setter@(noqa_string)
def @(member.name)(self, value: @(type_annotations_setter[member.name])) -> None:@(noqa_string)

@[ if isinstance(member.type, AbstractNestedType)]@
from collections.abc import Set
if isinstance(value, Set):
import warnings
warnings.warn(
'Using set or subclass of set is deprecated'
' please use a subclass of collections.abc.Sequence like list',
DeprecationWarning)

@[ end if]@
if self._check_fields:
@[ if isinstance(member.type, AbstractNestedType) and isinstance(member.type.value_type, BasicType) and member.type.value_type.typename in SPECIAL_NESTED_BASIC_TYPES]@
@[ if isinstance(member.type, Array)]@
Expand Down Expand Up @@ -529,8 +540,6 @@ if isinstance(member.type, (Array, AbstractSequence)):
@[ end if]@
@[ if isinstance(member.type, AbstractNestedType)]@
from collections.abc import Sequence
from collections.abc import Set
from collections import UserList
from collections import UserString
@[ elif isinstance(type_, AbstractGenericString) and type_.has_maximum_size()]@
from collections import UserString
Expand All @@ -540,11 +549,10 @@ if isinstance(member.type, (Array, AbstractSequence)):
assert \
@[ if isinstance(member.type, AbstractNestedType)]@
((isinstance(value, Sequence) or
isinstance(value, Set) or
isinstance(value, UserList)) and
isinstance(value, Set)) and
not isinstance(value, str) and
not isinstance(value, UserString) and
@{assert_msg_suffixes = ['a set or sequence']}@
@{assert_msg_suffixes = ['sequence']}@
@[ if isinstance(type_, AbstractGenericString) and type_.has_maximum_size()]@
all(len(val) <= @(type_.maximum_size) for val in value) and
@{assert_msg_suffixes.append('and each string value not longer than %d' % type_.maximum_size)}@
Expand Down
11 changes: 5 additions & 6 deletions rosidl_generator_py/rosidl_generator_py/generate_py_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,14 +414,13 @@ def get_setter_and_getter_type(member: Member, type_imports: set[str]) -> tuple[
type_annotations_getter = f'typing.Annotated[typing.Any, {type_annotation}]'

if isinstance(member.type, AbstractNestedType):
sequence_type = f'collections.abc.Sequence[{python_type}]'

if type_annotation != '':
type_annotation = f'{type_annotation}, '
type_annotation = (f'typing.Union[{type_annotation}'
f'collections.abc.Sequence[{python_type}], '
f'collections.abc.Set[{python_type}], '
f'collections.UserList[{python_type}]]')
type_annotation = f'typing.Union[{type_annotation}, {sequence_type}]'
else:
type_annotation = sequence_type

type_imports.add('import collections')
elif isinstance(member.type, AbstractGenericString) and member.type.has_maximum_size():
type_annotation = 'typing.Union[str, collections.UserString]'

Expand Down
3 changes: 3 additions & 0 deletions rosidl_generator_py/test/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,9 @@ def test_arrays() -> None:

assert msg2 != msg3

with pytest.warns(DeprecationWarning):
Arrays(string_values={'bar', 'baz', 'foo'})


def test_bounded_sequences() -> None:
msg = BoundedSequences(check_fields=True)
Expand Down