1111import pytest
1212from jupyter_client .blocking .client import BlockingKernelClient
1313
14- from .utils import TIMEOUT , assemble_output , get_replies , get_reply , new_kernel , flush_channels
14+ from .utils import TIMEOUT , assemble_output , flush_channels , get_replies , get_reply , new_kernel
1515
1616# Helpers
1717
@@ -40,7 +40,9 @@ def list_subshell_helper(kc: BlockingKernelClient):
4040 return reply ["content" ]
4141
4242
43- def execute_request (kc : BlockingKernelClient , code : str , subshell_id : str | None , silent : bool = False ):
43+ def execute_request (
44+ kc : BlockingKernelClient , code : str , subshell_id : str | None , silent : bool = False
45+ ):
4446 msg = kc .session .msg ("execute_request" , {"code" : code , "silent" : silent })
4547 msg ["header" ]["subshell_id" ] = subshell_id
4648 kc .shell_channel .send (msg )
@@ -260,56 +262,65 @@ def test_execute_stop_on_error(are_subshells):
260262 if subshell_id :
261263 delete_subshell_helper (kc , subshell_id )
262264
265+
263266def test_silent_flag_in_subshells ():
264267 """Verifies that the 'silent' flag suppresses output in main and subshell contexts."""
265268 with new_kernel () as kc :
266269 flush_channels (kc )
267270 # Test silent execution in main shell
268271 msg_main_silent = execute_request (kc , "a=1" , None , silent = True )
269272 reply_main_silent = get_reply (kc , msg_main_silent ["header" ]["msg_id" ])
270- assert reply_main_silent [' content' ][ ' status' ] == 'ok'
273+ assert reply_main_silent [" content" ][ " status" ] == "ok"
271274
272275 # Test silent execution in subshell
273276 subshell_id = create_subshell_helper (kc )["subshell_id" ]
274277 msg_sub_silent = execute_request (kc , "b=2" , subshell_id , silent = True )
275278 reply_sub_silent = get_reply (kc , msg_sub_silent ["header" ]["msg_id" ])
276- assert reply_sub_silent [' content' ][ ' status' ] == 'ok'
279+ assert reply_sub_silent [" content" ][ " status" ] == "ok"
277280
278281 # Check for no output from silent requests. We should only see status messages,
279282 # so we expect a timeout here when looking for other messages.
280283 while True :
281284 try :
282285 msg = kc .get_iopub_msg (timeout = 0.2 )
283286 # We should only receive status messages
284- if msg [' header' ][ ' msg_type' ] == ' status' :
287+ if msg [" header" ][ " msg_type" ] == " status" :
285288 continue
286289 # If we get anything else, it's a failure
287- pytest .fail (f"Silent execution produced an unexpected IOPub message: { msg ['header' ]['msg_type' ]} " )
290+ pytest .fail (
291+ f"Silent execution produced an unexpected IOPub message: { msg ['header' ]['msg_type' ]} "
292+ )
288293 except Empty :
289294 # No more messages, which is the expected behavior for silent execution
290295 break
291296
292297 # Test concurrent silent and non-silent execution
293- msg_silent = execute_request (kc , "import time; time.sleep(0.5); c=3" , subshell_id , silent = True )
298+ msg_silent = execute_request (
299+ kc , "import time; time.sleep(0.5); c=3" , subshell_id , silent = True
300+ )
294301 msg_noisy = execute_request (kc , "print('noisy')" , None , silent = False )
295302
296303 # Get replies for both messages
297- replies = get_replies (kc , [msg_silent [' header' ][ ' msg_id' ], msg_noisy [' header' ][ ' msg_id' ]])
304+ replies = get_replies (kc , [msg_silent [" header" ][ " msg_id" ], msg_noisy [" header" ][ " msg_id" ]])
298305 assert len (replies ) == 2
299306
300307 # Verify that we only receive stream output from the noisy message
301- stdout , stderr = assemble_output (kc .get_iopub_msg , parent_msg_id = msg_noisy ['header' ]['msg_id' ])
308+ stdout , stderr = assemble_output (
309+ kc .get_iopub_msg , parent_msg_id = msg_noisy ["header" ]["msg_id" ]
310+ )
302311 assert "noisy" in stdout
303312 assert not stderr
304313
305314 # Verify there is no output from the silent message.
306315 try :
307- stdout , stderr = assemble_output (kc .get_iopub_msg , parent_msg_id = msg_silent ['header' ]['msg_id' ])
316+ stdout , stderr = assemble_output (
317+ kc .get_iopub_msg , parent_msg_id = msg_silent ["header" ]["msg_id" ]
318+ )
308319 assert not stdout
309320 assert not stderr
310321 except Empty :
311322 # This is expected since there should be no output
312323 pass
313324
314325 # Cleanup
315- delete_subshell_helper (kc , subshell_id )
326+ delete_subshell_helper (kc , subshell_id )
0 commit comments