Add HiddenKeyboard and ForcedReplyKeyboard custom types and improve type annotation (#4481)

This commit is contained in:
Jahongir Qurbonov 2024-10-09 00:13:13 +05:00 committed by GitHub
parent 61642c04a5
commit 771954d010
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
32 changed files with 105 additions and 35 deletions

View File

@ -22,6 +22,7 @@ dependencies = [
"pyaes~=1.6", "pyaes~=1.6",
"rsa~=4.9", "rsa~=4.9",
"markdown-it-py~=3.0", "markdown-it-py~=3.0",
"typing-extensions~=4.12.2",
] ]
dynamic = ["version"] dynamic = ["version"]

View File

@ -1,7 +1,9 @@
from __future__ import annotations from __future__ import annotations
from collections.abc import AsyncIterator 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 ...session import PeerRef, UserRef
from ...tl import abcs, functions, types from ...tl import abcs, functions, types

View File

@ -4,7 +4,9 @@ import logging
from collections.abc import AsyncIterator, Awaitable, Callable from collections.abc import AsyncIterator, Awaitable, Callable
from pathlib import Path from pathlib import Path
from types import TracebackType 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 ....version import __version__ as default_version
from ...mtsender import Connector, Sender from ...mtsender import Connector, Sender

View File

@ -2,7 +2,9 @@ from __future__ import annotations
import datetime import datetime
import sys 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 ...session import ChannelRef, PeerRef
from ...tl import abcs, functions, types from ...tl import abcs, functions, types

View File

@ -1,7 +1,9 @@
from __future__ import annotations from __future__ import annotations
import abc 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 ...tl import abcs
from ..types import NoPublicConstructor, Peer from ..types import NoPublicConstructor, Peer

View File

@ -1,6 +1,8 @@
from __future__ import annotations 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 ...tl import abcs, types
from ..types import Message, Peer, expand_peer, peer_id from ..types import Message, Peer, expand_peer, peer_id

View File

@ -1,6 +1,8 @@
from __future__ import annotations 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 ...session import PeerRef
from ...tl import abcs, functions, types from ...tl import abcs, functions, types

View File

@ -1,7 +1,9 @@
import abc import abc
from collections import deque from collections import deque
from collections.abc import Generator 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") T = TypeVar("T")

View File

@ -1,6 +1,8 @@
from __future__ import annotations 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 ...tl import abcs, types
from .draft import Draft from .draft import Draft

View File

@ -1,7 +1,9 @@
from __future__ import annotations from __future__ import annotations
import datetime 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 ...session import PeerRef
from ...tl import abcs, functions, types from ...tl import abcs, functions, types

View File

@ -6,7 +6,9 @@ from collections.abc import Coroutine
from inspect import isawaitable from inspect import isawaitable
from io import BufferedWriter from io import BufferedWriter
from pathlib import Path 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 ...tl import abcs, types
from .meta import NoPublicConstructor from .meta import NoPublicConstructor

View File

@ -1,4 +1,4 @@
from typing import Self from typing_extensions import Self
from ...tl import types from ...tl import types
from .meta import NoPublicConstructor from .meta import NoPublicConstructor

View File

