diff --git a/ipykernel/kernelbase.py b/ipykernel/kernelbase.py index 84db8660..99358f9b 100644 --- a/ipykernel/kernelbase.py +++ b/ipykernel/kernelbase.py @@ -18,7 +18,7 @@ from datetime import datetime from signal import SIGINT, SIGTERM, Signals -from .control import CONTROL_THREAD_NAME +from .thread import CONTROL_THREAD_NAME if sys.platform != "win32": from signal import SIGKILL @@ -898,7 +898,7 @@ def kernel_info(self): "supported_features": [], } if self._supports_kernel_subshells: - info["supported_features"].append("kernel subshells") + info["supported_features"] = ["kernel subshells"] return info async def kernel_info_request(self, socket, ident, parent): @@ -1074,7 +1074,7 @@ async def do_debug_request(self, msg): # Subshell control message handlers # --------------------------------------------------------------------------- - async def create_subshell_request(self, socket, ident, parent): + async def create_subshell_request(self, socket, ident, parent) -> None: if not self.session: return if not self._supports_kernel_subshells: @@ -1089,7 +1089,7 @@ async def create_subshell_request(self, socket, ident, parent): self.session.send(socket, "create_subshell_reply", reply, parent, ident) - async def delete_subshell_request(self, socket, ident, parent): + async def delete_subshell_request(self, socket, ident, parent) -> None: if not self.session: return if not self._supports_kernel_subshells: @@ -1111,7 +1111,7 @@ async def delete_subshell_request(self, socket, ident, parent): self.session.send(socket, "delete_subshell_reply", reply, parent, ident) - async def list_subshell_request(self, socket, ident, parent): + async def list_subshell_request(self, socket, ident, parent) -> None: if not self.session: return if not self._supports_kernel_subshells: diff --git a/ipykernel/shellchannel.py b/ipykernel/shellchannel.py index 1935f062..bc0459c4 100644 --- a/ipykernel/shellchannel.py +++ b/ipykernel/shellchannel.py @@ -1,4 +1,6 @@ """A thread for a shell channel.""" +import zmq.asyncio + from .subshell_manager import SubshellManager from .thread import SHELL_CHANNEL_THREAD_NAME, BaseThread @@ -9,7 +11,7 @@ class ShellChannelThread(BaseThread): Communicates with shell/subshell threads via pairs of ZMQ inproc sockets. """ - def __init__(self, context, shell_socket, **kwargs): + def __init__(self, context: zmq.asyncio.Context, shell_socket: zmq.asyncio.Socket, **kwargs): """Initialize the thread.""" super().__init__(name=SHELL_CHANNEL_THREAD_NAME, **kwargs) self._manager: SubshellManager | None = None @@ -17,13 +19,13 @@ def __init__(self, context, shell_socket, **kwargs): self._shell_socket = shell_socket @property - def manager(self): + def manager(self) -> SubshellManager: # Lazy initialisation. if self._manager is None: self._manager = SubshellManager(self._context, self._shell_socket) return self._manager - def run(self): + def run(self) -> None: """Run the thread.""" try: super().run() diff --git a/ipykernel/subshell.py b/ipykernel/subshell.py index 19aa66c4..18e15ab3 100644 --- a/ipykernel/subshell.py +++ b/ipykernel/subshell.py @@ -17,7 +17,7 @@ def __init__(self, subshell_id: str, **kwargs): # Inproc PAIR socket, for communication with shell channel thread. self._pair_socket: zmq.asyncio.Socket | None = None - async def create_pair_socket(self, context: zmq.asyncio.Context, address: str): + async def create_pair_socket(self, context: zmq.asyncio.Context, address: str) -> None: """Create inproc PAIR socket, for communication with shell channel thread. Should be called from this thread, so usually via add_task before the @@ -27,7 +27,7 @@ async def create_pair_socket(self, context: zmq.asyncio.Context, address: str): self._pair_socket = context.socket(zmq.PAIR) self._pair_socket.connect(address) - def run(self): + def run(self) -> None: try: super().run() finally: diff --git a/ipykernel/subshell_manager.py b/ipykernel/subshell_manager.py index 3e4a9fd9..805d6f81 100644 --- a/ipykernel/subshell_manager.py +++ b/ipykernel/subshell_manager.py @@ -116,7 +116,7 @@ def list_subshell(self) -> list[str]: with self._lock_cache: return list(self._cache) - async def listen_from_control(self, subshell_task) -> None: + async def listen_from_control(self, subshell_task: t.Any) -> None: """Listen for messages on the control inproc socket, handle those messages and return replies on the same socket. Runs in the shell channel thread. """ @@ -141,7 +141,7 @@ async def listen_from_subshells(self) -> None: async for subshell_id in self._receive_stream: tg.start_soon(self._listen_for_subshell_reply, subshell_id) - def subshell_id_from_thread_id(self, thread_id) -> str | None: + def subshell_id_from_thread_id(self, thread_id: int) -> str | None: """Return subshell_id of the specified thread_id. Raises RuntimeError if thread_id is not the main shell or a subshell. @@ -169,7 +169,7 @@ def _create_inproc_pair_socket( socket.connect(address) return socket - async def _create_subshell(self, subshell_task) -> str: + async def _create_subshell(self, subshell_task: t.Any) -> str: """Create and start a new subshell thread.""" assert current_thread().name == SHELL_CHANNEL_THREAD_NAME @@ -241,7 +241,9 @@ async def _listen_for_subshell_reply(self, subshell_id: str | None) -> None: return raise - async def _process_control_request(self, request, subshell_task) -> dict[str, t.Any]: + async def _process_control_request( + self, request: dict[str, t.Any], subshell_task: t.Any + ) -> dict[str, t.Any]: """Process a control request message received on the control inproc socket and return the reply. Runs in the shell channel thread. """ diff --git a/ipykernel/thread.py b/ipykernel/thread.py index 7efcabce..a63011de 100644 --- a/ipykernel/thread.py +++ b/ipykernel/thread.py @@ -17,9 +17,9 @@ def __init__(self, **kwargs): self.pydev_do_not_trace = True self.is_pydev_daemon_thread = True self.__stop = Event() - self._tasks_and_args: t.List[t.Tuple[t.Callable, t.Tuple]] = [] + self._tasks_and_args: t.List[t.Tuple[t.Any, t.Any]] = [] - def add_task(self, task: t.Callable, *args: t.Tuple): + def add_task(self, task: t.Any, *args: t.Any) -> None: # May only add tasks before the thread is started. self._tasks_and_args.append((task, args)) @@ -34,7 +34,7 @@ async def _main(self) -> None: await to_thread.run_sync(self.__stop.wait) tg.cancel_scope.cancel() - def stop(self): + def stop(self) -> None: """Stop the thread. This method is threadsafe.