Skip to content

Commit a77cf77

Browse files
committed
Added forced casting of int values ​​to float for float-typed fields
Signed-off-by: EsipovPA <[email protected]>
1 parent 1fbd99b commit a77cf77

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

rosidl_generator_py/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ if(BUILD_TESTING)
5353
msg/BuiltinTypeSequencesIdl.idl
5454
msg/StringArrays.msg
5555
msg/Property.msg
56+
msg/Float.msg
5657
ADD_LINTER_TESTS
5758
SKIP_INSTALL
5859
)
@@ -79,6 +80,12 @@ if(BUILD_TESTING)
7980
APPEND_LIBRARY_DIRS "${_append_library_dirs}"
8081
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_py"
8182
)
83+
84+
ament_add_pytest_test(test_int_to_float_py test/test_int_to_float.py
85+
APPEND_ENV "PYTHONPATH=${pythonpath}"
86+
APPEND_LIBRARY_DIRS "${_append_library_dirs}"
87+
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_py"
88+
)
8289
endif()
8390
endif()
8491

rosidl_generator_py/msg/Float.msg

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
float32 float_value

rosidl_generator_py/resource/_msg.py.em

+4
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,10 @@ if member.name in dict(inspect.getmembers(builtins)).keys():
481481
from collections.abc import ByteString
482482
@[ elif isinstance(type_, BasicType) and type_.typename in CHARACTER_TYPES]@
483483
from collections import UserString
484+
@[ end if]@
485+
@[ if isinstance(type_, BasicType) and type_.typename in FLOATING_POINT_TYPES]@
486+
if isinstance(value, int):
487+
value = float(int)
484488
@[ end if]@
485489
assert \
486490
@[ if isinstance(member.type, AbstractNestedType)]@
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright 2021 Open Source Robotics Foundation, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from rosidl_generator_py.msg import Float
16+
17+
18+
def test_int_to_float():
19+
msg = Float()
20+
21+
assert isinstance(msg.float_value, float)
22+
23+
# default value
24+
assert 0 == msg.float_value
25+
26+
# set float value
27+
msg.float_value = float(1)
28+
29+
assert 1 == msg.float_value
30+
31+
# set int value, should not throw
32+
msg.float_value = int(1)
33+
34+
assert 1 == msg.float_value

0 commit comments

Comments
 (0)