Skip to content

Commit c18d2b7

Browse files
committed
Merge branch 'rolling' of github.com:ros2/rosidl into nan-constants
Signed-off-by: Marco A. Gutierrez <marcogg@marcogg.com>
2 parents 2e0e779 + c027a38 commit c18d2b7

File tree

12 files changed

+68
-27
lines changed

12 files changed

+68
-27
lines changed

rosidl_generator_c/rosidl_generator_c/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
# limitations under the License.
1414
from math import isnan
1515

16+
from typing import List
17+
1618
from rosidl_generator_type_description import parse_rihs_string
1719
from rosidl_generator_type_description import RIHS01_HASH_VALUE_SIZE
1820
from rosidl_parser.definition import AbstractGenericString
@@ -29,7 +31,7 @@
2931
from rosidl_pycommon import generate_files
3032

3133

32-
def generate_c(generator_arguments_file, disable_description_codegen=False):
34+
def generate_c(generator_arguments_file, disable_description_codegen: bool = False) -> List[str]:
3335
mapping = {
3436
'idl.h.em': '%s.h',
3537
'idl__description.c.em': 'detail/%s__description.c',
@@ -47,7 +49,7 @@ def generate_c(generator_arguments_file, disable_description_codegen=False):
4749
})
4850

4951

50-
def prefix_with_bom_if_necessary(content):
52+
def prefix_with_bom_if_necessary(content: str) -> str:
5153
try:
5254
content.encode('ASCII')
5355
except UnicodeError:

rosidl_generator_cpp/rosidl_generator_cpp/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from ast import literal_eval
1616
from math import isnan
17+
from typing import List
1718

1819
from rosidl_parser.definition import AbstractGenericString
1920
from rosidl_parser.definition import AbstractNestedType
@@ -29,7 +30,7 @@
2930
from rosidl_pycommon import generate_files
3031

3132

32-
def generate_cpp(generator_arguments_file):
33+
def generate_cpp(generator_arguments_file) -> List[str]:
3334
mapping = {
3435
'idl.hpp.em': '%s.hpp',
3536
'idl__builder.hpp.em': 'detail/%s__builder.hpp',
@@ -42,7 +43,7 @@ def generate_cpp(generator_arguments_file):
4243
post_process_callback=prefix_with_bom_if_necessary)
4344

4445

45-
def prefix_with_bom_if_necessary(content):
46+
def prefix_with_bom_if_necessary(content: str) -> str:
4647
try:
4748
content.encode('ASCII')
4849
except UnicodeError:

rosidl_pycommon/package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<test_depend>ament_copyright</test_depend>
2121
<test_depend>ament_flake8</test_depend>
2222
<test_depend>ament_pep257</test_depend>
23+
<test_depend>ament_mypy</test_depend>
2324
<test_depend>python3-pytest</test_depend>
2425

2526
<export>

rosidl_pycommon/py.typed

Whitespace-only changes.

rosidl_pycommon/rosidl_pycommon/__init__.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import pathlib
1919
import re
2020
import sys
21+
from typing import Any, Callable, Dict, List, Optional
2122

2223
import em
2324

@@ -31,7 +32,7 @@
3132
from rosidl_parser.parser import parse_idl_file
3233

3334

34-
def convert_camel_case_to_lower_case_underscore(value):
35+
def convert_camel_case_to_lower_case_underscore(value: str) -> str:
3536
# insert an underscore before any upper case letter
3637
# which is followed by a lower case letter
3738
value = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', value)
@@ -41,12 +42,14 @@ def convert_camel_case_to_lower_case_underscore(value):
4142
return value.lower()
4243

4344

44-
def read_generator_arguments(input_file):
45+
def read_generator_arguments(input_file: str) -> Any:
4546
with open(input_file, mode='r', encoding='utf-8') as h:
4647
return json.load(h)
4748

4849

49-
def get_newest_modification_time(target_dependencies):
50+
def get_newest_modification_time(
51+
target_dependencies: List[str]
52+
) -> Optional[float]:
5053
newest_timestamp = None
5154
for dep in target_dependencies:
5255
ts = os.path.getmtime(dep)
@@ -56,9 +59,10 @@ def get_newest_modification_time(target_dependencies):
5659

5760

5861
def generate_files(
59-
generator_arguments_file, mapping, additional_context=None,
60-
keep_case=False, post_process_callback=None
61-
):
62+
generator_arguments_file: str, mapping: Dict[str, str],
63+
additional_context: Optional[Dict[str, bool]] = None,
64+
keep_case: bool = False, post_process_callback: Optional[Callable[[str], str]] = None
65+
) -> List[str]:
6266
args = read_generator_arguments(generator_arguments_file)
6367

