Improve codegen

Avoid on-import modification of classes.
This makes it possible to have multiple namespaces work together.

Implement equality on all generated types.
This enables support in tests as well feeling similar to dataclasses.

Make generated code constructors keyword-only.
This increases readability and reduces risk of breakage during upgrades.
This commit is contained in:
Lonami Exo
2023-07-08 12:15:11 +02:00
parent 7b707cfc6c
commit e74332de75
6 changed files with 67 additions and 12 deletions

View File

@@ -1,5 +1,10 @@
import struct
from pytest import mark
from telethon._impl.tl.core import Reader
from telethon._impl.tl.core.serializable import Serializable
from telethon._impl.tl.mtproto.types import BadServerSalt
from telethon._impl.tl.types import GeoPoint
@mark.parametrize(
@@ -24,3 +29,21 @@ sentence made it past!",
def test_string(string: str, prefix: bytes, suffix: bytes) -> None:
data = prefix + string.encode("ascii") + suffix
assert str(Reader(data).read_bytes(), "ascii") == string
@mark.parametrize(
"obj",
[
GeoPoint(long=12.34, lat=56.78, access_hash=123123, accuracy_radius=100),
BadServerSalt(
bad_msg_id=1234,
bad_msg_seqno=5678,
error_code=9876,
new_server_salt=5432,
),
],
)
def test_generated_object(obj: Serializable) -> None:
assert bytes(obj)[:4] == struct.pack("<I", obj.constructor_id())
assert type(obj)._read_from(Reader(bytes(obj)[4:])) == obj
assert Reader(bytes(obj)).read_serializable(type(obj)) == obj