From 771954d01010cdd35a3c924b66e55c27c310f6dd Mon Sep 17 00:00:00 2001 From: Jahongir Qurbonov <109198731+Jahongir-Qurbonov@users.noreply.github.com> Date: Wed, 9 Oct 2024 00:13:13 +0500 Subject: [PATCH] Add HiddenKeyboard and ForcedReplyKeyboard custom types and improve type annotation (#4481) --- client/pyproject.toml | 1 + .../src/telethon/_impl/client/client/bots.py | 4 +++- .../telethon/_impl/client/client/client.py | 4 +++- .../telethon/_impl/client/client/messages.py | 4 +++- .../src/telethon/_impl/client/events/event.py | 4 +++- .../telethon/_impl/client/events/messages.py | 4 +++- .../telethon/_impl/client/events/queries.py | 4 +++- .../telethon/_impl/client/types/async_list.py | 4 +++- .../src/telethon/_impl/client/types/dialog.py | 4 +++- .../src/telethon/_impl/client/types/draft.py | 4 +++- .../src/telethon/_impl/client/types/file.py | 4 +++- .../_impl/client/types/forward_info.py | 2 +- .../telethon/_impl/client/types/keyboard.py | 23 ++++++++++++++++++- .../_impl/client/types/link_preview.py | 2 +- .../_impl/client/types/login_token.py | 4 +++- .../telethon/_impl/client/types/message.py | 4 +++- .../_impl/client/types/participant.py | 4 +++- .../_impl/client/types/password_token.py | 2 +- .../_impl/client/types/peer/channel.py | 4 +++- .../telethon/_impl/client/types/peer/group.py | 4 +++- .../telethon/_impl/client/types/peer/user.py | 4 +++- client/src/telethon/_impl/crypto/auth_key.py | 3 ++- .../src/telethon/_impl/mtproto/mtp/types.py | 4 +++- client/src/telethon/_impl/mtsender/sender.py | 4 +++- .../telethon/_impl/session/chat/peer_ref.py | 4 +++- .../telethon/_impl/tl/core/serializable.py | 4 +++- generator/pyproject.toml | 2 +- .../_impl/codegen/generator.py | 12 ++++++---- .../_impl/tl_parser/tl/definition.py | 3 ++- .../_impl/tl_parser/tl/flag.py | 3 ++- .../_impl/tl_parser/tl/parameter.py | 3 ++- .../_impl/tl_parser/tl/ty.py | 4 +++- 32 files changed, 105 insertions(+), 35 deletions(-) diff --git a/client/pyproject.toml b/client/pyproject.toml index f6e3875f..c3a2046d 100644 --- a/client/pyproject.toml +++ b/client/pyproject.toml @@ -22,6 +22,7 @@ dependencies = [ "pyaes~=1.6", "rsa~=4.9", "markdown-it-py~=3.0", + "typing-extensions~=4.12.2", ] dynamic = ["version"] diff --git a/client/src/telethon/_impl/client/client/bots.py b/client/src/telethon/_impl/client/client/bots.py index f8de14fc..fc5f42a8 100644 --- a/client/src/telethon/_impl/client/client/bots.py +++ b/client/src/telethon/_impl/client/client/bots.py @@ -1,7 +1,9 @@ from __future__ import annotations from collections.abc import AsyncIterator -from typing import TYPE_CHECKING, Optional, Self +from typing import TYPE_CHECKING, Optional + +from typing_extensions import Self from ...session import PeerRef, UserRef from ...tl import abcs, functions, types diff --git a/client/src/telethon/_impl/client/client/client.py b/client/src/telethon/_impl/client/client/client.py index fd9cd1f9..3f2c5f23 100644 --- a/client/src/telethon/_impl/client/client/client.py +++ b/client/src/telethon/_impl/client/client/client.py @@ -4,7 +4,9 @@ import logging from collections.abc import AsyncIterator, Awaitable, Callable from pathlib import Path from types import TracebackType -from typing import Any, Literal, Optional, Self, Sequence, Type, TypeVar +from typing import Any, Literal, Optional, Sequence, Type, TypeVar + +from typing_extensions import Self from ....version import __version__ as default_version from ...mtsender import Connector, Sender diff --git a/client/src/telethon/_impl/client/client/messages.py b/client/src/telethon/_impl/client/client/messages.py index e19aee63..428993a7 100644 --- a/client/src/telethon/_impl/client/client/messages.py +++ b/client/src/telethon/_impl/client/client/messages.py @@ -2,7 +2,9 @@ from __future__ import annotations import datetime import sys -from typing import TYPE_CHECKING, Literal, Optional, Self +from typing import TYPE_CHECKING, Literal, Optional + +from typing_extensions import Self from ...session import ChannelRef, PeerRef from ...tl import abcs, functions, types diff --git a/client/src/telethon/_impl/client/events/event.py b/client/src/telethon/_impl/client/events/event.py index 3fae19ec..c2632d06 100644 --- a/client/src/telethon/_impl/client/events/event.py +++ b/client/src/telethon/_impl/client/events/event.py @@ -1,7 +1,9 @@ from __future__ import annotations import abc -from typing import TYPE_CHECKING, Optional, Self +from typing import TYPE_CHECKING, Optional + +from typing_extensions import Self from ...tl import abcs from ..types import NoPublicConstructor, Peer diff --git a/client/src/telethon/_impl/client/events/messages.py b/client/src/telethon/_impl/client/events/messages.py index 65cf42de..bb76f5e5 100644 --- a/client/src/telethon/_impl/client/events/messages.py +++ b/client/src/telethon/_impl/client/events/messages.py @@ -1,6 +1,8 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Optional, Self, Sequence +from typing import TYPE_CHECKING, Optional, Sequence + +from typing_extensions import Self from ...tl import abcs, types from ..types import Message, Peer, expand_peer, peer_id diff --git a/client/src/telethon/_impl/client/events/queries.py b/client/src/telethon/_impl/client/events/queries.py index 3736d9a4..842d9345 100644 --- a/client/src/telethon/_impl/client/events/queries.py +++ b/client/src/telethon/_impl/client/events/queries.py @@ -1,6 +1,8 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Optional, Self +from typing import TYPE_CHECKING, Optional + +from typing_extensions import Self from ...session import PeerRef from ...tl import abcs, functions, types diff --git a/client/src/telethon/_impl/client/types/async_list.py b/client/src/telethon/_impl/client/types/async_list.py index 560a4c86..d5e4a986 100644 --- a/client/src/telethon/_impl/client/types/async_list.py +++ b/client/src/telethon/_impl/client/types/async_list.py @@ -1,7 +1,9 @@ import abc from collections import deque from collections.abc import Generator -from typing import Any, Generic, Self, TypeVar +from typing import Any, Generic, TypeVar + +from typing_extensions import Self T = TypeVar("T") diff --git a/client/src/telethon/_impl/client/types/dialog.py b/client/src/telethon/_impl/client/types/dialog.py index 673fbf78..a9b9b236 100644 --- a/client/src/telethon/_impl/client/types/dialog.py +++ b/client/src/telethon/_impl/client/types/dialog.py @@ -1,6 +1,8 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Optional, Self +from typing import TYPE_CHECKING, Optional + +from typing_extensions import Self from ...tl import abcs, types from .draft import Draft diff --git a/client/src/telethon/_impl/client/types/draft.py b/client/src/telethon/_impl/client/types/draft.py index 9368044d..42c68905 100644 --- a/client/src/telethon/_impl/client/types/draft.py +++ b/client/src/telethon/_impl/client/types/draft.py @@ -1,7 +1,9 @@ from __future__ import annotations import datetime -from typing import TYPE_CHECKING, Optional, Self +from typing import TYPE_CHECKING, Optional + +from typing_extensions import Self from ...session import PeerRef from ...tl import abcs, functions, types diff --git a/client/src/telethon/_impl/client/types/file.py b/client/src/telethon/_impl/client/types/file.py index 566e4aa4..2072f489 100644 --- a/client/src/telethon/_impl/client/types/file.py +++ b/client/src/telethon/_impl/client/types/file.py @@ -6,7 +6,9 @@ from collections.abc import Coroutine from inspect import isawaitable from io import BufferedWriter from pathlib import Path -from typing import TYPE_CHECKING, Any, Optional, Protocol, Self, Sequence +from typing import TYPE_CHECKING, Any, Optional, Protocol, Sequence + +from typing_extensions import Self from ...tl import abcs, types from .meta import NoPublicConstructor diff --git a/client/src/telethon/_impl/client/types/forward_info.py b/client/src/telethon/_impl/client/types/forward_info.py index 5a2976e1..cdcda117 100644 --- a/client/src/telethon/_impl/client/types/forward_info.py +++ b/client/src/telethon/_impl/client/types/forward_info.py @@ -1,4 +1,4 @@ -from typing import Self +from typing_extensions import Self from ...tl import types from .meta import NoPublicConstructor diff --git a/client/src/telethon/_impl/client/types/keyboard.py b/client/src/telethon/_impl/client/types/keyboard.py index d560463c..3329bb30 100644 --- a/client/src/telethon/_impl/client/types/keyboard.py +++ b/client/src/telethon/_impl/client/types/keyboard.py @@ -30,6 +30,7 @@ class Keyboard: def __init__( self, buttons: list[AnyButton] | list[list[AnyButton]], + *, resize: bool, single_use: bool, selective: bool, @@ -55,4 +56,24 @@ class InlineKeyboard: self._raw = types.ReplyInlineMarkup(rows=_build_keyboard_rows(buttons)) -KeyboardType: TypeAlias = Keyboard | InlineKeyboard +class HiddenKeyboard: + __slots__ = ("_raw",) + + def __init__(self, *, selective: bool) -> None: + self._raw = types.ReplyKeyboardHide(selective=selective) + + +class ForcedReplyKeyboard: + __slots__ = ("_raw",) + + def __init__( + self, *, single_use: bool, selective: bool, placeholder: Optional[str] + ) -> None: + self._raw = types.ReplyKeyboardForceReply( + single_use=single_use, selective=selective, placeholder=placeholder + ) + + +KeyboardType: TypeAlias = ( + Keyboard | InlineKeyboard | HiddenKeyboard | ForcedReplyKeyboard +) diff --git a/client/src/telethon/_impl/client/types/link_preview.py b/client/src/telethon/_impl/client/types/link_preview.py index 2b9b3684..cb8541cb 100644 --- a/client/src/telethon/_impl/client/types/link_preview.py +++ b/client/src/telethon/_impl/client/types/link_preview.py @@ -1,4 +1,4 @@ -from typing import Self +from typing_extensions import Self from ...tl import types from .meta import NoPublicConstructor diff --git a/client/src/telethon/_impl/client/types/login_token.py b/client/src/telethon/_impl/client/types/login_token.py index afd33087..9b39cb67 100644 --- a/client/src/telethon/_impl/client/types/login_token.py +++ b/client/src/telethon/_impl/client/types/login_token.py @@ -1,4 +1,6 @@ -from typing import Optional, Self +from typing import Optional + +from typing_extensions import Self from ...tl import types from .meta import NoPublicConstructor diff --git a/client/src/telethon/_impl/client/types/message.py b/client/src/telethon/_impl/client/types/message.py index 4d37263d..62bd4ccf 100644 --- a/client/src/telethon/_impl/client/types/message.py +++ b/client/src/telethon/_impl/client/types/message.py @@ -2,7 +2,9 @@ from __future__ import annotations import datetime import time -from typing import TYPE_CHECKING, Any, Optional, Self, Sequence, cast +from typing import TYPE_CHECKING, Any, Optional, Sequence, cast + +from typing_extensions import Self from ...session import PeerRef from ...tl import abcs, types diff --git a/client/src/telethon/_impl/client/types/participant.py b/client/src/telethon/_impl/client/types/participant.py index ce7937b5..80013534 100644 --- a/client/src/telethon/_impl/client/types/participant.py +++ b/client/src/telethon/_impl/client/types/participant.py @@ -1,7 +1,9 @@ from __future__ import annotations import datetime -from typing import TYPE_CHECKING, Optional, Self, Sequence +from typing import TYPE_CHECKING, Optional, Sequence + +from typing_extensions import Self from ...session import ChannelRef, GroupRef from ...tl import abcs, types diff --git a/client/src/telethon/_impl/client/types/password_token.py b/client/src/telethon/_impl/client/types/password_token.py index cfcf4365..a3ca1be3 100644 --- a/client/src/telethon/_impl/client/types/password_token.py +++ b/client/src/telethon/_impl/client/types/password_token.py @@ -1,4 +1,4 @@ -from typing import Self +from typing_extensions import Self from ...tl import types from .meta import NoPublicConstructor diff --git a/client/src/telethon/_impl/client/types/peer/channel.py b/client/src/telethon/_impl/client/types/peer/channel.py index 069b0040..56ff94dd 100644 --- a/client/src/telethon/_impl/client/types/peer/channel.py +++ b/client/src/telethon/_impl/client/types/peer/channel.py @@ -1,4 +1,6 @@ -from typing import Optional, Self +from typing import Optional + +from typing_extensions import Self from ....session import ChannelRef from ....tl import abcs, types diff --git a/client/src/telethon/_impl/client/types/peer/group.py b/client/src/telethon/_impl/client/types/peer/group.py index 5dce7ef3..9a41083d 100644 --- a/client/src/telethon/_impl/client/types/peer/group.py +++ b/client/src/telethon/_impl/client/types/peer/group.py @@ -1,7 +1,9 @@ from __future__ import annotations import datetime -from typing import TYPE_CHECKING, Optional, Self, Sequence +from typing import TYPE_CHECKING, Optional, Sequence + +from typing_extensions import Self from ....session import ChannelRef, GroupRef from ....tl import abcs, types diff --git a/client/src/telethon/_impl/client/types/peer/user.py b/client/src/telethon/_impl/client/types/peer/user.py index 8ae92273..912c48ea 100644 --- a/client/src/telethon/_impl/client/types/peer/user.py +++ b/client/src/telethon/_impl/client/types/peer/user.py @@ -1,4 +1,6 @@ -from typing import Optional, Self +from typing import Optional + +from typing_extensions import Self from ....session import UserRef from ....tl import abcs, types diff --git a/client/src/telethon/_impl/crypto/auth_key.py b/client/src/telethon/_impl/crypto/auth_key.py index f2c2cdc7..90afc306 100644 --- a/client/src/telethon/_impl/crypto/auth_key.py +++ b/client/src/telethon/_impl/crypto/auth_key.py @@ -1,6 +1,7 @@ from dataclasses import dataclass from hashlib import sha1 -from typing import Self + +from typing_extensions import Self @dataclass diff --git a/client/src/telethon/_impl/mtproto/mtp/types.py b/client/src/telethon/_impl/mtproto/mtp/types.py index 0dc23378..0f624c7c 100644 --- a/client/src/telethon/_impl/mtproto/mtp/types.py +++ b/client/src/telethon/_impl/mtproto/mtp/types.py @@ -1,7 +1,9 @@ import logging import re from abc import ABC, abstractmethod -from typing import NewType, Optional, Self +from typing import NewType, Optional + +from typing_extensions import Self from ...tl.mtproto.types import RpcError as GeneratedRpcError diff --git a/client/src/telethon/_impl/mtsender/sender.py b/client/src/telethon/_impl/mtsender/sender.py index 2cbb2ce8..72e1652a 100644 --- a/client/src/telethon/_impl/mtsender/sender.py +++ b/client/src/telethon/_impl/mtsender/sender.py @@ -6,7 +6,9 @@ from abc import ABC from asyncio import FIRST_COMPLETED, Event, Future from collections.abc import Iterator from dataclasses import dataclass -from typing import Generic, Optional, Protocol, Self, Type, TypeVar +from typing import Generic, Optional, Protocol, Type, TypeVar + +from typing_extensions import Self from ..crypto import AuthKey from ..mtproto import ( diff --git a/client/src/telethon/_impl/session/chat/peer_ref.py b/client/src/telethon/_impl/session/chat/peer_ref.py index 9752e1de..eee2f839 100644 --- a/client/src/telethon/_impl/session/chat/peer_ref.py +++ b/client/src/telethon/_impl/session/chat/peer_ref.py @@ -4,7 +4,9 @@ import abc import base64 import re import struct -from typing import Optional, Self, TypeAlias +from typing import Optional, TypeAlias + +from typing_extensions import Self from ...tl import abcs, types diff --git a/client/src/telethon/_impl/tl/core/serializable.py b/client/src/telethon/_impl/tl/core/serializable.py index 8af4e56c..d912649d 100644 --- a/client/src/telethon/_impl/tl/core/serializable.py +++ b/client/src/telethon/_impl/tl/core/serializable.py @@ -1,6 +1,8 @@ import abc import struct -from typing import Protocol, Self +from typing import Protocol + +from typing_extensions import Self from .reader import Reader diff --git a/generator/pyproject.toml b/generator/pyproject.toml index 84be3cbd..ec85efaa 100644 --- a/generator/pyproject.toml +++ b/generator/pyproject.toml @@ -20,7 +20,7 @@ classifiers = [ "Typing :: Typed", ] dynamic = ["version"] - +dependencies = ["typing-extensions~=4.12.2"] [project.optional-dependencies] dev = ["pytest~=7.3"] diff --git a/generator/src/telethon_generator/_impl/codegen/generator.py b/generator/src/telethon_generator/_impl/codegen/generator.py index d3fbe128..713318a9 100644 --- a/generator/src/telethon_generator/_impl/codegen/generator.py +++ b/generator/src/telethon_generator/_impl/codegen/generator.py @@ -97,7 +97,8 @@ def generate(fs: FakeFs, tl: ParsedTl) -> None: "# pyright: reportUnusedImport=false, reportConstantRedefinition=false" ) writer.write("import struct") - writer.write("from typing import Optional, Self, Sequence") + writer.write("from typing import Optional, Sequence") + writer.write("from typing_extensions import Self") writer.write("from .. import abcs") writer.write("from ..core import Reader, Serializable, serialize_bytes_to") writer.write("_bytes = bytes | bytearray | memoryview") @@ -163,7 +164,8 @@ def generate(fs: FakeFs, tl: ParsedTl) -> None: if function_path not in fs: writer.write("# pyright: reportUnusedImport=false") writer.write("import struct") - writer.write("from typing import Optional, Self, Sequence") + writer.write("from typing import Optional, Sequence") + writer.write("from typing_extensions import Self") writer.write("from .. import abcs") writer.write("from ..core import Request, serialize_bytes_to") writer.write("_bytes = bytes | bytearray | memoryview") @@ -192,14 +194,14 @@ def generate(fs: FakeFs, tl: ParsedTl) -> None: writer.write( "from .core import Serializable, Reader, deserialize_bool, deserialize_i32_list, deserialize_i64_list, deserialize_identity, single_deserializer, list_deserializer" ) - writer.write("from typing import cast, Type") + writer.write("from typing import Final, Type") writer.write(f"LAYER = {tl.layer!r}") writer.write( - "TYPE_MAPPING = {t.constructor_id(): t for t in cast(tuple[Type[Serializable]], (" + "TYPE_MAPPING: Final[dict[int, Type[Serializable]]] = {t.constructor_id(): t for t in (" ) for name in sorted(generated_type_names): writer.write(f" types.{name},") - writer.write("))}") + writer.write(")}") writer.write("RESPONSE_MAPPING = {") for functiondef in tl.functiondefs: writer.write( diff --git a/generator/src/telethon_generator/_impl/tl_parser/tl/definition.py b/generator/src/telethon_generator/_impl/tl_parser/tl/definition.py index cb234740..731b977c 100644 --- a/generator/src/telethon_generator/_impl/tl_parser/tl/definition.py +++ b/generator/src/telethon_generator/_impl/tl_parser/tl/definition.py @@ -1,5 +1,6 @@ from dataclasses import dataclass -from typing import Self + +from typing_extensions import Self from ..utils import infer_id from .parameter import Parameter, TypeDefNotImplementedError diff --git a/generator/src/telethon_generator/_impl/tl_parser/tl/flag.py b/generator/src/telethon_generator/_impl/tl_parser/tl/flag.py index 23a39c1f..c5dcbd88 100644 --- a/generator/src/telethon_generator/_impl/tl_parser/tl/flag.py +++ b/generator/src/telethon_generator/_impl/tl_parser/tl/flag.py @@ -1,5 +1,6 @@ from dataclasses import dataclass -from typing import Self + +from typing_extensions import Self @dataclass diff --git a/generator/src/telethon_generator/_impl/tl_parser/tl/parameter.py b/generator/src/telethon_generator/_impl/tl_parser/tl/parameter.py index 7c204623..2f44403a 100644 --- a/generator/src/telethon_generator/_impl/tl_parser/tl/parameter.py +++ b/generator/src/telethon_generator/_impl/tl_parser/tl/parameter.py @@ -1,5 +1,6 @@ from dataclasses import dataclass -from typing import Self + +from typing_extensions import Self from .parameter_type import BaseParameter diff --git a/generator/src/telethon_generator/_impl/tl_parser/tl/ty.py b/generator/src/telethon_generator/_impl/tl_parser/tl/ty.py index b12d86d4..9f131914 100644 --- a/generator/src/telethon_generator/_impl/tl_parser/tl/ty.py +++ b/generator/src/telethon_generator/_impl/tl_parser/tl/ty.py @@ -1,6 +1,8 @@ from collections.abc import Iterator from dataclasses import dataclass -from typing import Optional, Self +from typing import Optional + +from typing_extensions import Self @dataclass