From 84e14c2ec4d117f8f32f9258efbe4c150210a594 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Thu, 31 Aug 2023 10:34:36 +0200 Subject: [PATCH] Make Request class a generic container --- client/src/telethon/_impl/tl/core/request.py | 5 ++++- .../src/telethon_generator/_impl/codegen/generator.py | 7 ++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/client/src/telethon/_impl/tl/core/request.py b/client/src/telethon/_impl/tl/core/request.py index 8c135dd8..aa21ebe6 100644 --- a/client/src/telethon/_impl/tl/core/request.py +++ b/client/src/telethon/_impl/tl/core/request.py @@ -1,7 +1,10 @@ import struct +from typing import Generic, TypeVar + +Return = TypeVar("Return") -class Request(bytes): +class Request(bytes, Generic[Return]): __slots__ = () @property diff --git a/generator/src/telethon_generator/_impl/codegen/generator.py b/generator/src/telethon_generator/_impl/codegen/generator.py index 84d2cf8e..041ef55a 100644 --- a/generator/src/telethon_generator/_impl/codegen/generator.py +++ b/generator/src/telethon_generator/_impl/codegen/generator.py @@ -1,6 +1,7 @@ from pathlib import Path from typing import Set +from ..tl_parser.tl.parameter_type import NormalParameter from .fakefs import FakeFs, SourceWriter from .loader import ParsedTl from .serde.common import ( @@ -139,8 +140,6 @@ def generate(fs: FakeFs, tl: ParsedTl) -> None: writer.write(f" pass") for functiondef in tl.functiondefs: - required_params = [p for p in functiondef.params if not is_computed(p.ty)] - if len(functiondef.namespace) >= 2: raise NotImplementedError("nested function-namespaces are not supported") 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") # 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) star = "*" if params else "" + return_ty = param_type_fmt(NormalParameter(ty=functiondef.ty, flag=None)) 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) generate_function(writer, functiondef)