Skip to content

Commit

Permalink
run lint with pyupgrade at py39-plus
Browse files Browse the repository at this point in the history
  • Loading branch information
pacrob committed Jan 25, 2025
1 parent 20580b9 commit 8787613
Show file tree
Hide file tree
Showing 44 changed files with 220 additions and 239 deletions.
5 changes: 1 addition & 4 deletions libp2p/crypto/authenticated_encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
dataclass,
)
import hmac
from typing import (
Tuple,
)

from Crypto.Cipher import (
AES,
Expand Down Expand Up @@ -66,7 +63,7 @@ def decrypt_if_valid(self, data_with_tag: bytes) -> bytes:

def initialize_pair(
cipher_type: str, hash_type: str, secret: bytes
) -> Tuple[EncryptionParameters, EncryptionParameters]:
) -> tuple[EncryptionParameters, EncryptionParameters]:
"""
Return a pair of ``Keys`` for use in securing a communications channel
with authenticated encryption derived from the ``secret`` and using the
Expand Down
3 changes: 1 addition & 2 deletions libp2p/crypto/key_exchange.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import (
Callable,
Tuple,
cast,
)

Expand All @@ -22,7 +21,7 @@
int_bytelen = util.int_bytelen


def create_ephemeral_key_pair(curve_type: str) -> Tuple[PublicKey, SharedKeyGenerator]:
def create_ephemeral_key_pair(curve_type: str) -> tuple[PublicKey, SharedKeyGenerator]:
"""Facilitates ECDH key exchange."""
if curve_type != "P-256":
raise NotImplementedError()
Expand Down
13 changes: 7 additions & 6 deletions libp2p/host/basic_host.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from collections.abc import (
AsyncIterator,
Sequence,
)
from contextlib import (
asynccontextmanager,
)
import logging
from typing import (
TYPE_CHECKING,
AsyncIterator,
List,
Sequence,
)

import multiaddr
Expand Down Expand Up @@ -132,20 +133,20 @@ def get_mux(self) -> Multiselect:
"""
return self.multiselect

def get_addrs(self) -> List[multiaddr.Multiaddr]:
def get_addrs(self) -> list[multiaddr.Multiaddr]:
"""
:return: all the multiaddr addresses this host is listening to
"""
# TODO: We don't need "/p2p/{peer_id}" postfix actually.
p2p_part = multiaddr.Multiaddr(f"/p2p/{self.get_id()!s}")

addrs: List[multiaddr.Multiaddr] = []
addrs: list[multiaddr.Multiaddr] = []
for transport in self._network.listeners.values():
for addr in transport.get_addrs():
addrs.append(addr.encapsulate(p2p_part))
return addrs

def get_connected_peers(self) -> List[ID]:
def get_connected_peers(self) -> list[ID]:
"""
:return: all the ids of peers this host is currently connected to
"""
Expand Down
9 changes: 5 additions & 4 deletions libp2p/host/host_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
ABC,
abstractmethod,
)
from collections.abc import (
Sequence,
)
from typing import (
Any,
AsyncContextManager,
List,
Sequence,
)

import multiaddr
Expand Down Expand Up @@ -66,13 +67,13 @@ def get_mux(self) -> Any:
"""

@abstractmethod
def get_addrs(self) -> List[multiaddr.Multiaddr]:
def get_addrs(self) -> list[multiaddr.Multiaddr]:
"""
:return: all the multiaddr addresses this host is listening to
"""

@abstractmethod
def get_connected_peers(self) -> List[ID]:
def get_connected_peers(self) -> list[ID]:
"""
:return: all the ids of peers this host is currently connected to
"""
Expand Down
5 changes: 1 addition & 4 deletions libp2p/host/ping.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import logging
import secrets
import time
from typing import (
List,
)

import trio

Expand Down Expand Up @@ -102,7 +99,7 @@ class PingService:
def __init__(self, host: IHost):
self._host = host

async def ping(self, peer_id: PeerID, ping_amt: int = 1) -> List[int]:
async def ping(self, peer_id: PeerID, ping_amt: int = 1) -> list[int]:
stream = await self._host.new_stream(peer_id, [ID])

try:
Expand Down
5 changes: 1 addition & 4 deletions libp2p/network/connection/net_connection_interface.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from abc import (
abstractmethod,
)
from typing import (
Tuple,
)

import trio

Expand All @@ -27,5 +24,5 @@ async def new_stream(self) -> INetStream:
...

@abstractmethod
def get_streams(self) -> Tuple[INetStream, ...]:
def get_streams(self) -> tuple[INetStream, ...]:
...
6 changes: 2 additions & 4 deletions libp2p/network/connection/swarm_connection.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from typing import (
TYPE_CHECKING,
Set,
Tuple,
)

import trio
Expand Down Expand Up @@ -32,7 +30,7 @@
class SwarmConn(INetConn):
muxed_conn: IMuxedConn
swarm: "Swarm"
streams: Set[NetStream]
streams: set[NetStream]
event_closed: trio.Event

def __init__(self, muxed_conn: IMuxedConn, swarm: "Swarm") -> None:
Expand Down Expand Up @@ -104,7 +102,7 @@ async def new_stream(self) -> NetStream:
muxed_stream = await self.muxed_conn.open_stream()
return await self._add_stream(muxed_stream)

def get_streams(self) -> Tuple[NetStream, ...]:
def get_streams(self) -> tuple[NetStream, ...]:
return tuple(self.streams)

def remove_stream(self, stream: NetStream) -> None:
Expand Down
9 changes: 5 additions & 4 deletions libp2p/network/network_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
ABC,
abstractmethod,
)
from collections.abc import (
Sequence,
)
from typing import (
TYPE_CHECKING,
Dict,
Sequence,
)

from multiaddr import (
Expand Down Expand Up @@ -41,8 +42,8 @@

class INetwork(ABC):
peerstore: IPeerStore
connections: Dict[ID, INetConn]
listeners: Dict[str, IListener]
connections: dict[ID, INetConn]
listeners: dict[str, IListener]

@abstractmethod
def get_peer_id(self) -> ID:
Expand Down
10 changes: 4 additions & 6 deletions libp2p/network/swarm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import logging
from typing import (
Dict,
List,
Optional,
)

Expand Down Expand Up @@ -88,13 +86,13 @@ class Swarm(Service, INetworkService):
transport: ITransport
# TODO: Connection and `peer_id` are 1-1 mapping in our implementation,
# whereas in Go one `peer_id` may point to multiple connections.
connections: Dict[ID, INetConn]
listeners: Dict[str, IListener]
connections: dict[ID, INetConn]
listeners: dict[str, IListener]
common_stream_handler: StreamHandlerFn
listener_nursery: Optional[trio.Nursery]
event_listener_nursery_created: trio.Event

notifees: List[INotifee]
notifees: list[INotifee]

def __init__(
self,
Expand Down Expand Up @@ -161,7 +159,7 @@ async def dial_peer(self, peer_id: ID) -> INetConn:
if not addrs:
raise SwarmException(f"No known addresses to peer {peer_id}")

exceptions: List[SwarmException] = []
exceptions: list[SwarmException] = []

# Try all known addresses
for multiaddr in addrs:
Expand Down
7 changes: 3 additions & 4 deletions libp2p/peer/addrbook_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
ABC,
abstractmethod,
)
from typing import (
List,
from collections.abc import (
Sequence,
)

Expand Down Expand Up @@ -41,7 +40,7 @@ def add_addrs(self, peer_id: ID, addrs: Sequence[Multiaddr], ttl: int) -> None:
""" # noqa: E501