@ -30,6 +30,7 @@ class Keyboard:
def __init__( def __init__(
self, self,
buttons: list[AnyButton] | list[list[AnyButton]], buttons: list[AnyButton] | list[list[AnyButton]],
*,
resize: bool, resize: bool,
single_use: bool, single_use: bool,
selective: bool, selective: bool,
@ -55,4 +56,24 @@ class InlineKeyboard:
self._raw = types.ReplyInlineMarkup(rows=_build_keyboard_rows(buttons)) 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
)

View File

@ -1,4 +1,4 @@
from typing import Self from typing_extensions import Self
from ...tl import types from ...tl import types
from .meta import NoPublicConstructor from .meta import NoPublicConstructor

View File

@ -1,4 +1,6 @@
from typing import Optional, Self from typing import Optional
from typing_extensions import Self
from ...tl import types from ...tl import types
from .meta import NoPublicConstructor from .meta import NoPublicConstructor

View File

@ -2,7 +2,9 @@ from __future__ import annotations
import datetime import datetime
import time 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 ...session import PeerRef
from ...tl import abcs, types from ...tl import abcs, types

View File

@ -1,7 +1,9 @@
from __future__ import annotations from __future__ import annotations
import datetime 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 ...session import ChannelRef, GroupRef
from ...tl import abcs, types from ...tl import abcs, types

View File

@ -1,4 +1,4 @@
from typing import Self from typing_extensions import Self
from ...tl import types from ...tl import types
from .meta import NoPublicConstructor from .meta import NoPublicConstructor

View File

@ -1,4 +1,6 @@
from typing import Optional, Self from typing import Optional
from typing_extensions import Self
from ....session import ChannelRef from ....session import ChannelRef
from ....tl import abcs, types from ....tl import abcs, types

View File

@ -1,7 +1,9 @@
from __future__ import annotations from __future__ import annotations
import datetime 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 ....session import ChannelRef, GroupRef
from ....tl import abcs, types from ....tl import abcs, types

View File

@ -1,4 +1,6 @@
from typing import Optional, Self from typing import Optional
from typing_extensions import Self
from ....session import UserRef from ....session import UserRef
from ....tl import abcs, types from ....tl import abcs, types

View File

@ -1,6 +1,7 @@
from dataclasses import dataclass from dataclasses import dataclass
from hashlib import sha1 from hashlib import sha1
from typing import Self
from typing_extensions import Self
@dataclass @dataclass

View File

@ -1,7 +1,9 @@
import logging import logging
import re import re
from abc import ABC, abstractmethod 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 from ...tl.mtproto.types import RpcError as GeneratedRpcError

View File

@ -6,7 +6,9 @@ from abc import ABC
from asyncio import FIRST_COMPLETED, Event, Future from asyncio import FIRST_COMPLETED, Event, Future
from collections.abc import Iterator from collections.abc import Iterator
from dataclasses import dataclass 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 ..crypto import AuthKey
from ..mtproto import ( from ..mtproto import (

View File

@ -4,7 +4,9 @@ import abc
import base64 import base64
import re import re
import struct import struct
from typing import Optional, Self, TypeAlias from typing import Optional, TypeAlias
from typing_extensions import Self
from ...tl import abcs, types from ...tl import abcs, types

View File

@ -1,6 +1,8 @@
import abc import abc
import struct import struct
from typing import Protocol, Self from typing import Protocol
from typing_extensions import Self
from .reader import Reader from .reader import Reader

View File

@ -20,7 +20,7 @@ classifiers = [
"Typing :: Typed", "Typing :: Typed",
] ]
dynamic = ["version"] dynamic = ["version"]
dependencies = ["typing-extensions~=4.12.2"]
[project.optional-dependencies] [project.optional-dependencies]
dev = ["pytest~=7.3"] dev = ["pytest~=7.3"]

View File

@ -97,7 +97,8 @@ def generate(fs: FakeFs, tl: ParsedTl) -> None:
"# pyright: reportUnusedImport=false, reportConstantRedefinition=false" "# pyright: reportUnusedImport=false, reportConstantRedefinition=false"
) )
writer.write("import struct") 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 .. import abcs")
writer.write("from ..core import Reader, Serializable, serialize_bytes_to") writer.write("from ..core import Reader, Serializable, serialize_bytes_to")
writer.write("_bytes = bytes | bytearray | memoryview") writer.write("_bytes = bytes | bytearray | memoryview")
@ -163,7 +164,8 @@ def generate(fs: FakeFs, tl: ParsedTl) -> None:
if function_path not in fs: if function_path not in fs:
writer.write("# pyright: reportUnusedImport=false") writer.write("# pyright: reportUnusedImport=false")
writer.write("import struct") 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 .. import abcs")
writer.write("from ..core import Request, serialize_bytes_to") writer.write("from ..core import Request, serialize_bytes_to")
writer.write("_bytes = bytes | bytearray | memoryview") writer.write("_bytes = bytes | bytearray | memoryview")
@ -192,14 +194,14 @@ def generate(fs: FakeFs, tl: ParsedTl) -> None:
writer.write( writer.write(
"from .core import Serializable, Reader, deserialize_bool, deserialize_i32_list, deserialize_i64_list, deserialize_identity, single_deserializer, list_deserializer" "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(f"LAYER = {tl.layer!r}")
writer.write( 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): for name in sorted(generated_type_names):
writer.write(f" types.{name},") writer.write(f" types.{name},")
writer.write("))}") writer.write(")}")
writer.write("RESPONSE_MAPPING = {") writer.write("RESPONSE_MAPPING = {")
for functiondef in tl.functiondefs: for functiondef in tl.functiondefs:
writer.write( writer.write(

View File

@ -1,5 +1,6 @@
from dataclasses import dataclass from dataclasses import dataclass
from typing import Self
from typing_extensions import Self
from ..utils import infer_id from ..utils import infer_id
from .parameter import Parameter, TypeDefNotImplementedError from .parameter import Parameter, TypeDefNotImplementedError

View File

@ -1,5 +1,6 @@
from dataclasses import dataclass from dataclasses import dataclass
from typing import Self
from typing_extensions import Self
@dataclass @dataclass

View File

@ -1,5 +1,6 @@
from dataclasses import dataclass from dataclasses import dataclass
from typing import Self
from typing_extensions import Self
from .parameter_type import BaseParameter from .parameter_type import BaseParameter

View File

@ -1,6 +1,8 @@
from collections.abc import Iterator from collections.abc import Iterator
from dataclasses import dataclass from dataclasses import dataclass
from typing import Optional, Self from typing import Optional
from typing_extensions import Self
@dataclass @dataclass