diff --git a/ipykernel/ipkernel.py b/ipykernel/ipkernel.py index e1505a9a7..7b6f769d2 100644 --- a/ipykernel/ipkernel.py +++ b/ipykernel/ipkernel.py @@ -386,7 +386,7 @@ async def do_execute( if hasattr(shell, "run_cell_async") and hasattr(shell, "should_run_async"): run_cell = shell.run_cell_async should_run_async = shell.should_run_async - accepts_params = _accepts_parameters(run_cell, ["cell_id"]) + accepts_params = _accepts_parameters(run_cell, ["cell_id", "cell_meta"]) else: should_run_async = lambda cell: False # noqa: ARG005, E731 # older IPython, @@ -395,7 +395,7 @@ async def do_execute( async def run_cell(*args, **kwargs): return shell.run_cell(*args, **kwargs) - accepts_params = _accepts_parameters(shell.run_cell, ["cell_id"]) + accepts_params = _accepts_parameters(shell.run_cell, ["cell_id", "cell_meta"]) try: # default case: runner is asyncio and asyncio is already running # TODO: this should check every case for "are we inside the runner", @@ -406,6 +406,11 @@ async def run_cell(*args, **kwargs): except Exception: transformed_cell = code preprocessing_exc_tuple = sys.exc_info() + do_execute_args = {} + if accepts_params["cell_meta"]: + do_execute_args["cell_meta"] = cell_meta + if self._do_exec_accepted_params["cell_id"]: + do_execute_args["cell_id"] = cell_id if ( _asyncio_runner # type:ignore[truthy-bool] @@ -424,7 +429,7 @@ async def run_cell(*args, **kwargs): silent=silent, transformed_cell=transformed_cell, preprocessing_exc_tuple=preprocessing_exc_tuple, - cell_id=cell_id, + **do_execute_args, ) else: coro = run_cell( @@ -433,6 +438,7 @@ async def run_cell(*args, **kwargs): silent=silent, transformed_cell=transformed_cell, preprocessing_exc_tuple=preprocessing_exc_tuple, + **do_execute_args, ) coro_future = asyncio.ensure_future(coro) @@ -454,15 +460,9 @@ async def run_cell(*args, **kwargs): # runner isn't already running, # make synchronous call, # letting shell dispatch to loop runners - if accepts_params["cell_id"]: - res = shell.run_cell( - code, - store_history=store_history, - silent=silent, - cell_id=cell_id, - ) - else: - res = shell.run_cell(code, store_history=store_history, silent=silent) + res = shell.run_cell( + code, store_history=store_history, silent=silent, **do_execute_args + ) finally: self._restore_input() diff --git a/tests/inprocess/test_kernel.py b/tests/inprocess/test_kernel.py index b965d9b89..ad663313a 100644 --- a/tests/inprocess/test_kernel.py +++ b/tests/inprocess/test_kernel.py @@ -119,3 +119,9 @@ async def test_do_execute(kc): kernel = InProcessKernel() await kernel.do_execute("a=1", True) assert kernel.shell.user_ns["a"] == 1 + + +async def test_cell_meta_do_execute(): + kernel: InProcessKernel = InProcessKernel() + await kernel.do_execute("a=1", True, cell_meta={"testkey": "testvalue"}) + assert kernel.shell.user_ns["a"] == 1