Skip to content

Commit

Permalink
Misc cleanup and types updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Carreau committed Oct 15, 2024
1 parent 0bc51f2 commit fda47a0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
5 changes: 3 additions & 2 deletions ipykernel/kernelapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from io import FileIO, TextIOWrapper
from logging import StreamHandler
from pathlib import Path
from threading import Thread

import zmq
import zmq.asyncio
Expand Down Expand Up @@ -141,8 +142,8 @@ class IPKernelApp(BaseIPythonApplication, InteractiveShellApp, ConnectionFileMix
debug_shell_socket = Any()
stdin_socket = Any()
iopub_socket = Any()
iopub_thread = Any()
control_thread = Any()
iopub_thread: Thread
control_thread: Thread

_ports = Dict()

Expand Down
59 changes: 50 additions & 9 deletions ipykernel/kernelbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import queue
import sys
import threading
from threading import Thread
import time
import typing as t
import uuid
Expand Down Expand Up @@ -96,16 +97,17 @@ class Kernel(SingletonConfigurable):
implementation_version: str
banner: str

_is_test = Bool(False)
_is_test: bool

control_socket = Instance(zmq.asyncio.Socket, allow_none=True)
control_tasks: t.Any = List()

debug_shell_socket = Any()

control_thread = Any()
control_thread: Thread
iopub_thread = Thread

iopub_socket = Any()
iopub_thread = Any()
stdin_socket = Any()
log: logging.Logger = Instance(logging.Logger, allow_none=True) # type:ignore[assignment]

Expand Down Expand Up @@ -217,6 +219,9 @@ def _parent_header(self):
"shutdown_request",
"is_complete_request",
"interrupt_request",
# I have the impression that there is amix/match debug_request and do_debug_request
# former was from ipyparallel but is marked as deprecated, but now call do_debug_request
# "do_debug_request"
# deprecated:
"apply_request",
]
Expand Down Expand Up @@ -253,6 +258,17 @@ def __init__(self, **kwargs):
self.do_execute, ["cell_meta", "cell_id"]
)

if not inspect.iscoroutinefunction(self.do_debug_request):
# warning at init time, as we want to warn early enough, even if the
# function is not called
warnings.warn(
"`do_debug_request` will be required to be a coroutine "
"functions in the future. coroutine functions have ben supported "
"since ipykernel 6.0 (2021)",
DeprecationWarning,
stacklevel=2,
)

async def process_control(self):
try:
while True:
Expand Down Expand Up @@ -915,12 +931,29 @@ def do_is_complete(self, code):

async def debug_request(self, socket, ident, parent):
"""Handle a debug request."""
# If this is incorrect, remove `debug_request` from the deprecated message types
# at the beginning of this class
self.log.warning(
"abort_request is deprecated in kernel_base since ipykernel 6.10"
" (2022). It is only part of IPython parallel. Did you mean do_debug_request ?",
DeprecationWarning,
)
if not self.session:
return
content = parent["content"]
reply_content = self.do_debug_request(content)
if inspect.isawaitable(reply_content):
reply_content = await reply_content
if not inspect.iscoroutinefunction(self.do_debug_request):
# repeat the warning at run
reply_content = self.do_debug_request(content)
warnings.warn(
"`do_debug_request` will be required to be a coroutine "
"functions in the future. coroutine functions have ben supported "
"since ipykernel 6.0 (2021)",
DeprecationWarning,
)
if inspect.isawaitable(reply_content):
reply_content = await reply_content
else:
reply_content = await self.do_debug_request(content)
reply_content = json_clean(reply_content)
reply_msg = self.session.send(socket, "debug_reply", reply_content, parent, ident)
self.log.debug("%s", reply_msg)
Expand Down Expand Up @@ -983,7 +1016,11 @@ async def do_debug_request(self, msg):

async def apply_request(self, socket, ident, parent): # pragma: no cover
"""Handle an apply request."""
self.log.warning("apply_request is deprecated in kernel_base, moving to ipyparallel.")
self.log.warning(
"apply_request is deprecated in kernel_base since"
" IPykernel 6.10 (2022) , moving to ipyparallel.",
DeprecationWarning,
)
try:
content = parent["content"]
bufs = parent["buffers"]
Expand Down Expand Up @@ -1024,7 +1061,9 @@ def do_apply(self, content, bufs, msg_id, reply_metadata):
async def abort_request(self, socket, ident, parent): # pragma: no cover
"""abort a specific msg by id"""
self.log.warning(
"abort_request is deprecated in kernel_base. It is only part of IPython parallel"
"abort_request is deprecated in kernel_base since ipykernel 6.10"
" (2022). It is only part of IPython parallel",
DeprecationWarning,
)
msg_ids = parent["content"].get("msg_ids", None)
if isinstance(msg_ids, str):
Expand All @@ -1043,7 +1082,9 @@ async def abort_request(self, socket, ident, parent): # pragma: no cover
async def clear_request(self, socket, idents, parent): # pragma: no cover
"""Clear our namespace."""
self.log.warning(
"clear_request is deprecated in kernel_base. It is only part of IPython parallel"
"clear_request is deprecated in kernel_base since IPykernel 6.10"
" (2022). It is only part of IPython parallel",
DeprecationWarning,
)
content = self.do_clear()
if self.session:
Expand Down

0 comments on commit fda47a0

Please sign in to comment.