6468
template_basepath = pathlib.Path(args['template_dir'])
@@ -67,7 +71,7 @@ def generate_files(
6771
'Could not find template: ' + template_filename
6872

6973
latest_target_timestamp = get_newest_modification_time(args['target_dependencies'])
70-
generated_files = []
74+
generated_files: List[str] = []
7175

7276
type_description_files = {}
7377
for description_tuple in args.get('type_description_tuples', []):
@@ -128,10 +132,10 @@ def generate_files(
128132
return generated_files
129133

130134

131-
template_prefix_path = []
135+
template_prefix_path: List[pathlib.Path] = []
132136

133137

134-
def get_template_path(template_name):
138+
def get_template_path(template_name: str) -> pathlib.Path:
135139
global template_prefix_path
136140
for basepath in template_prefix_path:
137141
template_path = basepath / template_name
@@ -144,14 +148,16 @@ def get_template_path(template_name):
144148

145149

146150
def expand_template(
147-
template_name, data, output_file, minimum_timestamp=None,
148-
template_basepath=None, post_process_callback=None
149-
):
151+
template_name: str, data: Dict[str, Any], output_file: str,
152+
minimum_timestamp: Optional[float] = None,
153+
template_basepath: Optional[pathlib.Path] = None,
154+
post_process_callback: Optional[Callable[[str], str]] = None
155+
) -> None:
150156
# in the legacy API the first argument was the path to the template
151157
if template_basepath is None:
152-
template_name = pathlib.Path(template_name)
153-
template_basepath = template_name.parent
154-
template_name = template_name.name
158+
template_path = pathlib.Path(template_name)
159+
template_basepath = template_path.parent
160+
template_name = template_path.name
155161

156162
global template_prefix_path
157163
template_prefix_path.append(template_basepath)
@@ -226,14 +232,17 @@ def expand_template(
226232
h.write(content)
227233

228234

229-
def _add_helper_functions(data):
235+
def _add_helper_functions(data: Dict[str, Any]) -> None:
230236
data['TEMPLATE'] = _expand_template
231237

232238

233-
def _expand_template(template_name, **kwargs):
239+
def _expand_template(template_name: str, **kwargs: Any) -> None:
234240
global interpreter
235241
template_path = get_template_path(template_name)
236242
_add_helper_functions(kwargs)
243+
if interpreter is None:
244+
raise RuntimeError('_expand_template called before expand_template')
245+
237246
with template_path.open('r') as h:
238247
interpreter.invoke(
239248
'beforeInclude', name=str(template_path), file=h, locals=kwargs)

rosidl_pycommon/test/test_copyright.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818

1919
@pytest.mark.copyright
2020
@pytest.mark.linter
21-
def test_copyright():
21+
def test_copyright() -> None:
2222
rc = main(argv=['.', 'test'])
2323
assert rc == 0, 'Found errors'

rosidl_pycommon/test/test_flake8.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
@pytest.mark.flake8
2020
@pytest.mark.linter
21-
def test_flake8():
21+
def test_flake8() -> None:
2222
rc, errors = main_with_errors(argv=[])
2323
assert rc == 0, \
2424
'Found %d code style errors / warnings:\n' % len(errors) + \

rosidl_pycommon/test/test_mypy.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2024 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 ament_mypy.main import main
16+
import pytest
17+
18+
19+
@pytest.mark.mypy
20+
@pytest.mark.linter
21+
def test_mypy() -> None:
22+
rc = main(argv=[])
23+
assert rc == 0, 'Found type errors!'

rosidl_pycommon/test/test_pep257.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818

1919
@pytest.mark.linter
2020
@pytest.mark.pep257
21-
def test_pep257():
21+
def test_pep257() -> None:
2222
rc = main(argv=['.', 'test'])
2323
assert rc == 0, 'Found code style errors / warnings'

rosidl_typesupport_introspection_c/rosidl_typesupport_introspection_c/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from typing import List
16+
1517
from rosidl_pycommon import generate_files
1618

1719

18-
def generate_c(generator_arguments_file: str):
20+
def generate_c(generator_arguments_file: str) -> List[str]:
1921
"""
2022
Generate the C implementation of the type support.
2123

rosidl_typesupport_introspection_cpp/rosidl_typesupport_introspection_cpp/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from typing import List
16+
1517
from rosidl_pycommon import generate_files
1618

1719

18-
def generate_cpp(generator_arguments_file: str):
20+
def generate_cpp(generator_arguments_file: str) -> List[str]:
1921
"""
2022
Generate the C++ implementation of the type support.
2123

rosidl_typesupport_introspection_cpp/rosidl_typesupport_introspection_cpp/cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import pathlib
16+
from typing import List
1617

1718
from ament_index_python import get_package_share_directory
1819

@@ -32,7 +33,7 @@ def generate(
3233
interface_files,
3334
include_paths,
3435
output_path
35-
):
36+
) -> List[str]:
3637
package_share_path = pathlib.Path(
3738
get_package_share_directory('rosidl_typesupport_introspection_cpp'))
3839

0 commit comments

Comments
 (0)