3434
3535import launch .logging
3636
37- from osrf_pycommon .process_utils import async_execute_process
37+ from osrf_pycommon .process_utils import async_execute_process # type: ignore
3838from osrf_pycommon .process_utils import AsyncSubprocessProtocol
3939
4040from .emit_event import EmitEvent
6363from ..launch_context import LaunchContext
6464from ..launch_description import LaunchDescription
6565from ..launch_description_entity import LaunchDescriptionEntity
66- from ..some_actions_type import SomeActionsType
66+ from ..some_entities_type import SomeEntitiesType
6767from ..some_substitutions_type import SomeSubstitutionsType
6868from ..substitution import Substitution # noqa: F401
6969from ..substitutions import LaunchConfiguration
@@ -97,8 +97,8 @@ def __init__(
9797 cached_output : bool = False ,
9898 log_cmd : bool = False ,
9999 on_exit : Optional [Union [
100- SomeActionsType ,
101- Callable [[ProcessExited , LaunchContext ], Optional [SomeActionsType ]]
100+ SomeEntitiesType ,
101+ Callable [[ProcessExited , LaunchContext ], Optional [SomeEntitiesType ]]
102102 ]] = None ,
103103 respawn : Union [bool , SomeSubstitutionsType ] = False ,
104104 respawn_delay : Optional [float ] = None ,
@@ -193,9 +193,16 @@ def __init__(
193193 self .__sigterm_timeout = normalize_to_list_of_substitutions (sigterm_timeout )
194194 self .__sigkill_timeout = normalize_to_list_of_substitutions (sigkill_timeout )
195195 self .__emulate_tty = emulate_tty
196- self .__output = os .environ .get ('OVERRIDE_LAUNCH_PROCESS_OUTPUT' , output )
197- if not isinstance (self .__output , dict ):
198- self .__output = normalize_to_list_of_substitutions (self .__output )
196+ # Note: we need to use a temporary here so that we don't assign values with different types
197+ # to the same variable
198+ tmp_output : SomeSubstitutionsType = os .environ .get (
199+ 'OVERRIDE_LAUNCH_PROCESS_OUTPUT' , output
200+ )
201+ self .__output : Union [dict , List [Substitution ]]
202+ if not isinstance (tmp_output , dict ):
203+ self .__output = normalize_to_list_of_substitutions (tmp_output )
204+ else :
205+ self .__output = tmp_output
199206 self .__output_format = output_format
200207
201208 self .__log_cmd = log_cmd
@@ -336,7 +343,7 @@ def __on_signal_process_event(
336343 def __on_process_stdin (
337344 self ,
338345 event : ProcessIO
339- ) -> Optional [SomeActionsType ]:
346+ ) -> Optional [SomeEntitiesType ]:
340347 self .__logger .warning (
341348 "in ExecuteProcess('{}').__on_process_stdin_event()" .format (id (self )),
342349 )
@@ -345,7 +352,7 @@ def __on_process_stdin(
345352
346353 def __on_process_output (
347354 self , event : ProcessIO , buffer : io .TextIOBase , logger : logging .Logger
348- ) -> Optional [ SomeActionsType ] :
355+ ) -> None :
349356 to_write = event .text .decode (errors = 'replace' )
350357 if buffer .closed :
351358 # buffer was probably closed by __flush_buffers on shutdown. Output without
@@ -396,7 +403,7 @@ def __flush_buffers(self, event, context):
396403
397404 def __on_process_output_cached (
398405 self , event : ProcessIO , buffer , logger
399- ) -> Optional [ SomeActionsType ] :
406+ ) -> None :
400407 to_write = event .text .decode (errors = 'replace' )
401408 last_cursor = buffer .tell ()
402409 buffer .seek (0 , os .SEEK_END ) # go to end of buffer
@@ -423,7 +430,7 @@ def __flush_cached_buffers(self, event, context):
423430 self .__output_format .format (line = line , this = self )
424431 )
425432
426- def __on_shutdown (self , event : Event , context : LaunchContext ) -> Optional [SomeActionsType ]:
433+ def __on_shutdown (self , event : Event , context : LaunchContext ) -> Optional [SomeEntitiesType ]:
427434 due_to_sigint = cast (Shutdown , event ).due_to_sigint
428435 return self ._shutdown_process (
429436 context ,
@@ -578,9 +585,14 @@ async def __execute_process(self, context: LaunchContext) -> None:
578585 self .__logger .error ("process has died [pid {}, exit code {}, cmd '{}']." .format (
579586 pid , returncode , ' ' .join (filter (lambda part : part .strip (), cmd ))
580587 ))
581- await context .emit_event (ProcessExited (returncode = returncode , ** process_event_args ))
588+ await context .emit_event (
589+ ProcessExited (returncode = returncode , ** process_event_args )
590+ )
582591 # respawn the process if necessary
583- if not context .is_shutdown and not self .__shutdown_future .done () and self .__respawn :
592+ if not context .is_shutdown \
593+ and self .__shutdown_future is not None \
594+ and not self .__shutdown_future .done ()\
595+ and self .__respawn :
584596 if self .__respawn_delay is not None and self .__respawn_delay > 0.0 :
585597 # wait for a timeout(`self.__respawn_delay`) to respawn the process
586598 # and handle shutdown event with future(`self.__shutdown_future`)
@@ -608,7 +620,7 @@ def prepare(self, context: LaunchContext):
608620 # pid is added to the dictionary in the connection_made() method of the protocol.
609621 }
610622
611- self .__respawn = perform_typed_substitution (context , self .__respawn , bool )
623+ self .__respawn = cast ( bool , perform_typed_substitution (context , self .__respawn , bool ) )
612624
613625 def execute (self , context : LaunchContext ) -> Optional [List [LaunchDescriptionEntity ]]:
614626 """
@@ -663,7 +675,9 @@ def execute(self, context: LaunchContext) -> Optional[List[LaunchDescriptionEnti
663675 ),
664676 OnProcessExit (
665677 target_action = self ,
666- on_exit = self .__on_exit ,
678+ # TODO: This is also a little strange, OnProcessExit shouldn't ever be able to
679+ # take a None for the callable, but this seems to be the default case?
680+ on_exit = self .__on_exit , # type: ignore
667681 ),
668682 OnProcessExit (
669683 target_action = self ,
@@ -678,9 +692,13 @@ def execute(self, context: LaunchContext) -> Optional[List[LaunchDescriptionEnti
678692 self .__shutdown_future = create_future (context .asyncio_loop )
679693 self .__logger = launch .logging .get_logger (name )
680694 if not isinstance (self .__output , dict ):
681- self .__output = perform_substitutions (context , self .__output )
682- self .__stdout_logger , self .__stderr_logger = \
683- launch .logging .get_output_loggers (name , self .__output )
695+ self .__stdout_logger , self .__stderr_logger = \
696+ launch .logging .get_output_loggers (
697+ name , perform_substitutions (context , self .__output )
698+ )
699+ else :
700+ self .__stdout_logger , self .__stderr_logger = \
701+ launch .logging .get_output_loggers (name , self .__output )
684702 context .asyncio_loop .create_task (self .__execute_process (context ))
685703 except Exception :
686704 for event_handler in event_handlers :
0 commit comments