@abstractmethod
def addrs(self, peer_id: ID) -> List[Multiaddr]:
def addrs(self, peer_id: ID) -> list[Multiaddr]:
"""
:param peer_id: peer to get addresses of
:return: all known (and valid) addresses for the given peer
Expand All @@ -56,7 +55,7 @@ def clear_addrs(self, peer_id: ID) -> None:
"""

@abstractmethod
def peers_with_addrs(self) -> List[ID]:
def peers_with_addrs(self) -> list[ID]:
"""
:return: all of the peer IDs stored with addresses
"""
16 changes: 8 additions & 8 deletions libp2p/peer/peerdata.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from collections.abc import (
Sequence,
)
from typing import (
Any,
Dict,
List,
Sequence,
)

from multiaddr import (
Expand All @@ -22,9 +22,9 @@
class PeerData(IPeerData):
pubkey: PublicKey
privkey: PrivateKey
metadata: Dict[Any, Any]
protocols: List[str]
addrs: List[Multiaddr]
metadata: dict[Any, Any]
protocols: list[str]
addrs: list[Multiaddr]

def __init__(self) -> None:
self.pubkey = None
Expand All @@ -33,7 +33,7 @@ def __init__(self) -> None:
self.protocols = []
self.addrs = []

def get_protocols(self) -> List[str]:
def get_protocols(self) -> list[str]:
"""
:return: all protocols associated with given peer
"""
Expand All @@ -59,7 +59,7 @@ def add_addrs(self, addrs: Sequence[Multiaddr]) -> None:
if addr not in self.addrs:
self.addrs.append(addr)

def get_addrs(self) -> List[Multiaddr]:
def get_addrs(self) -> list[Multiaddr]:
"""
:return: all multiaddresses
"""
Expand Down
9 changes: 5 additions & 4 deletions libp2p/peer/peerdata_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
ABC,
abstractmethod,
)
from collections.abc import (
Sequence,
)
from typing import (
Any,
List,
Sequence,
)

from multiaddr import (
Expand All @@ -24,7 +25,7 @@

class IPeerData(ABC):
@abstractmethod
def get_protocols(self) -> List[str]:
def get_protocols(self) -> list[str]:
"""
:return: all protocols associated with given peer
"""
Expand All @@ -48,7 +49,7 @@ def add_addrs(self, addrs: Sequence[Multiaddr]) -> None:
"""

@abstractmethod
def get_addrs(self) -> List[Multiaddr]:
def get_addrs(self) -> list[Multiaddr]:
"""
:return: all multiaddresses
"""
Expand Down
7 changes: 4 additions & 3 deletions libp2p/peer/peerinfo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from collections.abc import (
Sequence,
)
from typing import (
Any,
List,
Sequence,
)

import multiaddr
Expand All @@ -13,7 +14,7 @@

class PeerInfo:
peer_id: ID
addrs: List[multiaddr.Multiaddr]
addrs: list[multiaddr.Multiaddr]

def __init__(self, peer_id: ID, addrs: Sequence[multiaddr.Multiaddr]) -> None:
self.peer_id = peer_id
Expand Down
Loading

0 comments on commit 8787613

Please sign in to comment.