From 8dda7b933ed9cc198d271002eff7543afe0bdb21 Mon Sep 17 00:00:00 2001 From: pacrob <5199899+pacrob@users.noreply.github.com> Date: Sat, 27 Apr 2024 09:41:56 -0600 Subject: [PATCH] turn mypy checks back on, fix some errors --- .pre-commit-config.yaml | 15 ++++++++------- libp2p/crypto/keys.py | 6 ++++-- libp2p/host/basic_host.py | 3 ++- libp2p/io/msgio.py | 5 ++++- libp2p/network/connection/swarm_connection.py | 4 +--- libp2p/network/swarm.py | 10 +++------- libp2p/pubsub/subscription.py | 6 ++---- libp2p/security/insecure/transport.py | 4 +++- libp2p/stream_muxer/mplex/mplex.py | 3 ++- libp2p/tools/interop/process.py | 3 +-- scripts/release/test_package.py | 13 +++++++++---- 11 files changed, 39 insertions(+), 33 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f1668a93..db0dbdb3 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -43,13 +43,14 @@ repos: - id: mdformat additional_dependencies: - mdformat-gfm -# - repo: https://github.com/pre-commit/mirrors-mypy -# rev: v1.5.1 -# hooks: -# - id: mypy -# additional_dependencies: -# - mypy-protobuf -# exclude: 'tests/|tests_interop/|crypto/|identity/|pubsub/|insecure/|noise/|security/' +- repo: https://github.com/pre-commit/mirrors-mypy + rev: v1.5.1 + hooks: + - id: mypy + additional_dependencies: + - mypy-protobuf + # exclude: 'tests/|crypto/|identity/|pubsub/|insecure/|noise/|security/' + exclude: 'tests/' - repo: local hooks: - id: check-rst-files diff --git a/libp2p/crypto/keys.py b/libp2p/crypto/keys.py index f5af513b..1dee7878 100644 --- a/libp2p/crypto/keys.py +++ b/libp2p/crypto/keys.py @@ -56,7 +56,8 @@ class PublicKey(Key): """Return the protobuf representation of this ``Key``.""" key_type = self.get_type().value data = self.to_bytes() - protobuf_key = protobuf.PublicKey(key_type=key_type, data=data) + # type ignored - TODO add ECD_P256 to KeyType + protobuf_key = protobuf.PublicKey(key_type=key_type, data=data) # type: ignore return protobuf_key def serialize(self) -> bytes: @@ -83,7 +84,8 @@ class PrivateKey(Key): """Return the protobuf representation of this ``Key``.""" key_type = self.get_type().value data = self.to_bytes() - protobuf_key = protobuf.PrivateKey(key_type=key_type, data=data) + # type ignored - TODO add ECD_P256 to KeyType + protobuf_key = protobuf.PrivateKey(key_type=key_type, data=data) # type: ignore return protobuf_key def serialize(self) -> bytes: diff --git a/libp2p/host/basic_host.py b/libp2p/host/basic_host.py index 76ffff12..6c39759f 100644 --- a/libp2p/host/basic_host.py +++ b/libp2p/host/basic_host.py @@ -145,7 +145,8 @@ class BasicHost(IHost): addrs.append(addr.encapsulate(p2p_part)) return addrs - @asynccontextmanager + # type ignored because asynccontextmanager decorator is untyped + @asynccontextmanager # type: ignore async def run( self, listen_addrs: Sequence[multiaddr.Multiaddr] ) -> AsyncIterator[None]: diff --git a/libp2p/io/msgio.py b/libp2p/io/msgio.py index 9556de6a..fa049cbd 100644 --- a/libp2p/io/msgio.py +++ b/libp2p/io/msgio.py @@ -8,6 +8,9 @@ NOTE: currently missing the capability to indicate lengths by "varint" method. from abc import ( abstractmethod, ) +from typing import ( + Literal, +) from libp2p.io.abc import ( MsgReadWriteCloser, @@ -26,7 +29,7 @@ from .exceptions import ( MessageTooLarge, ) -BYTE_ORDER = "big" +BYTE_ORDER: Literal["big", "little"] = "big" async def read_length(reader: Reader, size_len_bytes: int) -> int: diff --git a/libp2p/network/connection/swarm_connection.py b/libp2p/network/connection/swarm_connection.py index 9dbc1052..c079b2b8 100644 --- a/libp2p/network/connection/swarm_connection.py +++ b/libp2p/network/connection/swarm_connection.py @@ -83,9 +83,7 @@ class SwarmConn(INetConn): async def _handle_muxed_stream(self, muxed_stream: IMuxedStream) -> None: net_stream = await self._add_stream(muxed_stream) try: - # Ignore type here since mypy complains: - # https://github.com/python/mypy/issues/2427 - await self.swarm.common_stream_handler(net_stream) # type: ignore + await self.swarm.common_stream_handler(net_stream) finally: # As long as `common_stream_handler`, remove the stream. self.remove_stream(net_stream) diff --git a/libp2p/network/swarm.py b/libp2p/network/swarm.py index 54dbd9ca..614bc3c2 100644 --- a/libp2p/network/swarm.py +++ b/libp2p/network/swarm.py @@ -113,9 +113,7 @@ class Swarm(Service, INetworkService): # Create Notifee array self.notifees = [] - # Ignore type here since mypy complains: - # https://github.com/python/mypy/issues/2427 - self.common_stream_handler = create_default_stream_handler(self) # type: ignore + self.common_stream_handler = create_default_stream_handler(self) self.listener_nursery = None self.event_listener_nursery_created = trio.Event() @@ -137,9 +135,7 @@ class Swarm(Service, INetworkService): return self.self_id def set_stream_handler(self, stream_handler: StreamHandlerFn) -> None: - # Ignore type here since mypy complains: - # https://github.com/python/mypy/issues/2427 - self.common_stream_handler = stream_handler # type: ignore + self.common_stream_handler = stream_handler async def dial_peer(self, peer_id: ID) -> INetConn: """ @@ -273,7 +269,7 @@ class Swarm(Service, INetworkService): return True async def conn_handler( - read_write_closer: ReadWriteCloser, maddr=maddr + read_write_closer: ReadWriteCloser, maddr: Multiaddr = maddr ) -> None: raw_conn = RawConnection(read_write_closer, False) diff --git a/libp2p/pubsub/subscription.py b/libp2p/pubsub/subscription.py index 27aae289..aaab7e57 100644 --- a/libp2p/pubsub/subscription.py +++ b/libp2p/pubsub/subscription.py @@ -44,12 +44,10 @@ class TrioSubscriptionAPI(BaseSubscriptionAPI): unsubscribe_fn: UnsubscribeFn, ) -> None: self.receive_channel = receive_channel - # Ignore type here since mypy complains: https://github.com/python/mypy/issues/2427 # noqa: E501 - self.unsubscribe_fn = unsubscribe_fn # type: ignore + self.unsubscribe_fn = unsubscribe_fn async def unsubscribe(self) -> None: - # Ignore type here since mypy complains: https://github.com/python/mypy/issues/2427 # noqa: E501 - await self.unsubscribe_fn() # type: ignore + await self.unsubscribe_fn() def __aiter__(self) -> AsyncIterator[rpc_pb2.Message]: return self.receive_channel.__aiter__() diff --git a/libp2p/security/insecure/transport.py b/libp2p/security/insecure/transport.py index 06610c5b..e2a02946 100644 --- a/libp2p/security/insecure/transport.py +++ b/libp2p/security/insecure/transport.py @@ -182,7 +182,9 @@ class InsecureTransport(BaseSecureTransport): def make_exchange_message(pubkey: PublicKey) -> plaintext_pb2.Exchange: pubkey_pb = crypto_pb2.PublicKey( - key_type=pubkey.get_type().value, data=pubkey.to_bytes() + # type ignored - TODO add ECD_P256 to KeyType + key_type=pubkey.get_type().value, # type: ignore + data=pubkey.to_bytes(), ) id_bytes = ID.from_pubkey(pubkey).to_bytes() return plaintext_pb2.Exchange(id=id_bytes, pubkey=pubkey_pb) diff --git a/libp2p/stream_muxer/mplex/mplex.py b/libp2p/stream_muxer/mplex/mplex.py index 89d364c4..e76c3227 100644 --- a/libp2p/stream_muxer/mplex/mplex.py +++ b/libp2p/stream_muxer/mplex/mplex.py @@ -189,7 +189,8 @@ class Mplex(IMuxedConn): _bytes = header + encode_varint_prefixed(data) - return await self.write_to_stream(_bytes) + # type ignored TODO figure out return for this and write_to_stream + return await self.write_to_stream(_bytes) # type: ignore async def write_to_stream(self, _bytes: bytes) -> None: """ diff --git a/libp2p/tools/interop/process.py b/libp2p/tools/interop/process.py index 7cdf8729..f6e56130 100644 --- a/libp2p/tools/interop/process.py +++ b/libp2p/tools/interop/process.py @@ -56,8 +56,7 @@ class BaseInteractiveProcess(AbstractInterativeProcess): async def start(self) -> None: if self.proc is not None: return - # NOTE: Ignore type checks here since mypy complains about bufsize=0 - self.proc = await trio.open_process( # type: ignore + self.proc = await trio.open_process( [self.cmd] + self.args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, # Redirect stderr to stdout, which makes parsing easier # noqa: E501 diff --git a/scripts/release/test_package.py b/scripts/release/test_package.py index cc16c8e9..e8221c1d 100644 --- a/scripts/release/test_package.py +++ b/scripts/release/test_package.py @@ -5,10 +5,13 @@ import subprocess from tempfile import ( TemporaryDirectory, ) +from typing import ( + Tuple, +) import venv -def create_venv(parent_path): +def create_venv(parent_path: Path) -> Path: venv_path = parent_path / "package-smoke-test" venv.create(venv_path, with_pip=True) subprocess.run( @@ -17,7 +20,7 @@ def create_venv(parent_path): return venv_path -def find_wheel(project_path): +def find_wheel(project_path: Path) -> Path: wheels = list(project_path.glob("dist/*.whl")) if len(wheels) != 1: @@ -29,7 +32,9 @@ def find_wheel(project_path): return wheels[0] -def install_wheel(venv_path, wheel_path, extras=()): +def install_wheel( + venv_path: Path, wheel_path: Path, extras: Tuple[str, ...] = () +) -> None: if extras: extra_suffix = f"[{','.join(extras)}]" else: @@ -41,7 +46,7 @@ def install_wheel(venv_path, wheel_path, extras=()): ) -def test_install_local_wheel(): +def test_install_local_wheel() -> None: with TemporaryDirectory() as tmpdir: venv_path = create_venv(Path(tmpdir)) wheel_path = find_wheel(Path("."))