Skip to content

Commit

Permalink
Added forced casting of int values ​​to float for float-typed fields
Browse files Browse the repository at this point in the history
Signed-off-by: EsipovPA <[email protected]>
  • Loading branch information
EsipovPA committed Sep 10, 2024
1 parent 1fbd99b commit dafb769
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
7 changes: 7 additions & 0 deletions rosidl_generator_py/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ if(BUILD_TESTING)
msg/BuiltinTypeSequencesIdl.idl
msg/StringArrays.msg
msg/Property.msg
msg/Float.msg
ADD_LINTER_TESTS
SKIP_INSTALL
)
Expand All @@ -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()

Expand Down
1 change: 1 addition & 0 deletions rosidl_generator_py/msg/Float.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
float32 float_value
4 changes: 4 additions & 0 deletions rosidl_generator_py/resource/_msg.py.em
Original file line number Diff line number Diff line change
Expand Up @@ -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(value)
@[ end if]@
assert \
@[ if isinstance(member.type, AbstractNestedType)]@
Expand Down
26 changes: 26 additions & 0 deletions rosidl_generator_py/test/test_int_to_float.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 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():
msg1 = Float(check_types=True, float_value=float(1))

assert isinstance(msg1.float_value, float)
assert 1 == msg1.float_value

msg2 = Float(check_types=True, float_value=int(1))
assert isinstance(msg2.float_value, float)
assert 1 == msg2.float_value

0 comments on commit dafb769

Please sign in to comment.