From 686d5c90de5f56ef4063e183fdc14af0bb38416c Mon Sep 17 00:00:00 2001 From: M Bussonnier Date: Thu, 3 Oct 2024 16:04:15 +0200 Subject: [PATCH] Run Mypy by default. Otherwise the types annotations are less usefull, even with ruff. Ignore/fix types errors. --- .pre-commit-config.yaml | 3 +-- ipykernel/inprocess/blocking.py | 1 + ipykernel/inprocess/client.py | 11 ++++++++--- ipykernel/inprocess/session.py | 2 +- ipykernel/kernelapp.py | 2 +- ipykernel/kernelbase.py | 8 ++++++++ ipykernel/zmqshell.py | 10 ++++++---- 7 files changed, 26 insertions(+), 11 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index cc2cfd9d..20d8497d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -40,11 +40,10 @@ repos: types_or: [yaml, html, json] - repo: https://github.com/pre-commit/mirrors-mypy - rev: "v1.8.0" + rev: "v1.11.0" hooks: - id: mypy files: ipykernel - stages: [manual] args: ["--install-types", "--non-interactive"] additional_dependencies: [ diff --git a/ipykernel/inprocess/blocking.py b/ipykernel/inprocess/blocking.py index b5c421a7..5d70368f 100644 --- a/ipykernel/inprocess/blocking.py +++ b/ipykernel/inprocess/blocking.py @@ -68,6 +68,7 @@ def call_handlers(self, msg): _raw_input = self.client.kernel._sys_raw_input prompt = msg["content"]["prompt"] print(prompt, end="", file=sys.__stdout__) + assert sys.__stdout__ is not None sys.__stdout__.flush() self.client.input(_raw_input()) diff --git a/ipykernel/inprocess/client.py b/ipykernel/inprocess/client.py index 8ca97470..244efbef 100644 --- a/ipykernel/inprocess/client.py +++ b/ipykernel/inprocess/client.py @@ -55,7 +55,7 @@ def _default_blocking_class(self): return BlockingInProcessKernelClient - def get_connection_info(self): + def get_connection_info(self): # type: ignore[override] """Get the connection info for the client.""" d = super().get_connection_info() d["kernel"] = self.kernel # type:ignore[assignment] @@ -100,8 +100,13 @@ def hb_channel(self): # Methods for sending specific messages # ------------------------------------- - async def execute( - self, code, silent=False, store_history=True, user_expressions=None, allow_stdin=None + async def execute( # type: ignore[override] + self, + code, + silent=False, + store_history=True, + user_expressions=None, + allow_stdin=None, ): """Execute code on the client.""" if allow_stdin is None: diff --git a/ipykernel/inprocess/session.py b/ipykernel/inprocess/session.py index 0eaed2c6..3da2829e 100644 --- a/ipykernel/inprocess/session.py +++ b/ipykernel/inprocess/session.py @@ -2,7 +2,7 @@ class Session(_Session): - async def recv(self, socket, copy=True): + async def recv(self, socket, copy=True): # type: ignore [override] return await socket.recv_multipart() def send( diff --git a/ipykernel/kernelapp.py b/ipykernel/kernelapp.py index 2f462af4..1582a161 100644 --- a/ipykernel/kernelapp.py +++ b/ipykernel/kernelapp.py @@ -261,7 +261,7 @@ def _bind_socket(self, s, port): raise return None - def write_connection_file(self): + def write_connection_file(self): # type: ignore[override] """write connection info to JSON file""" cf = self.abs_connection_file connection_info = dict( diff --git a/ipykernel/kernelbase.py b/ipykernel/kernelbase.py index 99358f9b..1c59c161 100644 --- a/ipykernel/kernelbase.py +++ b/ipykernel/kernelbase.py @@ -233,6 +233,14 @@ def _parent_header(self): ] _eventloop_set: Event = Event() + control_handlers: Dict[ + str, + t.Callable[[zmq.asyncio.Socket, list[bytes], str], t.Awaitable[t.Any]] + | + # I think this one should be deprecated, and we should check the handlers are + # coroutine functions. + t.Callable[[zmq.asyncio.Socket, list[bytes], str], t.Any], + ] def __init__(self, **kwargs): """Initialize the kernel.""" diff --git a/ipykernel/zmqshell.py b/ipykernel/zmqshell.py index 3f97e817..f9327ae2 100644 --- a/ipykernel/zmqshell.py +++ b/ipykernel/zmqshell.py @@ -78,7 +78,7 @@ def _hooks(self): self._thread_local.hooks = [] return self._thread_local.hooks - def publish( + def publish( # type:ignore[override] self, data, metadata=None, @@ -516,7 +516,7 @@ def _update_exit_now(self, change): # Over ZeroMQ, GUI control isn't done with PyOS_InputHook as there is no # interactive input being read; we provide event loop support in ipkernel - def enable_gui(self, gui): + def enable_gui(self, gui): # type:ignore[override] """Enable a given guil.""" from .eventloops import enable_gui as real_enable_gui @@ -635,11 +635,13 @@ def set_parent(self, parent): if hasattr(self, "_data_pub"): self.data_pub.set_parent(parent) try: - sys.stdout.set_parent(parent) # type:ignore[attr-defined] + stdout = sys.stdout + stdout.set_parent(parent) # type:ignore[union-attr] except AttributeError: pass try: - sys.stderr.set_parent(parent) # type:ignore[attr-defined] + stderr = sys.stderr + stderr.set_parent(parent) # type:ignore[union-attr] except AttributeError: pass