@@ -195,7 +195,8 @@ async def _async_wait_for_ready(self, timeout: t.Optional[float] = None) -> None
195195 break
196196
197197 if not await self ._async_is_alive ():
198- raise RuntimeError ("Kernel died before replying to kernel_info" )
198+ msg = "Kernel died before replying to kernel_info"
199+ raise RuntimeError (msg )
199200
200201 # Check if current time is ready check time plus timeout
201202 if time .time () > abs_timeout :
@@ -223,7 +224,8 @@ async def _async_recv_reply(
223224 else :
224225 reply = await self ._async_get_shell_msg (timeout = timeout )
225226 except Empty as e :
226- raise TimeoutError ("Timeout waiting for reply" ) from e
227+ msg = "Timeout waiting for reply"
228+ raise TimeoutError (msg ) from e
227229 if reply ["parent_header" ].get ("msg_id" ) != msg_id :
228230 # not my reply, someone may have forgotten to retrieve theirs
229231 continue
@@ -232,13 +234,10 @@ async def _async_recv_reply(
232234 async def _stdin_hook_default (self , msg : t .Dict [str , t .Any ]) -> None :
233235 """Handle an input request"""
234236 content = msg ["content" ]
235- if content .get ("password" , False ):
236- prompt = getpass
237- else :
238- prompt = input # type: ignore
237+ prompt = getpass if content .get ("password" , False ) else input
239238
240239 try :
241- raw_data = prompt (content ["prompt" ])
240+ raw_data = prompt (content ["prompt" ]) # type:ignore[operator]
242241 except EOFError :
243242 # turn EOFError into EOF character
244243 raw_data = "\x04 "
@@ -475,11 +474,13 @@ async def _async_execute_interactive(
475474 The reply message for this request
476475 """
477476 if not self .iopub_channel .is_alive ():
478- raise RuntimeError ("IOPub channel must be running to receive output" )
477+ emsg = "IOPub channel must be running to receive output"
478+ raise RuntimeError (emsg )
479479 if allow_stdin is None :
480480 allow_stdin = self .allow_stdin
481481 if allow_stdin and not self .stdin_channel .is_alive ():
482- raise RuntimeError ("stdin channel must be running to allow input" )
482+ emsg = "stdin channel must be running to allow input"
483+ raise RuntimeError (emsg )
483484 msg_id = await ensure_async (
484485 self .execute (
485486 code ,
@@ -492,20 +493,19 @@ async def _async_execute_interactive(
492493 )
493494 if stdin_hook is None :
494495 stdin_hook = self ._stdin_hook_default
495- if output_hook is None :
496- # detect IPython kernel
497- if "IPython" in sys .modules :
498- from IPython import get_ipython
499-
500- ip = get_ipython ()
501- in_kernel = getattr (ip , "kernel" , False )
502- if in_kernel :
503- output_hook = partial (
504- self ._output_hook_kernel ,
505- ip .display_pub .session ,
506- ip .display_pub .pub_socket ,
507- ip .display_pub .parent_header ,
508- )
496+ # detect IPython kernel
497+ if output_hook is None and "IPython" in sys .modules :
498+ from IPython import get_ipython
499+
500+ ip = get_ipython ()
501+ in_kernel = getattr (ip , "kernel" , False )
502+ if in_kernel :
503+ output_hook = partial (
504+ self ._output_hook_kernel ,
505+ ip .display_pub .session ,
506+ ip .display_pub .pub_socket ,
507+ ip .display_pub .parent_header ,
508+ )
509509 if output_hook is None :
510510 # default: redisplay plain-text outputs
511511 output_hook = self ._output_hook_default
@@ -532,7 +532,8 @@ async def _async_execute_interactive(
532532 timeout_ms = int (1000 * timeout )
533533 events = dict (poller .poll (timeout_ms ))
534534 if not events :
535- raise TimeoutError ("Timeout waiting for output" )
535+ emsg = "Timeout waiting for output"
536+ raise TimeoutError (emsg )
536537 if stdin_socket in events :
537538 req = await ensure_async (self .stdin_channel .get_msg (timeout = 0 ))
538539 res = stdin_hook (req )
@@ -747,10 +748,7 @@ def comm_info(self, target_name: t.Optional[str] = None) -> str:
747748 -------
748749 The msg_id of the message sent
749750 """
750- if target_name is None :
751- content = {}
752- else :
753- content = {"target_name" : target_name }
751+ content = {} if target_name is None else {"target_name" : target_name }
754752 msg = self .session .msg ("comm_info_request" , content )
755753 self .shell_channel .send (msg )
756754 return msg ["header" ]["msg_id" ]
0 commit comments