Skip to content

Commit b287255

Browse files
erawnpre-commit-ci[bot]ianthomas23
authored
pass cell_meta from do_execute to run_cell (#1475)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Ian Thomas <ianthomas23@gmail.com>
1 parent 770afbe commit b287255

File tree

3 files changed

+26
-29
lines changed

3 files changed

+26
-29
lines changed

ipykernel/ipkernel.py

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ async def do_execute(
392392
if hasattr(shell, "run_cell_async") and hasattr(shell, "should_run_async"):
393393
run_cell = shell.run_cell_async
394394
should_run_async = shell.should_run_async
395-
accepts_params = _accepts_parameters(run_cell, ["cell_id"])
395+
accepts_params = _accepts_parameters(run_cell, ["cell_id", "cell_meta"])
396396
else:
397397
should_run_async = lambda cell: False # noqa: ARG005, E731
398398
# older IPython,
@@ -401,7 +401,7 @@ async def do_execute(
401401
async def run_cell(*args, **kwargs):
402402
return shell.run_cell(*args, **kwargs)
403403

404-
accepts_params = _accepts_parameters(shell.run_cell, ["cell_id"])
404+
accepts_params = _accepts_parameters(shell.run_cell, ["cell_id", "cell_meta"])
405405
try:
406406
# default case: runner is asyncio and asyncio is already running
407407
# TODO: this should check every case for "are we inside the runner",
@@ -413,6 +413,12 @@ async def run_cell(*args, **kwargs):
413413
transformed_cell = code
414414
preprocessing_exc_tuple = sys.exc_info()
415415

416+
do_execute_args = {}
417+
if accepts_params["cell_meta"]:
418+
do_execute_args["cell_meta"] = cell_meta
419+
if accepts_params["cell_id"]:
420+
do_execute_args["cell_id"] = cell_id
421+
416422
if (
417423
_asyncio_runner # type:ignore[truthy-bool]
418424
and shell.loop_runner is _asyncio_runner
@@ -423,23 +429,14 @@ async def run_cell(*args, **kwargs):
423429
preprocessing_exc_tuple=preprocessing_exc_tuple,
424430
)
425431
):
426-
if accepts_params["cell_id"]:
427-
coro = run_cell(
428-
code,
429-
store_history=store_history,
430-
silent=silent,
431-
transformed_cell=transformed_cell,
432-
preprocessing_exc_tuple=preprocessing_exc_tuple,
433-
cell_id=cell_id,
434-
)
435-
else:
436-
coro = run_cell(
437-
code,
438-
store_history=store_history,
439-
silent=silent,
440-
transformed_cell=transformed_cell,
441-
preprocessing_exc_tuple=preprocessing_exc_tuple,
442-
)
432+
coro = run_cell(
433+
code,
434+
store_history=store_history,
435+
silent=silent,
436+
transformed_cell=transformed_cell,
437+
preprocessing_exc_tuple=preprocessing_exc_tuple,
438+
**do_execute_args,
439+
)
443440

444441
coro_future = asyncio.ensure_future(coro)
445442

@@ -460,15 +457,9 @@ async def run_cell(*args, **kwargs):
460457
# runner isn't already running,
461458
# make synchronous call,
462459
# letting shell dispatch to loop runners
463-
if accepts_params["cell_id"]:
464-
res = shell.run_cell(
465-
code,
466-
store_history=store_history,
467-
silent=silent,
468-
cell_id=cell_id,
469-
)
470-
else:
471-
res = shell.run_cell(code, store_history=store_history, silent=silent)
460+
res = shell.run_cell(
461+
code, store_history=store_history, silent=silent, **do_execute_args
462+
)
472463
finally:
473464
self._restore_input()
474465

ipykernel/kernelbase.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ async def execute_request(self, stream, ident, parent):
794794
store_history = content.get("store_history", not silent)
795795
user_expressions = content.get("user_expressions", {})
796796
allow_stdin = content.get("allow_stdin", False)
797-
cell_meta = parent.get("metadata", {})
797+
cell_meta = parent.get("metadata", None)
798798
cell_id = cell_meta.get("cellId")
799799
except Exception:
800800
self.log.error("Got bad msg: ")

tests/inprocess/test_kernel.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,9 @@ async def test_do_execute(kc):
119119
kernel = InProcessKernel()
120120
await kernel.do_execute("a=1", True)
121121
assert kernel.shell.user_ns["a"] == 1
122+
123+
124+
async def test_cell_meta_do_execute():
125+
kernel: InProcessKernel = InProcessKernel()
126+
await kernel.do_execute("a=1", True, cell_meta={"testkey": "testvalue"})
127+
assert kernel.shell.user_ns["a"] == 1

0 commit comments

Comments
 (0)