Make Request class a generic container

This commit is contained in:
Lonami Exo 2023-08-31 10:34:36 +02:00
parent 8f89dbb5c2
commit 84e14c2ec4
2 changed files with 8 additions and 4 deletions

View File

@ -1,7 +1,10 @@
import struct import struct
from typing import Generic, TypeVar
Return = TypeVar("Return")
class Request(bytes): class Request(bytes, Generic[Return]):
__slots__ = () __slots__ = ()
@property @property

View File

@ -1,6 +1,7 @@
from pathlib import Path from pathlib import Path
from typing import Set from typing import Set
from ..tl_parser.tl.parameter_type import NormalParameter
from .fakefs import FakeFs, SourceWriter from .fakefs import FakeFs, SourceWriter
from .loader import ParsedTl from .loader import ParsedTl
from .serde.common import ( from .serde.common import (
@ -139,8 +140,6 @@ def generate(fs: FakeFs, tl: ParsedTl) -> None:
writer.write(f" pass") writer.write(f" pass")
for functiondef in tl.functiondefs: for functiondef in tl.functiondefs:
required_params = [p for p in functiondef.params if not is_computed(p.ty)]
if len(functiondef.namespace) >= 2: if len(functiondef.namespace) >= 2:
raise NotImplementedError("nested function-namespaces are not supported") raise NotImplementedError("nested function-namespaces are not supported")
elif len(functiondef.namespace) == 1: elif len(functiondef.namespace) == 1:
@ -161,10 +160,12 @@ def generate(fs: FakeFs, tl: ParsedTl) -> None:
writer.write(f"from ..core import Request, serialize_bytes_to") writer.write(f"from ..core import Request, serialize_bytes_to")
# def name(params, ...) # def name(params, ...)
required_params = [p for p in functiondef.params if not is_computed(p.ty)]
params = "".join(f", {p.name}: {param_type_fmt(p.ty)}" for p in required_params) params = "".join(f", {p.name}: {param_type_fmt(p.ty)}" for p in required_params)
star = "*" if params else "" star = "*" if params else ""
return_ty = param_type_fmt(NormalParameter(ty=functiondef.ty, flag=None))
writer.write( writer.write(
f"def {to_method_name(functiondef.name)}({star}{params}) -> Request:" f"def {to_method_name(functiondef.name)}({star}{params}) -> Request[{return_ty}]:"
) )
writer.indent(2) writer.indent(2)
generate_function(writer, functiondef) generate_function(writer, functiondef)