Skip to content

Commit c817714

Browse files
Merge pull request #791 from joouha/mypy-any-fixes-1
Correct `Any` type annotations.
2 parents 23ec0ac + 0057186 commit c817714

File tree

7 files changed

+32
-36
lines changed

7 files changed

+32
-36
lines changed

jupyter_client/channels.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def _create_socket(self) -> None:
101101
assert self.context is not None
102102
self.socket = self.context.socket(zmq.REQ)
103103
self.socket.linger = 1000
104+
assert self.address is not None
104105
self.socket.connect(self.address)
105106

106107
self.poller.register(self.socket, zmq.POLLIN)
@@ -192,9 +193,7 @@ def call_handlers(self, since_last_heartbeat: float) -> None:
192193
class ZMQSocketChannel(object):
193194
"""A ZMQ socket in an async API"""
194195

195-
def __init__(
196-
self, socket: zmq.sugar.socket.Socket, session: Session, loop: t.Any = None
197-
) -> None:
196+
def __init__(self, socket: zmq.asyncio.Socket, session: Session, loop: t.Any = None) -> None:
198197
"""Create a channel.
199198
200199
Parameters
@@ -208,7 +207,7 @@ def __init__(
208207
"""
209208
super().__init__()
210209

211-
self.socket: t.Optional[zmq.sugar.socket.Socket] = socket
210+
self.socket: t.Optional[zmq.asyncio.Socket] = socket
212211
self.session = session
213212

214213
async def _recv(self, **kwargs: t.Any) -> t.Dict[str, t.Any]:

jupyter_client/client.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,19 @@ def __del__(self):
137137
# Channel proxy methods
138138
# --------------------------------------------------------------------------
139139

140-
async def _async_get_shell_msg(self, *args: Any, **kwargs: Any) -> t.Dict[str, t.Any]:
140+
async def _async_get_shell_msg(self, *args: t.Any, **kwargs: t.Any) -> t.Dict[str, t.Any]:
141141
"""Get a message from the shell channel"""
142142
return await self.shell_channel.get_msg(*args, **kwargs)
143143

144-
async def _async_get_iopub_msg(self, *args: Any, **kwargs: Any) -> t.Dict[str, t.Any]:
144+
async def _async_get_iopub_msg(self, *args: t.Any, **kwargs: t.Any) -> t.Dict[str, t.Any]:
145145
"""Get a message from the iopub channel"""
146146
return await self.iopub_channel.get_msg(*args, **kwargs)
147147

148-
async def _async_get_stdin_msg(self, *args: Any, **kwargs: Any) -> t.Dict[str, t.Any]:
148+
async def _async_get_stdin_msg(self, *args: t.Any, **kwargs: t.Any) -> t.Dict[str, t.Any]:
149149
"""Get a message from the stdin channel"""
150150
return await self.stdin_channel.get_msg(*args, **kwargs)
151151

152-
async def _async_get_control_msg(self, *args: Any, **kwargs: Any) -> t.Dict[str, t.Any]:
152+
async def _async_get_control_msg(self, *args: t.Any, **kwargs: t.Any) -> t.Dict[str, t.Any]:
153153
"""Get a message from the control channel"""
154154
return await self.control_channel.get_msg(*args, **kwargs)
155155

@@ -270,7 +270,7 @@ def _output_hook_kernel(
270270
self,
271271
session: Session,
272272
socket: zmq.sugar.socket.Socket,
273-
parent_header: Any,
273+
parent_header: t.Any,
274274
msg: t.Dict[str, t.Any],
275275
) -> None:
276276
"""Output hook when running inside an IPython kernel
@@ -687,7 +687,7 @@ def history(
687687
raw: bool = True,
688688
output: bool = False,
689689
hist_access_type: str = "range",
690-
**kwargs: Any,
690+
**kwargs: t.Any,
691691
) -> str:
692692
"""Get entries from the kernel's history list.
693693

jupyter_client/manager.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def remove_restart_callback(self, callback: t.Callable, event: str = "restart")
235235
# create a Client connected to our Kernel
236236
# --------------------------------------------------------------------------
237237

238-
def client(self, **kwargs: Any) -> KernelClient:
238+
def client(self, **kwargs: t.Any) -> KernelClient:
239239
"""Create a client configured to connect to our kernel"""
240240
kw = {}
241241
kw.update(self.get_connection_info(session=True))
@@ -296,7 +296,7 @@ def from_ns(match):
296296

297297
return [pat.sub(from_ns, arg) for arg in cmd]
298298

