diff --git a/generator/src/telethon_generator/_impl/codegen/generator.py b/generator/src/telethon_generator/_impl/codegen/generator.py index d06146b3..ff13680a 100644 --- a/generator/src/telethon_generator/_impl/codegen/generator.py +++ b/generator/src/telethon_generator/_impl/codegen/generator.py @@ -94,7 +94,7 @@ def generate(fs: FakeFs, tl: ParsedTl) -> None: if type_path not in fs: writer.write("import struct") - writer.write("from typing import List, Optional, Self") + writer.write("from typing import List, Optional, Self, Sequence") writer.write("from .. import abcs") writer.write("from ..core import Reader, Serializable, serialize_bytes_to") @@ -118,7 +118,8 @@ def generate(fs: FakeFs, tl: ParsedTl) -> None: # def __init__() if property_params: params = "".join( - f", {p.name}: {param_type_fmt(p.ty)}" for p in property_params + f", {p.name}: {param_type_fmt(p.ty, immutable=False)}" + for p in property_params ) writer.write(f" def __init__(_s, *{params}) -> None:") for p in property_params: @@ -158,15 +159,20 @@ def generate(fs: FakeFs, tl: ParsedTl) -> None: if function_path not in fs: writer.write("import struct") - writer.write("from typing import List, Optional, Self") + writer.write("from typing import List, Optional, Self, Sequence") writer.write("from .. import abcs") writer.write("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) + params = "".join( + f", {p.name}: {param_type_fmt(p.ty, immutable=True)}" + for p in required_params + ) star = "*" if params else "" - return_ty = param_type_fmt(NormalParameter(ty=functiondef.ty, flag=None)) + return_ty = param_type_fmt( + NormalParameter(ty=functiondef.ty, flag=None), immutable=False + ) writer.write( f"def {to_method_name(functiondef.name)}({star}{params}) -> Request[{return_ty}]:" ) diff --git a/generator/src/telethon_generator/_impl/codegen/serde/common.py b/generator/src/telethon_generator/_impl/codegen/serde/common.py index adcbe714..e96fca74 100644 --- a/generator/src/telethon_generator/_impl/codegen/serde/common.py +++ b/generator/src/telethon_generator/_impl/codegen/serde/common.py @@ -86,7 +86,7 @@ def inner_type_fmt(ty: Type) -> str: return f"abcs.{ns}{to_class_name(ty.name)}" -def param_type_fmt(ty: BaseParameter) -> str: +def param_type_fmt(ty: BaseParameter, *, immutable: bool) -> str: if isinstance(ty, FlagsParameter): return "int" elif not isinstance(ty, NormalParameter): @@ -104,7 +104,10 @@ def param_type_fmt(ty: BaseParameter) -> str: res = "bytes" if inner_ty.name == "Object" else inner_type_fmt(inner_ty) if ty.ty.generic_arg: - res = f"List[{res}]" + if immutable: + res = f"Sequence[{res}]" + else: + res = f"List[{res}]" if ty.flag and ty.ty.name != "true": res = f"Optional[{res}]"