Skip to content

Commit

Permalink
Fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Oct 23, 2023
1 parent fa0780a commit d1b2282
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ jobs:
run: |
hatch run typing:test
hatch run lint:style
pipx run interrogate -vv .
pipx run interrogate -vv . --fail-under 90
pipx run doc8 --max-line-length=200
check_release:
Expand Down
14 changes: 7 additions & 7 deletions ipykernel/debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -116,9 +117,9 @@ 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(
max_buffer_size=inf
)
self.message_send_stream, self.message_receive_stream = create_memory_object_stream[
dict[str, Any]
](max_buffer_size=inf)
self.log = log

def _reset_tcp_pos(self):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -343,9 +343,9 @@ 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(
max_buffer_size=inf
)
self.stopped_send_stream, self.stopped_receive_stream = create_memory_object_stream[
dict[str, Any]
](max_buffer_size=inf)

self.started_debug_handlers = {}
for msg_type in Debugger.started_debug_msg_types:
Expand Down
4 changes: 2 additions & 2 deletions ipykernel/inprocess/ipkernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
20 changes: 14 additions & 6 deletions ipykernel/inprocess/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -56,20 +58,26 @@ 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."""
Expand Down
13 changes: 7 additions & 6 deletions ipykernel/inprocess/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -31,12 +32,12 @@ 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(
max_buffer_size=inf
)
self.out_send_stream, self.out_receive_stream = create_memory_object_stream(
max_buffer_size=inf
)
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[
dict[str, Any]
](max_buffer_size=inf)

def put(self, msg):
self.in_send_stream.send_nowait(msg)
Expand Down
2 changes: 1 addition & 1 deletion ipykernel/kernelapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion ipykernel/kernelbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit d1b2282

Please sign in to comment.