1414from queue import Empty , Queue
1515from threading import Thread
1616from time import monotonic
17+ from turtle import st
18+ from types import CoroutineType , coroutine
1719from typing import TYPE_CHECKING , Any , Optional , cast
1820
1921import websocket
@@ -893,8 +895,8 @@ async def _maybe_awaitable(self, func_result):
893895 async def _handle_iopub_stdin_messages (
894896 self ,
895897 msg_id : str ,
896- output_hook : t .Callable ,
897- stdin_hook : t .Callable ,
898+ output_hook : t .Optional [ t . Callable [[ dict [ str , t . Any ]], t . Any ]] ,
899+ stdin_hook : t .Optional [ t . Callable [[ dict [ str , t . Any ]], t . Any ]] ,
898900 timeout : t .Optional [float ],
899901 allow_stdin : bool ,
900902 start_time : float ,
@@ -909,7 +911,8 @@ async def _handle_iopub_stdin_messages(
909911 raise TimeoutError ("Timeout in IOPub handling" )
910912 else :
911913 remaining = None
912- await self ._handle_stdin_messages (stdin_hook , allow_stdin )
914+ if stdin_hook is not None and allow_stdin :
915+ await self ._handle_stdin_messages (stdin_hook , allow_stdin )
913916 try :
914917 msg = await self .iopub_channel .get_msg (timeout = remaining )
915918 except Exception as e :
@@ -918,7 +921,8 @@ async def _handle_iopub_stdin_messages(
918921 if msg ["parent_header" ].get ("msg_id" ) != msg_id :
919922 continue
920923
921- await self ._maybe_awaitable (output_hook (msg ))
924+ if output_hook is not None :
925+ await self ._maybe_awaitable (output_hook (msg ))
922926
923927 if (
924928 msg ["header" ]["msg_type" ] == "status"
@@ -928,7 +932,7 @@ async def _handle_iopub_stdin_messages(
928932
929933 async def _handle_stdin_messages (
930934 self ,
931- stdin_hook : t .Callable ,
935+ stdin_hook : t .Callable [[ dict [ str , t . Any ]], t . Any ] ,
932936 allow_stdin : bool ,
933937 ) -> None :
934938 """Handle stdin messages until iopub is idle"""
@@ -990,7 +994,7 @@ async def _wait_for_execution_reply(
990994
991995 for task in done :
992996 try :
993- msg = task .result ()
997+ msg : dict [ str , t . Any ] = task .result ()
994998 if msg ["parent_header" ].get ("msg_id" ) == msg_id :
995999 return msg
9961000 except Exception :
@@ -1010,9 +1014,9 @@ async def execute_interactive(
10101014 allow_stdin : t .Optional [bool ] = None ,
10111015 stop_on_error : bool = True ,
10121016 timeout : t .Optional [float ] = None ,
1013- output_hook : t .Optional [t .Callable [[dict ], t .Any ]] = None ,
1014- stdin_hook : t .Optional [t .Callable [[dict ], t .Any ]] = None ,
1015- ) -> dict [str , t .Any ]:
1017+ output_hook : t .Optional [t .Callable [[dict [ str , t . Any ] ], t .Any ]] = None ,
1018+ stdin_hook : t .Optional [t .Callable [[dict [ str , t . Any ] ], t .Any ]] = None ,
1019+ ) -> dict [str , t .Any ]: # type: ignore[override] # Reason: base class sets `execute_interactive` via assignment, so mypy cannot infer override compatibility
10161020 """Execute code in the kernel interactively via gateway"""
10171021
10181022 # Channel alive checks
0 commit comments