Fix type annotation for keyboard buttons (#4438)

This commit is contained in:
Jahongir Qurbonov 2024-08-21 22:28:16 +05:00 committed by GitHub
parent babeba46d3
commit 6253d28143
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,11 +1,14 @@
from typing import Optional, TypeAlias from typing import Generic, Optional, TypeAlias, TypeVar
from ...tl import abcs, types from ...tl import abcs, types
from .buttons.button import Button from .buttons import Button, InlineButton
AnyButton = TypeVar("AnyButton", bound=Button)
AnyInlineButton = TypeVar("AnyInlineButton", bound=InlineButton)
def _build_keyboard_rows( def _build_keyboard_rows(
btns: list[Button] | list[list[Button]], btns: list[AnyButton] | list[list[AnyButton]],
) -> list[abcs.KeyboardButtonRow]: ) -> list[abcs.KeyboardButtonRow]:
# list[button] -> list[list[button]] # list[button] -> list[list[button]]
# This does allow for "invalid" inputs (mixing lists and non-lists), but that's acceptable. # This does allow for "invalid" inputs (mixing lists and non-lists), but that's acceptable.
@ -21,12 +24,12 @@ def _build_keyboard_rows(
] ]
class Keyboard: class Keyboard(Generic[AnyButton]):
__slots__ = ("_raw",) __slots__ = ("_raw",)
def __init__( def __init__(
self, self,
buttons: list[Button] | list[list[Button]], buttons: list[AnyButton] | list[list[AnyButton]],
resize: bool, resize: bool,
single_use: bool, single_use: bool,
selective: bool, selective: bool,
@ -43,10 +46,12 @@ class Keyboard:
) )
class InlineKeyboard: class InlineKeyboard(Generic[AnyInlineButton]):
__slots__ = ("_raw",) __slots__ = ("_raw",)
def __init__(self, buttons: list[Button] | list[list[Button]]) -> None: def __init__(
self, buttons: list[AnyInlineButton] | list[list[AnyInlineButton]]
) -> None:
self._raw = types.ReplyInlineMarkup(rows=_build_keyboard_rows(buttons)) self._raw = types.ReplyInlineMarkup(rows=_build_keyboard_rows(buttons))