Skip to content

Commit 8b9cbd7

Browse files
committed
extend argument handling of do_execute with cell metadata
1 parent bb9f350 commit 8b9cbd7

File tree

2 files changed

+37
-26
lines changed

2 files changed

+37
-26
lines changed

ipykernel/ipkernel.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from .debugger import Debugger, _is_debugpy_available
2424
from .eventloops import _use_appnope
2525
from .kernelbase import Kernel as KernelBase
26-
from .kernelbase import _accepts_cell_id
26+
from .kernelbase import _accepts_parameters
2727
from .zmqshell import ZMQInteractiveShell
2828

2929
try:
@@ -347,6 +347,7 @@ async def do_execute(
347347
user_expressions=None,
348348
allow_stdin=False,
349349
*,
350+
cell_meta=None,
350351
cell_id=None,
351352
):
352353
"""Handle code execution."""
@@ -359,7 +360,7 @@ async def do_execute(
359360
if hasattr(shell, "run_cell_async") and hasattr(shell, "should_run_async"):
360361
run_cell = shell.run_cell_async
361362
should_run_async = shell.should_run_async
362-
with_cell_id = _accepts_cell_id(run_cell)
363+
with_cell_id = _accepts_parameters(run_cell, ["cell_id"])
363364
else:
364365
should_run_async = lambda cell: False # noqa
365366
# older IPython,
@@ -368,7 +369,7 @@ async def do_execute(
368369
async def run_cell(*args, **kwargs):
369370
return shell.run_cell(*args, **kwargs)
370371

371-
with_cell_id = _accepts_cell_id(shell.run_cell)
372+
with_cell_id = _accepts_parameters(shell.run_cell, ["cell_id"])
372373
try:
373374
# default case: runner is asyncio and asyncio is already running
374375
# TODO: this should check every case for "are we inside the runner",

ipykernel/kernelbase.py

+33-23
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,18 @@
6363
from ._version import kernel_protocol_version
6464

6565

66-
def _accepts_cell_id(meth):
66+
def _accepts_parameters(meth, param_names):
6767
parameters = inspect.signature(meth).parameters
68-
cid_param = parameters.get("cell_id")
69-
return (cid_param and cid_param.kind == cid_param.KEYWORD_ONLY) or any(
70-
p.kind == p.VAR_KEYWORD for p in parameters.values()
71-
)
68+
accepts = {param: False for param in param_names}
69+
70+
for param in param_names:
71+
param_spec = parameters.get(param)
72+
accepts[param] = (
73+
param_spec
74+
and param_spec.kind in [param_spec.KEYWORD_ONLY, param_spec.POSITIONAL_OR_KEYWORD]
75+
) or any(p.kind == p.VAR_KEYWORD for p in parameters.values())
76+
77+
return accepts
7278

7379

7480
class Kernel(SingletonConfigurable):
@@ -735,25 +741,28 @@ async def execute_request(self, stream, ident, parent):
735741
self.execution_count += 1
736742
self._publish_execute_input(code, parent, self.execution_count)
737743

738-
cell_id = (parent.get("metadata") or {}).get("cellId")
744+
cell_meta = parent.get("metadata", {})
745+
cell_id = metadata.get("cellId")
739746

740-
if _accepts_cell_id(self.do_execute):
741-
reply_content = self.do_execute(
742-
code,
743-
silent,
744-
store_history,
745-
user_expressions,
746-
allow_stdin,
747-
cell_id=cell_id,
748-
)
749-
else:
750-
reply_content = self.do_execute(
751-
code,
752-
silent,
753-
store_history,
754-
user_expressions,
755-
allow_stdin,
756-
)
747+
# Check which parameters do_execute can accept
748+
accepts_params = _accepts_parameters(self.do_execute, ["metadata", "cell_id"])
749+
750+
# Arguments based on the do_execute signature
751+
do_execute_args = {
752+
"code": code,
753+
"silent": silent,
754+
"store_history": store_history,
755+
"user_expressions": user_expressions,
756+
"allow_stdin": allow_stdin,
757+
}
758+
759+
if accepts_params["metadata"]:
760+
do_execute_args["metadata"] = cell_meta
761+
if accepts_params["cell_id"]:
762+
do_execute_args["cell_id"] = cell_id
763+
764+
# Call do_execute with the appropriate arguments
765+
reply_content = self.do_execute(**do_execute_args)
757766

758767
if inspect.isawaitable(reply_content):
759768
reply_content = await reply_content
@@ -793,6 +802,7 @@ def do_execute(
793802
user_expressions=None,
794803
allow_stdin=False,
795804
*,
805+
cell_meta=None,
796806
cell_id=None,
797807
):
798808
"""Execute user code. Must be overridden by subclasses."""

0 commit comments

Comments
 (0)