Skip to content

Commit eb31e83

Browse files
Deprecate using set for container based input (#243)
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
1 parent ad7dc07 commit eb31e83

3 files changed

Lines changed: 20 additions & 11 deletions

File tree

rosidl_generator_py/resource/_msg.py.em

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,16 @@ if isinstance(member.type, (Array, AbstractSequence)):
494494

495495
@@@(member.name).setter@(noqa_string)
496496
def @(member.name)(self, value: @(type_annotations_setter[member.name])) -> None:@(noqa_string)
497+
498+
@[ if isinstance(member.type, AbstractNestedType)]@
499+
from collections.abc import Set
500+
if isinstance(value, Set):
501+
import warnings
502+
warnings.warn(
503+
'Using set or subclass of set is deprecated,'
504+
' please use a subclass of collections.abc.Sequence like list',
505+
DeprecationWarning)
506+
@[ end if]@
497507
if self._check_fields:
498508
@[ if isinstance(member.type, AbstractNestedType) and isinstance(member.type.value_type, BasicType) and member.type.value_type.typename in SPECIAL_NESTED_BASIC_TYPES]@
499509
@[ if isinstance(member.type, Array)]@
@@ -529,8 +539,6 @@ if isinstance(member.type, (Array, AbstractSequence)):
529539
@[ end if]@
530540
@[ if isinstance(member.type, AbstractNestedType)]@
531541
from collections.abc import Sequence
532-
from collections.abc import Set
533-
from collections import UserList
534542
from collections import UserString
535543
@[ elif isinstance(type_, AbstractGenericString) and type_.has_maximum_size()]@
536544
from collections import UserString
@@ -540,11 +548,10 @@ if isinstance(member.type, (Array, AbstractSequence)):
540548
assert \
541549
@[ if isinstance(member.type, AbstractNestedType)]@
542550
((isinstance(value, Sequence) or
543-
isinstance(value, Set) or
544-
isinstance(value, UserList)) and
551+
isinstance(value, Set)) and
545552
not isinstance(value, str) and
546553
not isinstance(value, UserString) and
547-
@{assert_msg_suffixes = ['a set or sequence']}@
554+
@{assert_msg_suffixes = ['sequence']}@
548555
@[ if isinstance(type_, AbstractGenericString) and type_.has_maximum_size()]@
549556
all(len(val) <= @(type_.maximum_size) for val in value) and
550557
@{assert_msg_suffixes.append('and each string value not longer than %d' % type_.maximum_size)}@

rosidl_generator_py/rosidl_generator_py/generate_py_impl.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,14 +414,13 @@ def get_setter_and_getter_type(member: Member, type_imports: set[str]) -> tuple[
414414
type_annotations_getter = f'typing.Annotated[typing.Any, {type_annotation}]'
415415

416416
if isinstance(member.type, AbstractNestedType):
417+
sequence_type = f'collections.abc.Sequence[{python_type}]'
418+
417419
if type_annotation != '':
418-
type_annotation = f'{type_annotation}, '
419-
type_annotation = (f'typing.Union[{type_annotation}'
420-
f'collections.abc.Sequence[{python_type}], '
421-
f'collections.abc.Set[{python_type}], '
422-
f'collections.UserList[{python_type}]]')
420+
type_annotation = f'typing.Union[{type_annotation}, {sequence_type}]'
421+
else:
422+
type_annotation = sequence_type
423423

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

rosidl_generator_py/test/test_interfaces.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,9 @@ def test_arrays() -> None:
529529

530530
assert msg2 != msg3
531531

532+
with pytest.warns(DeprecationWarning):
533+
Arrays(string_values={'bar', 'baz', 'foo'})
534+
532535

533536
def test_bounded_sequences() -> None:
534537
msg = BoundedSequences(check_fields=True)

0 commit comments

Comments
 (0)