diff --git a/rosidl_generator_py/resource/_msg.py.em b/rosidl_generator_py/resource/_msg.py.em index 98285ecb..57819826 100644 --- a/rosidl_generator_py/resource/_msg.py.em +++ b/rosidl_generator_py/resource/_msg.py.em @@ -497,12 +497,29 @@ if isinstance(member.type, (Array, AbstractSequence)): @[ if isinstance(member.type, AbstractNestedType)]@ from collections.abc import Set + from collections.abc import Sequence + from collections.abc import MutableSequence 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', + ' please use a subclass of collections.abc.MutableSequence like list', DeprecationWarning) +@[ if isinstance(member.type, Array) and isinstance(member.type.value_type, BasicType) and member.type.value_type.typename in SPECIAL_NESTED_BASIC_TYPES]@ + if isinstance(value, Sequence) and not isinstance(value, (MutableSequence, numpy.ndarray)): + import warnings + warnings.warn( + "Using a subclass of Sequence that isn't a subclass of MutableSequence is deprecated," + ' please use a subclass of collections.abc.MutableSequence like list or a numpy.ndarray', + DeprecationWarning) +@[ else]@ + if isinstance(value, Sequence) and not isinstance(value, MutableSequence): + import warnings + warnings.warn( + "Using a subclass of Sequence that isn't a subclass of MutableSequence is deprecated," + ' please use a subclass of collections.abc.MutableSequence like list', + DeprecationWarning) +@[ end if]@ @[ 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]@ @@ -538,7 +555,6 @@ if isinstance(member.type, (Array, AbstractSequence)): @[ end if]@ @[ end if]@ @[ if isinstance(member.type, AbstractNestedType)]@ - from collections.abc import Sequence from collections import UserString @[ elif isinstance(type_, AbstractGenericString) and type_.has_maximum_size()]@ from collections import UserString diff --git a/rosidl_generator_py/test/test_interfaces.py b/rosidl_generator_py/test/test_interfaces.py index 8d11d423..86692e18 100644 --- a/rosidl_generator_py/test/test_interfaces.py +++ b/rosidl_generator_py/test/test_interfaces.py @@ -532,6 +532,12 @@ def test_arrays() -> None: with pytest.warns(DeprecationWarning): Arrays(string_values={'bar', 'baz', 'foo'}) + with pytest.warns(DeprecationWarning): + Arrays(string_values=('bar', 'baz', 'foo')) + + msg4 = Arrays(int8_values=numpy.array([4, 5, 3], dtype=numpy.int8)) + assert msg3 == msg4 + def test_bounded_sequences() -> None: msg = BoundedSequences(check_fields=True)