Prechádzať zdrojové kódy

type: silence google.protobuf mypy errors

protobuf ships no type information (no py.typed, no .pyi), so mypy flagged
every 'import google.protobuf...' in the hand-written transport with
'Library stubs not installed'. Rather than hand-write stubs for a large
external library (which would rot), add google.protobuf.* to the existing
[[tool.mypy.overrides]] untyped-module list, alongside the generated _pb/_pb
modules and websockets.

Also cast the _BinaryCodec.encode result to bytes: with Message now
Any-typed, SerializeToString() is Any, and the concrete '-> bytes' return
tripped no-any-return. The cast documents that protobuf returns bytes.

mypy is now fully clean (9 source files, no issues); ruff and the full test
suite (73 passed) are unaffected.
Federico Feliziani 2 mesiacov pred
rodič
commit
4dcb79aa93
2 zmenil súbory, kde vykonal 14 pridanie a 3 odobranie
  1. 10 1
      pyproject.toml
  2. 4 2
      src/chattolib/_connect.py

+ 10 - 1
pyproject.toml

@@ -67,6 +67,15 @@ warn_unused_configs = true
 exclude = ["src/chattolib/_pb", "src/chattolib/_codegen"]
 
 [[tool.mypy.overrides]]
-module = ["chattolib._pb.*", "chattolib._codegen.*", "websockets.*"]
+# Generated protobuf modules and third-party libs that ship no type info are
+# treated as untyped: protobuf bundles no .pyi/py.typed, and websockets is only
+# used behind the [realtime] extra. ignore_missing_imports keeps mypy from
+# flagging the `import google.protobuf...` lines in the hand-written transport.
+module = [
+    "chattolib._pb.*",
+    "chattolib._codegen.*",
+    "google.protobuf.*",
+    "websockets.*",
+]
 follow_imports = "skip"
 ignore_missing_imports = true

+ 4 - 2
src/chattolib/_connect.py

@@ -27,7 +27,7 @@ import asyncio
 import enum
 from collections.abc import Mapping
 from dataclasses import dataclass
-from typing import Any
+from typing import Any, cast
 
 import httpx
 from google.protobuf.message import Message
@@ -118,7 +118,9 @@ class _BinaryCodec:
         return "proto"
 
     def encode(self, message: Message) -> bytes:
-        return message.SerializeToString()
+        # SerializeToString returns bytes; the cast documents that (Message is
+        # untyped because protobuf ships no type info, so mypy sees Any).
+        return cast(bytes, message.SerializeToString())
 
     def decode(self, data: bytes, message_class: type[Message]) -> Message:
         return message_class.FromString(data)