From a77cf77a6c32c7e940cde1375d2ec92c6b2b73ff Mon Sep 17 00:00:00 2001 From: EsipovPA Date: Tue, 10 Sep 2024 14:54:13 +0300 Subject: [PATCH] =?UTF-8?q?Added=20forced=20casting=20of=20int=20values=20?= =?UTF-8?q?=E2=80=8B=E2=80=8Bto=20float=20for=20float-typed=20fields?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: EsipovPA --- rosidl_generator_py/CMakeLists.txt | 7 ++++ rosidl_generator_py/msg/Float.msg | 1 + rosidl_generator_py/resource/_msg.py.em | 4 +++ rosidl_generator_py/test/test_int_to_float.py | 34 +++++++++++++++++++ 4 files changed, 46 insertions(+) create mode 100644 rosidl_generator_py/msg/Float.msg create mode 100644 rosidl_generator_py/test/test_int_to_float.py diff --git a/rosidl_generator_py/CMakeLists.txt b/rosidl_generator_py/CMakeLists.txt index 07d124a1..cdae8ee8 100644 --- a/rosidl_generator_py/CMakeLists.txt +++ b/rosidl_generator_py/CMakeLists.txt @@ -53,6 +53,7 @@ if(BUILD_TESTING) msg/BuiltinTypeSequencesIdl.idl msg/StringArrays.msg msg/Property.msg + msg/Float.msg ADD_LINTER_TESTS SKIP_INSTALL ) @@ -79,6 +80,12 @@ if(BUILD_TESTING) APPEND_LIBRARY_DIRS "${_append_library_dirs}" WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_py" ) + + ament_add_pytest_test(test_int_to_float_py test/test_int_to_float.py + APPEND_ENV "PYTHONPATH=${pythonpath}" + APPEND_LIBRARY_DIRS "${_append_library_dirs}" + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_py" + ) endif() endif() diff --git a/rosidl_generator_py/msg/Float.msg b/rosidl_generator_py/msg/Float.msg new file mode 100644 index 00000000..91bec70e --- /dev/null +++ b/rosidl_generator_py/msg/Float.msg @@ -0,0 +1 @@ +float32 float_value diff --git a/rosidl_generator_py/resource/_msg.py.em b/rosidl_generator_py/resource/_msg.py.em index 8c21e552..f0eece45 100644 --- a/rosidl_generator_py/resource/_msg.py.em +++ b/rosidl_generator_py/resource/_msg.py.em @@ -481,6 +481,10 @@ if member.name in dict(inspect.getmembers(builtins)).keys(): from collections.abc import ByteString @[ elif isinstance(type_, BasicType) and type_.typename in CHARACTER_TYPES]@ from collections import UserString +@[ end if]@ +@[ if isinstance(type_, BasicType) and type_.typename in FLOATING_POINT_TYPES]@ + if isinstance(value, int): + value = float(int) @[ end if]@ assert \ @[ if isinstance(member.type, AbstractNestedType)]@ diff --git a/rosidl_generator_py/test/test_int_to_float.py b/rosidl_generator_py/test/test_int_to_float.py new file mode 100644 index 00000000..74942f06 --- /dev/null +++ b/rosidl_generator_py/test/test_int_to_float.py @@ -0,0 +1,34 @@ +# Copyright 2021 Open Source Robotics Foundation, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from rosidl_generator_py.msg import Float + + +def test_int_to_float(): + msg = Float() + + assert isinstance(msg.float_value, float) + + # default value + assert 0 == msg.float_value + + # set float value + msg.float_value = float(1) + + assert 1 == msg.float_value + + # set int value, should not throw + msg.float_value = int(1) + + assert 1 == msg.float_value