From 876148ec19cc4598c81f347c93ccfe6cfc20af1b Mon Sep 17 00:00:00 2001 From: David Brochart Date: Mon, 23 Oct 2023 16:46:15 +0200 Subject: [PATCH] Fix types --- ipykernel/debugger.py | 6 +++--- ipykernel/inprocess/ipkernel.py | 4 ++-- ipykernel/inprocess/manager.py | 15 +++++++++------ ipykernel/inprocess/socket.py | 5 +++-- ipykernel/kernelapp.py | 2 +- ipykernel/kernelbase.py | 10 +++++++++- 6 files changed, 27 insertions(+), 15 deletions(-) diff --git a/ipykernel/debugger.py b/ipykernel/debugger.py index 509f0f26..ef7ba847 100644 --- a/ipykernel/debugger.py +++ b/ipykernel/debugger.py @@ -4,6 +4,7 @@ import sys import typing as t from math import inf +from typing import Any import zmq from anyio import Event, create_memory_object_stream @@ -116,7 +117,7 @@ def __init__(self, event_callback, log): self.tcp_buffer = "" self._reset_tcp_pos() self.event_callback = event_callback - self.message_send_stream, self.message_receive_stream = create_memory_object_stream( + self.message_send_stream, self.message_receive_stream = create_memory_object_stream[dict[str, Any]]( max_buffer_size=inf ) self.log = log @@ -287,7 +288,6 @@ def disconnect_tcp_socket(self): """Disconnect from the tcp socket.""" self.debugpy_socket.disconnect(self._get_endpoint()) self.routing_id = None - self.init_event = Event() self.init_event_seq = -1 self.wait_for_attach = True @@ -343,7 +343,7 @@ def __init__( self.is_started = False self.event_callback = event_callback self.just_my_code = just_my_code - self.stopped_send_stream, self.stopped_receive_stream = create_memory_object_stream( + self.stopped_send_stream, self.stopped_receive_stream = create_memory_object_stream[dict[str, Any]]( max_buffer_size=inf ) diff --git a/ipykernel/inprocess/ipkernel.py b/ipykernel/inprocess/ipkernel.py index 087b323a..faf3caa5 100644 --- a/ipykernel/inprocess/ipkernel.py +++ b/ipykernel/inprocess/ipkernel.py @@ -49,11 +49,11 @@ class InProcessKernel(IPythonKernel): # Kernel interface # ------------------------------------------------------------------------- - shell_class = Type(allow_none=True) + shell_class = Type(allow_none=True) # type:ignore[assignment] _underlying_iopub_socket = Instance(DummySocket, (False,)) iopub_thread: IOPubThread = Instance(IOPubThread) # type:ignore[assignment] - shell_socket = Instance(DummySocket, (True,)) + shell_socket = Instance(DummySocket, (True,)) # type:ignore[arg-type] @default("iopub_thread") def _default_iopub_thread(self): diff --git a/ipykernel/inprocess/manager.py b/ipykernel/inprocess/manager.py index 04c718c6..3169482c 100644 --- a/ipykernel/inprocess/manager.py +++ b/ipykernel/inprocess/manager.py @@ -3,6 +3,8 @@ # Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. +from typing import Any + from anyio import TASK_STATUS_IGNORED from anyio.abc import TaskStatus from jupyter_client.manager import KernelManager @@ -43,7 +45,7 @@ def _default_session(self): # Kernel management methods # -------------------------------------------------------------------------- - async def start_kernel(self, *, task_status: TaskStatus = TASK_STATUS_IGNORED, **kwds) -> None: + async def start_kernel(self, *, task_status: TaskStatus = TASK_STATUS_IGNORED, **kwds: Any) -> None: # type: ignore[explicit-override, override] """Start the kernel.""" from ipykernel.inprocess.ipkernel import InProcessKernel @@ -56,20 +58,21 @@ def shutdown_kernel(self): self.kernel.iopub_thread.stop() self._kill_kernel() - async def restart_kernel( - self, now=False, *, task_status: TaskStatus = TASK_STATUS_IGNORED, **kwds + async def restart_kernel( # type: ignore[explicit-override, override] + self, now: bool = False, newports: bool = False, *, task_status: TaskStatus = TASK_STATUS_IGNORED, **kw: Any ) -> None: """Restart the kernel.""" self.shutdown_kernel() - await self.start_kernel(task_status=task_status, **kwds) + await self.start_kernel(task_status=task_status, **kw) @property def has_kernel(self): return self.kernel is not None def _kill_kernel(self): - self.kernel.stop() - self.kernel = None + if self.kernel: + self.kernel.stop() + self.kernel = None def interrupt_kernel(self): """Interrupt the kernel.""" diff --git a/ipykernel/inprocess/socket.py b/ipykernel/inprocess/socket.py index 3e79297c..aa75d38c 100644 --- a/ipykernel/inprocess/socket.py +++ b/ipykernel/inprocess/socket.py @@ -4,6 +4,7 @@ # Distributed under the terms of the Modified BSD License. from math import inf +from typing import Any import zmq from anyio import create_memory_object_stream @@ -31,10 +32,10 @@ def __init__(self, is_shell, *args, **kwargs): self.is_shell = is_shell self.on_recv = None if is_shell: - self.in_send_stream, self.in_receive_stream = create_memory_object_stream( + self.in_send_stream, self.in_receive_stream = create_memory_object_stream[dict[str, Any]]( max_buffer_size=inf ) - self.out_send_stream, self.out_receive_stream = create_memory_object_stream( + self.out_send_stream, self.out_receive_stream = create_memory_object_stream[dict[str, Any]]( max_buffer_size=inf ) diff --git a/ipykernel/kernelapp.py b/ipykernel/kernelapp.py index adf03ac3..7b2a1db0 100644 --- a/ipykernel/kernelapp.py +++ b/ipykernel/kernelapp.py @@ -550,7 +550,7 @@ def init_signal(self): def init_kernel(self): """Create the Kernel object itself""" - kernel_factory = self.kernel_class.instance + kernel_factory = self.kernel_class.instance # type:ignore[attr-defined] kernel = kernel_factory( parent=self, diff --git a/ipykernel/kernelbase.py b/ipykernel/kernelbase.py index d1ea79fc..f780c6dd 100644 --- a/ipykernel/kernelbase.py +++ b/ipykernel/kernelbase.py @@ -314,6 +314,9 @@ async def process_shell(self): raise e async def process_shell_message(self, msg=None): + assert self.shell_socket is not None + assert self.session is not None + no_msg = msg is None if self._is_test else not await self.shell_socket.poll(0) msg = msg or await self.shell_socket.recv_multipart() @@ -398,6 +401,9 @@ async def process_control(self): raise e async def process_control_message(self, msg=None): + assert self.control_socket is not None + assert self.session is not None + msg = msg or await self.control_socket.recv_multipart() idents, msg = self.session.feed_identities(msg, copy=True) try: @@ -666,6 +672,7 @@ async def execute_request(self, socket, ident, parent): self.log.debug("%s", reply_msg) + assert reply_msg is not None if not silent and reply_msg["content"]["status"] == "error" and stop_on_error: # while this flag is true, # execute requests will be aborted @@ -768,7 +775,7 @@ async def connect_request(self, socket, ident, parent): """Handle a connect request.""" if not self.session: return - content = self._recorded_ports.copy() if self._recorded_ports is not None else {} + content = self._recorded_ports.copy() if self._recorded_ports else {} content["status"] = "ok" msg = self.session.send(socket, "connect_reply", content, parent, ident) self.log.debug("%s", msg) @@ -1049,6 +1056,7 @@ async def _send_abort_reply(self, socket, msg, idents): md = self.finish_metadata(msg, md, status) md.update(status) + assert self.session is not None self.session.send( socket, reply_type,