Skip to content

Commit cd88aef

Browse files
Carreaukrassowski
authored andcommitted
Bump mypy (#1333)
Co-authored-by: Michał Krassowski <5832902+krassowski@users.noreply.github.com>
1 parent 70e895c commit cd88aef

File tree

7 files changed

+38
-23
lines changed

7 files changed

+38
-23
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ repos:
4040
types_or: [yaml, html, json]
4141

4242
- repo: https://github.com/pre-commit/mirrors-mypy
43-
rev: "v1.8.0"
43+
rev: "v1.15.0"
4444
hooks:
4545
- id: mypy
4646
files: ipykernel
47-
stages: [manual]
4847
args: ["--install-types", "--non-interactive"]
4948
additional_dependencies:
5049
[
5150
"traitlets>=5.13",
5251
"ipython>=8.16.1",
5352
"jupyter_client>=8.5",
5453
"appnope",
54+
"types-psutil",
5555
]
5656

5757
- repo: https://github.com/adamchainz/blacken-docs

ipykernel/heartbeat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def run(self):
103103

104104
while True:
105105
try:
106-
zmq.device(zmq.QUEUE, self.socket, self.socket)
106+
zmq.device(zmq.QUEUE, self.socket, self.socket) # type:ignore[attr-defined]
107107
except zmq.ZMQError as e:
108108
if e.errno == errno.EINTR:
109109
# signal interrupt, resume heartbeat

ipykernel/inprocess/blocking.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def call_handlers(self, msg):
6868
_raw_input = self.client.kernel._sys_raw_input
6969
prompt = msg["content"]["prompt"]
7070
print(prompt, end="", file=sys.__stdout__)
71+
assert sys.__stdout__ is not None
7172
sys.__stdout__.flush()
7273
self.client.input(_raw_input())
7374

ipykernel/inprocess/client.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# -----------------------------------------------------------------------------
1313

1414
import asyncio
15+
from typing import Any
1516

1617
from jupyter_client.client import KernelClient
1718
from jupyter_client.clientabc import KernelClientABC
@@ -57,9 +58,9 @@ def _default_blocking_class(self):
5758

5859
return BlockingInProcessKernelClient
5960

60-
def get_connection_info(self):
61+
def get_connection_info(self, session: bool = False):
6162
"""Get the connection info for the client."""
62-
d = super().get_connection_info()
63+
d = super().get_connection_info(session=session)
6364
d["kernel"] = self.kernel # type:ignore[assignment]
6465
return d
6566

@@ -103,8 +104,14 @@ def hb_channel(self):
103104
# -------------------------------------
104105

105106
def execute(
106-
self, code, silent=False, store_history=True, user_expressions=None, allow_stdin=None
107-
):
107+
self,
108+
code: str,
109+
silent: bool = False,
110+
store_history: bool = True,
111+
user_expressions: dict[str, Any] | None = None,
112+
allow_stdin: bool | None = None,
113+
stop_on_error: bool = True,
114+
) -> str:
108115
"""Execute code on the client."""
109116
if allow_stdin is None:
110117
allow_stdin = self.allow_stdin
@@ -117,7 +124,9 @@ def execute(
117124
)
118125
msg = self.session.msg("execute_request", content)
119126
self._dispatch_to_kernel(msg)
120-
return msg["header"]["msg_id"]
127+
res = msg["header"]["msg_id"]
128+
assert isinstance(res, str)
129+
return res
121130

122131
def complete(self, code, cursor_pos=None):
123132
"""Get code completion."""

ipykernel/kernelapp.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,12 @@ def init_io(self):
488488
sys.stderr = outstream_factory(self.session, self.iopub_thread, "stderr", echo=e_stderr)
489489
if hasattr(sys.stderr, "_original_stdstream_copy"):
490490
for handler in self.log.handlers:
491-
if isinstance(handler, StreamHandler) and (handler.stream.buffer.fileno() == 2):
491+
if (
492+
isinstance(handler, StreamHandler)
493+
and (buffer := getattr(handler.stream, "buffer", None))
494+
and (fileno := getattr(buffer, "fileno", None))
495+
and fileno() == sys.stderr._original_stdstream_fd
496+
):
492497
self.log.debug("Seeing logger to stderr, rerouting to raw filedescriptor.")
493498

494499
handler.stream = TextIOWrapper(
@@ -549,9 +554,9 @@ def init_signal(self):
549554
def init_kernel(self):
550555
"""Create the Kernel object itself"""
551556
shell_stream = ZMQStream(self.shell_socket)
552-
control_stream = ZMQStream(self.control_socket, self.control_thread.io_loop)
553-
debugpy_stream = ZMQStream(self.debugpy_socket, self.control_thread.io_loop)
554-
self.control_thread.start()
557+
control_stream = ZMQStream(self.control_socket, self.control_thread.io_loop) # type:ignore[union-attr]
558+
debugpy_stream = ZMQStream(self.debugpy_socket, self.control_thread.io_loop) # type:ignore[union-attr]
559+
self.control_thread.start() # type:ignore[union-attr]
555560
kernel_factory = self.kernel_class.instance # type:ignore[attr-defined]
556561

557562
kernel = kernel_factory(

ipykernel/kernelbase.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ def start(self):
580580

581581
control_loop = self.control_thread.io_loop if self.control_thread else self.io_loop
582582

583-
asyncio.run_coroutine_threadsafe(self.poll_control_queue(), control_loop.asyncio_loop)
583+
asyncio.run_coroutine_threadsafe(self.poll_control_queue(), control_loop.asyncio_loop) # type:ignore[union-attr]
584584
if self.shell_stream:
585585
self.shell_stream.on_recv(
586586
partial(

ipykernel/zmqshell.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,16 @@ def _hooks(self):
7878
self._thread_local.hooks = []
7979
return self._thread_local.hooks
8080

81-
def publish(
81+
# Feb: 2025 IPython has a deprecated, `source` parameter, marked for removal that
82+
# triggers typing errors.
83+
def publish( # type: ignore [override]
8284
self,
8385
data,
8486
metadata=None,
87+
*,
8588
transient=None,
8689
update=False,
90+
**kwargs,
8791
):
8892
"""Publish a display-data message
8993
@@ -483,7 +487,7 @@ def _update_exit_now(self, change):
483487

484488
# Over ZeroMQ, GUI control isn't done with PyOS_InputHook as there is no
485489
# interactive input being read; we provide event loop support in ipkernel
486-
def enable_gui(self, gui):
490+
def enable_gui(self, gui=None):
487491
"""Enable a given guil."""
488492
from .eventloops import enable_gui as real_enable_gui
489493

@@ -595,14 +599,10 @@ def set_parent(self, parent):
595599
self.display_pub.set_parent(parent) # type:ignore[attr-defined]
596600
if hasattr(self, "_data_pub"):
597601
self.data_pub.set_parent(parent)
598-
try:
599-
sys.stdout.set_parent(parent) # type:ignore[attr-defined]
600-
except AttributeError:
601-
pass
602-
try:
603-
sys.stderr.set_parent(parent) # type:ignore[attr-defined]
604-
except AttributeError:
605-
pass
602+
if hasattr(sys.stdout, "set_parent"):
603+
sys.stdout.set_parent(parent)
604+
if hasattr(sys.stderr, "set_parent"):
605+
sys.stderr.set_parent(parent)
606606

607607
def get_parent(self):
608608
"""Get the parent header."""

0 commit comments

Comments
 (0)