From d6bc9a9ea01bf2f980114a420fe438fd69d9d393 Mon Sep 17 00:00:00 2001 From: Egbert Bouman Date: Tue, 16 Jan 2024 23:19:48 +0100 Subject: [PATCH] Fixed typing errors --- ipv8/dht/community.py | 11 ++++++----- ipv8/messaging/anonymization/hidden_services.py | 6 ++++-- ipv8/messaging/anonymization/tunnel.py | 8 ++++---- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/ipv8/dht/community.py b/ipv8/dht/community.py index 1a2325223..69aced349 100644 --- a/ipv8/dht/community.py +++ b/ipv8/dht/community.py @@ -636,12 +636,13 @@ async def find(self, target: bytes, force_nodes: bool, offset: int, for routing_table in self.routing_tables.values(): crawl = Crawl(target, routing_table, force_nodes=force_nodes, offset=offset) futures.append(self._find(crawl, debug=debug)) - results: list[list[Node]] | \ - list[list[DHTValue]] | \ - list[tuple[list[DHTValue], Crawl]] = await gather(*futures) + results: list[list[Any] | + list[DHTValue] | + tuple[list[DHTValue], Crawl]] = await gather(*futures) + if debug: - results = cast(List[Tuple[List[Tuple[bytes, Optional[bytes]]], Crawl]], results) - return tuple(*[r[0] for r in results]), [r[1] for r in results] + results_debug = cast(List[Tuple[List[DHTValue], Crawl]], results) + return tuple(*[r[0] for r in results]), [r[1] for r in results_debug] return tuple(*results) async def find_values(self, target: bytes, offset: int = 0, diff --git a/ipv8/messaging/anonymization/hidden_services.py b/ipv8/messaging/anonymization/hidden_services.py index 57636c60d..28ed03dfa 100644 --- a/ipv8/messaging/anonymization/hidden_services.py +++ b/ipv8/messaging/anonymization/hidden_services.py @@ -10,7 +10,7 @@ import random import socket import struct -from asyncio import CancelledError, gather, iscoroutine +from asyncio import gather, iscoroutine from typing import TYPE_CHECKING, Any, Coroutine, Set, Tuple, cast from ...bootstrapping.dispersy.bootstrapper import DispersyBootstrapper @@ -171,7 +171,9 @@ async def estimate_swarm_size(self, info_hash: bytes, hops: int = 1, max_request responses = await gather(*[swarm.lookup(ip) for ip in ips], return_exceptions=True) # Collect responses - all_ += [result for result in responses if not isinstance(result, (CancelledError, Exception))] + for result in responses: + if result and not isinstance(result, (BaseException, Exception)): + all_.extend(result) tried |= set(ips) return len({ip.seeder_pk for ip in all_ if ip is not None and ip.source == PEER_SOURCE_PEX}) diff --git a/ipv8/messaging/anonymization/tunnel.py b/ipv8/messaging/anonymization/tunnel.py index e0e477003..ba0960de2 100644 --- a/ipv8/messaging/anonymization/tunnel.py +++ b/ipv8/messaging/anonymization/tunnel.py @@ -3,7 +3,7 @@ import contextlib import logging import time -from asyncio import CancelledError, Future, gather +from asyncio import Future, gather from binascii import hexlify from dataclasses import dataclass from typing import TYPE_CHECKING, Callable, Sequence, cast @@ -427,9 +427,9 @@ def on_success(ips: list[IntroductionPoint]) -> list[IntroductionPoint]: results = [] tasks = [self.lookup_func(self.info_hash, ip, self.hops) for ip in self.intro_points] if tasks: - results = [result for result in (await gather(*tasks, return_exceptions=True)) - if not isinstance(result, (CancelledError, Exception))] - results = sum(results, []) + for result in (await gather(*tasks, return_exceptions=True)): + if not isinstance(result, (BaseException, Exception)): + results.extend(result) return on_success(results) self.logger.info("Skipping lookup for swarm %s", hexlify(self.info_hash)) return None