mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-07-27 08:19:47 +03:00
Fix type hint for Serializable
This commit is contained in:
parent
381096bf41
commit
2078b87f72
|
@ -1,7 +1,7 @@
|
||||||
import functools
|
import functools
|
||||||
import struct
|
import struct
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
from typing import TYPE_CHECKING, Any, Optional, Type
|
from typing import TYPE_CHECKING, Any, Optional, Type, TypeVar
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from typing import Protocol
|
from typing import Protocol
|
||||||
|
@ -12,6 +12,9 @@ if TYPE_CHECKING:
|
||||||
def __buffer__(self, flags: int, /) -> memoryview: ...
|
def __buffer__(self, flags: int, /) -> memoryview: ...
|
||||||
|
|
||||||
|
|
||||||
|
SerializableType = TypeVar("SerializableType", bound="Serializable")
|
||||||
|
|
||||||
|
|
||||||
def _bootstrap_get_ty(constructor_id: int) -> Optional[Type["Serializable"]]:
|
def _bootstrap_get_ty(constructor_id: int) -> Optional[Type["Serializable"]]:
|
||||||
# Lazy import because generate code depends on the Reader.
|
# Lazy import because generate code depends on the Reader.
|
||||||
# After the first call, the class method is replaced with direct access.
|
# After the first call, the class method is replaced with direct access.
|
||||||
|
@ -76,7 +79,7 @@ class Reader:
|
||||||
|
|
||||||
_get_ty = staticmethod(_bootstrap_get_ty)
|
_get_ty = staticmethod(_bootstrap_get_ty)
|
||||||
|
|
||||||
def read_serializable(self, cls: Type["Serializable"]) -> "Serializable":
|
def read_serializable(self, cls: Type[SerializableType]) -> SerializableType:
|
||||||
# Calls to this method likely need to ignore "type-abstract".
|
# Calls to this method likely need to ignore "type-abstract".
|
||||||
# See https://github.com/python/mypy/issues/4717.
|
# See https://github.com/python/mypy/issues/4717.
|
||||||
# Unfortunately `typing.cast` would add a tiny amount of runtime overhead
|
# Unfortunately `typing.cast` would add a tiny amount of runtime overhead
|
||||||
|
@ -91,8 +94,10 @@ class Reader:
|
||||||
|
|
||||||
|
|
||||||
@functools.cache
|
@functools.cache
|
||||||
def single_deserializer(cls: Type["Serializable"]) -> Callable[[bytes], "Serializable"]:
|
def single_deserializer(
|
||||||
def deserializer(body: bytes) -> "Serializable":
|
cls: Type[SerializableType],
|
||||||
|
) -> Callable[[bytes], SerializableType]:
|
||||||
|
def deserializer(body: bytes) -> SerializableType:
|
||||||
return Reader(body).read_serializable(cls)
|
return Reader(body).read_serializable(cls)
|
||||||
|
|
||||||
return deserializer
|
return deserializer
|
||||||
|
@ -100,9 +105,9 @@ def single_deserializer(cls: Type["Serializable"]) -> Callable[[bytes], "Seriali
|
||||||
|
|
||||||
@functools.cache
|
@functools.cache
|
||||||
def list_deserializer(
|
def list_deserializer(
|
||||||
cls: Type["Serializable"],
|
cls: Type[SerializableType],
|
||||||
) -> Callable[[bytes], list["Serializable"]]:
|
) -> Callable[[bytes], list[SerializableType]]:
|
||||||
def deserializer(body: bytes) -> list["Serializable"]:
|
def deserializer(body: bytes) -> list[SerializableType]:
|
||||||
reader = Reader(body)
|
reader = Reader(body)
|
||||||
vec_id, length = reader.read_fmt("<ii", 8)
|
vec_id, length = reader.read_fmt("<ii", 8)
|
||||||
assert vec_id == 0x1CB5C415 and length >= 0
|
assert vec_id == 0x1CB5C415 and length >= 0
|
||||||
|
|
Loading…
Reference in New Issue
Block a user