From bbf59e3561b63fc77dc5199416aec7818aeb5877 Mon Sep 17 00:00:00 2001 From: boyska Date: Thu, 30 Mar 2023 17:22:51 +0200 Subject: [PATCH] type hint for callbacks this make mypy --strict useful in checking if we forgot to update any callback --- .../usr/lib/python3/dist-packages/tca/torutils.py | 12 +++++++----- .../usr/lib/python3/dist-packages/tca/ui/asyncutils.py | 10 ++++++++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/config/chroot_local-includes/usr/lib/python3/dist-packages/tca/torutils.py b/config/chroot_local-includes/usr/lib/python3/dist-packages/tca/torutils.py index 2d5a0b6ac82..2da0812f4a4 100644 --- a/config/chroot_local-includes/usr/lib/python3/dist-packages/tca/torutils.py +++ b/config/chroot_local-includes/usr/lib/python3/dist-packages/tca/torutils.py @@ -10,9 +10,11 @@ import json import socket from stem.control import Controller import stem.socket -from typing import List, Optional, Dict, Any, Tuple, cast, Callable +from typing import List, Optional, Dict, Any, Tuple, cast import tca.config +from tca.ui.asyncutils import AsyncCallback + log = logging.getLogger("torutils") @@ -436,10 +438,10 @@ class TorConnectionConfig: class TorLauncherUtils: def __init__(self, stem_controller: Controller, - read_config_fn: Callable[[Callable], None], - write_config_fn: Callable[[Callable, str], None], + read_config_fn: AsyncCallback, + write_config_fn: AsyncCallback, state_buf, - set_tor_sandbox_fn: Callable[[Callable, str], None]): + set_tor_sandbox_fn: AsyncCallback): """ Arguments: stem_controller -- an already connected and authorized stem Controller @@ -518,7 +520,7 @@ class TorLauncherUtils: def read_tca_state(self): return decode_json_from_buf(self.state_buf) - def apply_conf(self, callback: Callable) -> bool: + def apply_conf(self, callback: AsyncCallback) -> bool: """ Apply the configuration from self.tor_connection_config to the running tor daemon, and asynchronously ensure Tor's Sandbox diff --git a/config/chroot_local-includes/usr/lib/python3/dist-packages/tca/ui/asyncutils.py b/config/chroot_local-includes/usr/lib/python3/dist-packages/tca/ui/asyncutils.py index 30f3ca01a72..6e401807d16 100644 --- a/config/chroot_local-includes/usr/lib/python3/dist-packages/tca/ui/asyncutils.py +++ b/config/chroot_local-includes/usr/lib/python3/dist-packages/tca/ui/asyncutils.py @@ -1,5 +1,5 @@ import os -from typing import List, Callable, Optional +from typing import List, Callable, Optional, Any import socket from logging import getLogger @@ -14,6 +14,12 @@ from gi.repository import GLib # noqa: E402 log = getLogger('asyncutils') +AsyncCallback = Callable[ + [GObject.GObject, Optional[dict], Optional[str], Optional[dict]], + Any + ] + + class GJsonRpcClient(GObject.GObject): """ Wrap a raw socket and uses JSON-RPC over it. @@ -56,7 +62,7 @@ class GJsonRpcClient(GObject.GObject): GLib.io_add_watch(self.sock.fileno(), GLib.IO_IN, self._on_data) GLib.io_add_watch(self.sock.fileno(), GLib.IO_HUP | GLib.IO_ERR, self._on_close) - def call_async(self, method: str, callback: Optional[Callable], *args, **kwargs): + def call_async(self, method: str, callback: Optional[AsyncCallback], *args, **kwargs): req = self.protocol.create_request(method, args, kwargs) log.debug('call async %s %s %s %d', method, args, kwargs, req.unique_id) if callback is not None: -- 2.11.4.GIT