299-
async def _async_launch_kernel(self, kernel_cmd: t.List[str], **kw: Any) -> None:
299+
async def _async_launch_kernel(self, kernel_cmd: t.List[str], **kw: t.Any) -> None:
300300
"""actually launch the kernel
301301
302302
override in a subclass to launch kernel subprocesses differently
@@ -324,7 +324,9 @@ def _close_control_socket(self) -> None:
324324
self._control_socket.close()
325325
self._control_socket = None
326326

327-
async def _async_pre_start_kernel(self, **kw: Any) -> t.Tuple[t.List[str], t.Dict[str, t.Any]]:
327+
async def _async_pre_start_kernel(
328+
self, **kw: t.Any
329+
) -> t.Tuple[t.List[str], t.Dict[str, t.Any]]:
328330
"""Prepares a kernel for startup in a separate process.
329331
330332
If random ports (port=0) are being used, this method must be called
@@ -352,7 +354,7 @@ async def _async_pre_start_kernel(self, **kw: Any) -> t.Tuple[t.List[str], t.Dic
352354

353355
pre_start_kernel = run_sync(_async_pre_start_kernel)
354356

355-
async def _async_post_start_kernel(self, **kw: Any) -> None:
357+
async def _async_post_start_kernel(self, **kw: t.Any) -> None:
356358
"""Performs any post startup tasks relative to the kernel.
357359
358360
Parameters
@@ -368,7 +370,7 @@ async def _async_post_start_kernel(self, **kw: Any) -> None:
368370
post_start_kernel = run_sync(_async_post_start_kernel)
369371

370372
@in_pending_state
371-
async def _async_start_kernel(self, **kw: Any) -> None:
373+
async def _async_start_kernel(self, **kw: t.Any) -> None:
372374
"""Starts a kernel on this host in a separate process.
373375
374376
If random ports (port=0) are being used, this method must be called
@@ -500,7 +502,7 @@ async def _async_shutdown_kernel(self, now: bool = False, restart: bool = False)
500502
shutdown_kernel = run_sync(_async_shutdown_kernel)
501503

502504
async def _async_restart_kernel(
503-
self, now: bool = False, newports: bool = False, **kw: Any
505+
self, now: bool = False, newports: bool = False, **kw: t.Any
504506
) -> None:
505507
"""Restarts a kernel with the arguments that were used to launch it.
506508
@@ -661,7 +663,7 @@ class AsyncKernelManager(KernelManager):
661663

662664

663665
def start_new_kernel(
664-
startup_timeout: float = 60, kernel_name: str = "python", **kwargs: Any
666+
startup_timeout: float = 60, kernel_name: str = "python", **kwargs: t.Any
665667
) -> t.Tuple[KernelManager, KernelClient]:
666668
"""Start a new kernel, and return its Manager and Client"""
667669
km = KernelManager(kernel_name=kernel_name)
@@ -679,7 +681,7 @@ def start_new_kernel(
679681

680682

681683
async def start_new_async_kernel(
682-
startup_timeout: float = 60, kernel_name: str = "python", **kwargs: Any
684+
startup_timeout: float = 60, kernel_name: str = "python", **kwargs: t.Any
683685
) -> t.Tuple[AsyncKernelManager, KernelClient]:
684686
"""Start a new kernel, and return its Manager and Client"""
685687
km = AsyncKernelManager(kernel_name=kernel_name)
@@ -697,7 +699,7 @@ async def start_new_async_kernel(
697699

698700

699701
@contextmanager
700-
def run_kernel(**kwargs: Any) -> t.Iterator[KernelClient]:
702+
def run_kernel(**kwargs: t.Any) -> t.Iterator[KernelClient]:
701703
"""Context manager to create a kernel in a subprocess.
702704
703705
The kernel is shut down when the context exits.

jupyter_client/multikernelmanager.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def kernel_method(f: t.Callable) -> t.Callable:
3434
"""decorator for proxying MKM.method(kernel_id) to individual KMs by ID"""
3535

3636
def wrapped(
37-
self: Any, kernel_id: str, *args: Any, **kwargs: Any
37+
self: t.Any, kernel_id: str, *args: t.Any, **kwargs: t.Any
3838
) -> t.Union[t.Callable, t.Awaitable]:
3939
# get the kernel
4040
km = self.get_kernel(kernel_id)
@@ -79,7 +79,7 @@ def _kernel_manager_factory_default(self):
7979
def _create_kernel_manager_factory(self) -> t.Callable:
8080
kernel_manager_ctor = import_item(self.kernel_manager_class)
8181

82-
def create_kernel_manager(*args: Any, **kwargs: Any) -> KernelManager:
82+
def create_kernel_manager(*args: t.Any, **kwargs: t.Any) -> KernelManager:
8383
if self.shared_context:
8484
if self.context.closed:
8585
# recreate context if closed
@@ -142,7 +142,7 @@ def __contains__(self, kernel_id: str) -> bool:
142142
return kernel_id in self._kernels
143143

144144
def pre_start_kernel(
145-
self, kernel_name: t.Optional[str], kwargs: Any
145+
self, kernel_name: t.Optional[str], kwargs: t.Any
146146
) -> t.Tuple[KernelManager, str, str]:
147147
# kwargs should be mutable, passing it as a dict argument.
148148
kernel_id = kwargs.pop("kernel_id", self.new_kernel_id(**kwargs))
@@ -192,7 +192,9 @@ def _using_pending_kernels(self):
192192
"""
193193
return getattr(self, 'use_pending_kernels', False)
194194

195-
async def _async_start_kernel(self, kernel_name: t.Optional[str] = None, **kwargs: Any) -> str:
195+
async def _async_start_kernel(
196+
self, kernel_name: t.Optional[str] = None, **kwargs: t.Any
197+
) -> str:
196198
"""Start a new kernel.
197199
198200
The caller can pick a kernel_id by passing one in as a keyword arg,
@@ -517,7 +519,7 @@ def connect_hb(self, kernel_id: str, identity: t.Optional[bytes] = None) -> sock
517519
stream : zmq Socket or ZMQStream
518520
"""
519521

520-
def new_kernel_id(self, **kwargs: Any) -> str:
522+
def new_kernel_id(self, **kwargs: t.Any) -> str:
521523
"""
522524
Returns the id to associate with the kernel for this request. Subclasses may override
523525
this method to substitute other sources of kernel ids.

jupyter_client/session.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ class SessionFactory(LoggingConfigurable):
216216
logname = Unicode("")
217217

218218
@observe("logname") # type:ignore[misc]
219-
def _logname_changed(self, change: Any) -> None:
219+
def _logname_changed(self, change: t.Any) -> None:
220220
self.log = logging.getLogger(change["new"])
221221

222222
# not configurable:
@@ -1077,7 +1077,7 @@ def deserialize(
10771077
# adapt to the current version
10781078
return adapt(message)
10791079

1080-
def unserialize(self, *args: Any, **kwargs: Any) -> t.Dict[str, t.Any]:
1080+
def unserialize(self, *args: t.Any, **kwargs: t.Any) -> t.Dict[str, t.Any]:
10811081
warnings.warn(
10821082
"Session.unserialize is deprecated. Use Session.deserialize.",
10831083
DeprecationWarning,

jupyter_client/threaded.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
from threading import Event
99
from threading import Thread
1010
from typing import Any
11-
from typing import Awaitable
1211
from typing import Dict
1312
from typing import List
1413
from typing import Optional
15-
from typing import Union
1614

1715
import zmq
1816
from traitlets import Instance
@@ -30,10 +28,6 @@
3028
# during garbage collection of threads at exit
3129

3230

33-
async def get_msg(msg: Awaitable) -> Union[List[bytes], List[zmq.Message]]:
34-
return await msg
35-
36-
3731
class ThreadedZMQSocketChannel(object):
3832
"""A ZMQ socket invoking a callback in the ioloop"""
3933

@@ -68,6 +62,7 @@ def __init__(
6862
evt = Event()
6963

7064
def setup_stream():
65+
assert self.socket is not None
7166
self.stream = zmqstream.ZMQStream(self.socket, self.ioloop)
7267
self.stream.on_recv(self._handle_recv)
7368
evt.set()
@@ -113,13 +108,11 @@ def thread_send():
113108
assert self.ioloop is not None
114109
self.ioloop.add_callback(thread_send)
115110

116-
def _handle_recv(self, future_msg: Awaitable) -> None:
111+
def _handle_recv(self, msg_list: List[bytes]) -> None:
117112
"""Callback for stream.on_recv.
118113
119114
Unpacks message, and calls handlers with it.
120115
"""
121-
assert self.ioloop is not None
122-
msg_list = self.ioloop._asyncio_event_loop.run_until_complete(get_msg(future_msg))
123116
assert self.session is not None
124117
ident, smsg = self.session.feed_identities(msg_list)
125118
msg = self.session.deserialize(smsg)

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ dependencies = [
2828
"jupyter_core>=4.9.2",
2929
"nest-asyncio>=1.5.4",
3030
"python-dateutil>=2.8.2",
31-
"pyzmq>=22.3",
31+
"pyzmq>=23.0",
3232
"tornado>=6.0",
3333
"traitlets",
3434
]

0 commit comments

Comments
 (0)