From 960ff5adb6c177b7130cefc094343499326f6d35 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 4 Jan 2023 17:21:32 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ipykernel/athread.py | 10 +++------- ipykernel/inprocess/ipkernel.py | 2 +- ipykernel/iostream.py | 15 +++++++-------- ipykernel/ipkernel.py | 6 +----- ipykernel/kernelapp.py | 3 +-- ipykernel/kernelbase.py | 27 +++++++-------------------- ipykernel/shell.py | 1 - ipykernel/tests/conftest.py | 2 +- ipykernel/tests/test_io.py | 6 +++--- 9 files changed, 24 insertions(+), 48 deletions(-) diff --git a/ipykernel/athread.py b/ipykernel/athread.py index e0dd3236..d8795cab 100644 --- a/ipykernel/athread.py +++ b/ipykernel/athread.py @@ -1,13 +1,11 @@ import asyncio -import inspect import threading import janus class AThread(threading.Thread): - """A thread that can run async tasks. - """ + """A thread that can run async tasks.""" def __init__(self, name, awaitables=[]): super().__init__(name=name, daemon=True) @@ -40,8 +38,7 @@ async def _main(self): task.cancel() def create_task(self, awaitable): - """Create a task in the thread (thread-safe). - """ + """Create a task in the thread (thread-safe).""" with self._lock: if self.__initialized: self._queue.sync_q.put(awaitable) @@ -49,8 +46,7 @@ def create_task(self, awaitable): self._aws.append(awaitable) def stop(self): - """Stop the thread (thread-safe). - """ + """Stop the thread (thread-safe).""" with self._lock: if self.__initialized: self._queue.sync_q.put(None) diff --git a/ipykernel/inprocess/ipkernel.py b/ipykernel/inprocess/ipkernel.py index 9b4c3aa5..3707867e 100644 --- a/ipykernel/inprocess/ipkernel.py +++ b/ipykernel/inprocess/ipkernel.py @@ -51,7 +51,7 @@ class InProcessKernel(IPythonKernel): _underlying_iopub_socket = Instance(DummySocket, ()) iopub_thread: IOPubThread = Instance(IOPubThread) # type:ignore[assignment] - #shell_stream = Instance(DummySocket, ()) + # shell_stream = Instance(DummySocket, ()) @default("iopub_thread") def _default_iopub_thread(self): diff --git a/ipykernel/iostream.py b/ipykernel/iostream.py index 9b058d49..bfce08fc 100644 --- a/ipykernel/iostream.py +++ b/ipykernel/iostream.py @@ -65,7 +65,7 @@ def __init__(self, socket, pipe=False): self._events: Deque[Callable[..., Any]] = deque() self._event_pipes: WeakSet[Any] = WeakSet() self._setup_event_pipe() - #self.thread = threading.Thread(target=self._thread_main, name="IOPub") + # self.thread = threading.Thread(target=self._thread_main, name="IOPub") aws = [self._handle_event()] if self._pipe_flag: aws.append(self._handle_pipe_msg()) @@ -169,7 +169,7 @@ def _check_mp_mode(self): def start(self): """Start the IOPub thread""" - #self.thread.name = "IOPub" + # self.thread.name = "IOPub" self.thread.start() # make sure we don't prevent process exit # I'm not sure why setting daemon=True above isn't enough, but it doesn't appear to be. @@ -179,7 +179,7 @@ def stop(self): """Stop the IOPub thread""" if not self.thread.is_alive(): return - #self.io_loop.call_soon_threadsafe(self.io_loop.stop) + # self.io_loop.call_soon_threadsafe(self.io_loop.stop) self.thread.stop() self.thread.join() # close *all* event pipes, created in any thread @@ -210,9 +210,9 @@ def schedule(self, f): if self.thread.is_alive(): self._events.append(f) # wake event thread (message content is ignored) - #try: + # try: # asyncio.get_running_loop() - #except BaseException: + # except BaseException: # asyncio.set_event_loop(asyncio.new_event_loop()) self._event_pipe.send(b"") else: @@ -482,13 +482,12 @@ def _schedule_flush(self): self._flush_pending = True # add_timeout has to be handed to the io thread via event pipe - #def _schedule_in_thread(): + # def _schedule_in_thread(): # self._io_loop.call_later(self.flush_interval, self._flush) - #self.pub_thread.schedule(_schedule_in_thread) + # self.pub_thread.schedule(_schedule_in_thread) self.pub_thread.schedule(self._flush) - def flush(self): """trigger actual zmq send diff --git a/ipykernel/ipkernel.py b/ipykernel/ipkernel.py index 91d037d9..fca4e30f 100644 --- a/ipykernel/ipkernel.py +++ b/ipykernel/ipkernel.py @@ -7,10 +7,7 @@ import sys import threading import typing as t -from contextlib import contextmanager -from functools import partial -import janus import comm import zmq.asyncio from IPython.core import release @@ -232,7 +229,7 @@ def start(self): self.log.warning("debugpy_socket undefined, debugging will not be enabled") super().start() if self.debugpy_socket: - #asyncio.run_coroutine_threadsafe(self.process_debugpy(), self.control_thread.loop) + # asyncio.run_coroutine_threadsafe(self.process_debugpy(), self.control_thread.loop) self.control_thread.create_task(self.process_debugpy()) def set_parent(self, ident, parent, channel="shell"): @@ -364,7 +361,6 @@ async def run_cell(*args, **kwargs): kwargs.update( transformed_cell=transformed_cell, preprocessing_exc_tuple=preprocessing_exc_tuple, - ) coro = run_cell(code, **kwargs) diff --git a/ipykernel/kernelapp.py b/ipykernel/kernelapp.py index 58b7d0c7..488169c2 100644 --- a/ipykernel/kernelapp.py +++ b/ipykernel/kernelapp.py @@ -740,8 +740,7 @@ def start(self): return def stop(self): - """Stop the kernel, thread-safe. - """ + """Stop the kernel, thread-safe.""" try: self._stop_queue.sync_q.put(None) except AttributeError: diff --git a/ipykernel/kernelbase.py b/ipykernel/kernelbase.py index a730f284..8b6c73bd 100644 --- a/ipykernel/kernelbase.py +++ b/ipykernel/kernelbase.py @@ -52,8 +52,8 @@ from ipykernel.jsonutil import json_clean -from .shell import ShellThread from ._version import kernel_protocol_version +from .shell import ShellThread def DEBUG(msg): @@ -622,12 +622,7 @@ async def execute_request(self, socket, ident, parent): kwargs.update(cell_id=cell_id) reply_content = self.do_execute( - code, - silent, - store_history, - user_expressions, - allow_stdin, - **kwargs + code, silent, store_history, user_expressions, allow_stdin, **kwargs ) DEBUG(f"execute_request {parent['header'].get('shell_id')}") @@ -673,7 +668,7 @@ def do_execute( allow_stdin=False, *, cell_id=None, - shell_id=None + shell_id=None, ): """Execute user code. Must be overridden by subclasses.""" raise NotImplementedError @@ -873,9 +868,7 @@ async def is_complete_request(self, socket, ident, parent): if inspect.isawaitable(reply_content): reply_content = await reply_content reply_content = json_clean(reply_content) - reply_msg = self.session.send( - socket, "is_complete_reply", reply_content, parent, ident - ) + reply_msg = self.session.send(socket, "is_complete_reply", reply_content, parent, ident) self.log.debug("%s", reply_msg) def do_is_complete(self, code): @@ -889,9 +882,7 @@ async def debug_request(self, socket, ident, parent): if inspect.isawaitable(reply_content): reply_content = await reply_content reply_content = json_clean(reply_content) - reply_msg = self.session.send( - socket, "debug_reply", reply_content, parent, ident - ) + reply_msg = self.session.send(socket, "debug_reply", reply_content, parent, ident) self.log.debug("%s", reply_msg) def get_process_metric_value(self, process, name, attribute=None): @@ -937,9 +928,7 @@ async def usage_request(self, socket, ident, parent): reply_content["host_cpu_percent"] = cpu_percent reply_content["cpu_count"] = psutil.cpu_count(logical=True) reply_content["host_virtual_memory"] = dict(psutil.virtual_memory()._asdict()) - reply_msg = self.session.send( - socket, "usage_reply", reply_content, parent, ident - ) + reply_msg = self.session.send(socket, "usage_reply", reply_content, parent, ident) self.log.debug("%s", reply_msg) async def do_debug_request(self, msg): @@ -1013,9 +1002,7 @@ async def clear_request(self, socket, idents, parent): # pragma: no cover "clear_request is deprecated in kernel_base. It is only part of IPython parallel" ) content = self.do_clear() - self.session.send( - socket, "clear_reply", ident=idents, parent=parent, content=content - ) + self.session.send(socket, "clear_reply", ident=idents, parent=parent, content=content) def do_clear(self): """DEPRECATED since 4.0.3""" diff --git a/ipykernel/shell.py b/ipykernel/shell.py index 121c1a0e..d4bf34ac 100644 --- a/ipykernel/shell.py +++ b/ipykernel/shell.py @@ -1,4 +1,3 @@ -from typing import Optional from .athread import AThread diff --git a/ipykernel/tests/conftest.py b/ipykernel/tests/conftest.py index 2aabef4c..9d09a123 100644 --- a/ipykernel/tests/conftest.py +++ b/ipykernel/tests/conftest.py @@ -92,7 +92,7 @@ def _prep_msg(self, *args, **kwargs): raw_msg = self.session.msg(*args, **kwargs) msg = self.session.serialize(raw_msg) return msg - #return [zmq.Message(m) for m in msg] + # return [zmq.Message(m) for m in msg] async def _wait_for_msg(self): while not self._reply: diff --git a/ipykernel/tests/test_io.py b/ipykernel/tests/test_io.py index a2651d4a..77639909 100644 --- a/ipykernel/tests/test_io.py +++ b/ipykernel/tests/test_io.py @@ -63,8 +63,8 @@ def test_io_thread(): pub = ctx.socket(zmq.PUB) thread = IOPubThread(pub) thread._setup_pipe_in() - #msg = [thread._pipe_uuid, b"a"] - #thread._handle_pipe_msg(msg) + # msg = [thread._pipe_uuid, b"a"] + # thread._handle_pipe_msg(msg) ctx1, pipe = thread._setup_pipe_out() pipe.close() thread._pipe_in1.close() @@ -118,4 +118,4 @@ def test_outstream(): assert stream.writable() thread.stop() thread.close() - #ctx.term() + # ctx.term()