Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added forced casting of int values ​​to float for float-typed fields #216

Open
wants to merge 1 commit into
base: rolling
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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