1313# limitations under the License.
1414
1515import argparse
16- import pathlib
16+ from pathlib import Path
1717import sys
18+ from typing import Callable , List , Literal , TYPE_CHECKING
1819
1920from catkin_pkg .package import package_exists_at
2021from catkin_pkg .package import parse_package
2728from rosidl_cli .command .translate .extensions import TranslateCommandExtension
2829
2930
30- def convert_files_to_idl (extension , conversion_function , argv = sys .argv [1 :]):
31+ if TYPE_CHECKING :
32+ from typing_extensions import TypeAlias
33+
34+ ConversionFunctionType : TypeAlias = Callable [[Path , str , Path , Path ], Path ]
35+
36+
37+ def convert_files_to_idl (
38+ extension : Literal ['.msg' , '.srv' , '.action' ],
39+ conversion_function : 'ConversionFunctionType' ,
40+ argv : List [str ] = sys .argv [1 :]
41+ ) -> None :
42+
3143 parser = argparse .ArgumentParser (
3244 description = f'Convert { extension } files to .idl' )
3345 parser .add_argument (
@@ -36,7 +48,7 @@ def convert_files_to_idl(extension, conversion_function, argv=sys.argv[1:]):
3648 args = parser .parse_args (argv )
3749
3850 for interface_file in args .interface_files :
39- interface_file = pathlib . Path (interface_file )
51+ interface_file = Path (interface_file )
4052 package_dir = interface_file .parent .absolute ()
4153 while (
4254 len (package_dir .parents ) and
@@ -48,8 +60,7 @@ def convert_files_to_idl(extension, conversion_function, argv=sys.argv[1:]):
4860 f"Could not find package for '{ interface_file } '" ,
4961 file = sys .stderr )
5062 continue
51- warnings = []
52- pkg = parse_package (package_dir , warnings = warnings )
63+ pkg = parse_package (package_dir , warnings = [])
5364
5465 conversion_function (
5566 package_dir , pkg .name ,
@@ -63,14 +74,14 @@ class TranslateToIDL(TranslateCommandExtension):
6374
6475 def translate (
6576 self ,
66- package_name ,
67- interface_files ,
68- include_paths ,
69- output_path
70- ):
77+ package_name : str ,
78+ interface_files : List [ str ] ,
79+ include_paths : List [ str ] ,
80+ output_path : Path
81+ ) -> List [ str ] :
7182 translated_interface_files = []
72- for interface_file in interface_files :
73- prefix , interface_file = interface_path_as_tuple (interface_file )
83+ for interface_file_str in interface_files :
84+ prefix , interface_file = interface_path_as_tuple (interface_file_str )
7485 output_dir = output_path / interface_file .parent
7586 translated_interface_file = self .conversion_function (
7687 prefix , package_name , interface_file , output_dir )
@@ -87,7 +98,7 @@ class TranslateMsgToIDL(TranslateToIDL):
8798 input_format = 'msg'
8899
89100 @property
90- def conversion_function (self ):
101+ def conversion_function (self ) -> 'ConversionFunctionType' :
91102 return convert_msg_to_idl
92103
93104
@@ -96,13 +107,13 @@ class TranslateSrvToIDL(TranslateToIDL):
96107 input_format = 'srv'
97108
98109 @property
99- def conversion_function (self ):
110+ def conversion_function (self ) -> 'ConversionFunctionType' :
100111 return convert_srv_to_idl
101112
102113
103114class TranslateActionToIDL (TranslateToIDL ):
104115 input_format = 'action'
105116
106117 @property
107- def conversion_function (self ):
118+ def conversion_function (self ) -> 'ConversionFunctionType' :
108119 return convert_action_to_idl
0 commit comments