From d927cc4fe9b0e67026d63d8d5f889219b5eb0ab0 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Sun, 8 Sep 2024 21:02:23 +0200 Subject: [PATCH 01/16] Improve Type Completeness --- telegram/ext/_application.py | 12 +++++---- telegram/ext/_callbackcontext.py | 16 +++++++----- telegram/ext/_handlers/basehandler.py | 7 ++--- .../_handlers/businessconnectionhandler.py | 4 +-- .../businessmessagesdeletedhandler.py | 4 +-- .../ext/_handlers/callbackqueryhandler.py | 4 +-- telegram/ext/_handlers/chatboosthandler.py | 8 +++--- .../ext/_handlers/chatjoinrequesthandler.py | 4 +-- telegram/ext/_handlers/chatmemberhandler.py | 4 +-- .../_handlers/choseninlineresulthandler.py | 4 +-- telegram/ext/_handlers/commandhandler.py | 4 +-- telegram/ext/_handlers/conversationhandler.py | 26 +++++++++---------- telegram/ext/_handlers/inlinequeryhandler.py | 4 +-- telegram/ext/_handlers/messagehandler.py | 4 +-- .../ext/_handlers/messagereactionhandler.py | 4 +-- telegram/ext/_handlers/pollanswerhandler.py | 4 +-- telegram/ext/_handlers/pollhandler.py | 4 +-- .../ext/_handlers/precheckoutqueryhandler.py | 4 +-- telegram/ext/_handlers/prefixhandler.py | 4 +-- .../ext/_handlers/shippingqueryhandler.py | 4 +-- .../ext/_handlers/stringcommandhandler.py | 4 +-- telegram/ext/_handlers/stringregexhandler.py | 4 +-- telegram/ext/_handlers/typehandler.py | 4 +-- 23 files changed, 74 insertions(+), 67 deletions(-) diff --git a/telegram/ext/_application.py b/telegram/ext/_application.py index a1013ce570a..86e38b1de9f 100644 --- a/telegram/ext/_application.py +++ b/telegram/ext/_application.py @@ -323,7 +323,7 @@ def __init__( self.update_queue: asyncio.Queue[object] = update_queue self.context_types: ContextTypes[CCT, UD, CD, BD] = context_types self.updater: Optional[Updater] = updater - self.handlers: Dict[int, List[BaseHandler[Any, CCT]]] = {} + self.handlers: Dict[int, List[BaseHandler[Any, CCT, Any]]] = {} self.error_handlers: Dict[ HandlerCallback[object, CCT, None], Union[bool, DefaultValue[bool]] ] = {} @@ -1352,7 +1352,7 @@ async def process_update(self, update: object) -> None: # (in __create_task_callback) self._mark_for_persistence_update(update=update) - def add_handler(self, handler: BaseHandler[Any, CCT], group: int = DEFAULT_GROUP) -> None: + def add_handler(self, handler: BaseHandler[Any, CCT, Any], group: int = DEFAULT_GROUP) -> None: """Register a handler. TL;DR: Order and priority counts. 0 or 1 handlers per group will be used. End handling of @@ -1420,8 +1420,8 @@ def add_handler(self, handler: BaseHandler[Any, CCT], group: int = DEFAULT_GROUP def add_handlers( self, handlers: Union[ - Union[List[BaseHandler[Any, CCT]], Tuple[BaseHandler[Any, CCT]]], - Dict[int, Union[List[BaseHandler[Any, CCT]], Tuple[BaseHandler[Any, CCT]]]], + Union[List[BaseHandler[Any, CCT, Any]], Tuple[BaseHandler[Any, CCT, Any]]], + Dict[int, Union[List[BaseHandler[Any, CCT, Any]], Tuple[BaseHandler[Any, CCT, Any]]]], ], group: Union[int, DefaultValue[int]] = _DEFAULT_0, ) -> None: @@ -1469,7 +1469,9 @@ def add_handlers( "dictionary where the keys are groups and values are sequences of handlers." ) - def remove_handler(self, handler: BaseHandler[Any, CCT], group: int = DEFAULT_GROUP) -> None: + def remove_handler( + self, handler: BaseHandler[Any, CCT, Any], group: int = DEFAULT_GROUP + ) -> None: """Remove a handler from the specified group. Args: diff --git a/telegram/ext/_callbackcontext.py b/telegram/ext/_callbackcontext.py index ea708e0e6b8..b976c9c0bba 100644 --- a/telegram/ext/_callbackcontext.py +++ b/telegram/ext/_callbackcontext.py @@ -29,6 +29,7 @@ NoReturn, Optional, Type, + TypeVar, Union, ) @@ -49,6 +50,9 @@ "/wiki/Storing-bot%2C-user-and-chat-related-data" ) +# something like poor mans "tying.Self" for py<3.11 +ST = TypeVar("ST", bound="CallbackContext[Any, Any, Any, Any]") + class CallbackContext(Generic[BT, UD, CD, BD]): """ @@ -133,24 +137,24 @@ class CallbackContext(Generic[BT, UD, CD, BD]): ) def __init__( - self: "CCT", - application: "Application[BT, CCT, UD, CD, BD, Any]", + self: ST, + application: "Application[BT, ST, UD, CD, BD, Any]", chat_id: Optional[int] = None, user_id: Optional[int] = None, ): - self._application: Application[BT, CCT, UD, CD, BD, Any] = application + self._application: Application[BT, ST, UD, CD, BD, Any] = application self._chat_id: Optional[int] = chat_id self._user_id: Optional[int] = user_id self.args: Optional[List[str]] = None self.matches: Optional[List[Match[str]]] = None self.error: Optional[Exception] = None - self.job: Optional[Job[CCT]] = None + self.job: Optional[Job[Any]] = None self.coroutine: Optional[ Union[Generator[Optional[Future[object]], None, Any], Awaitable[Any]] ] = None @property - def application(self) -> "Application[BT, CCT, UD, CD, BD, Any]": + def application(self) -> "Application[BT, ST, UD, CD, BD, Any]": """:class:`telegram.ext.Application`: The application associated with this context.""" return self._application @@ -398,7 +402,7 @@ def bot(self) -> BT: return self._application.bot @property - def job_queue(self) -> Optional["JobQueue[CCT]"]: + def job_queue(self) -> Optional["JobQueue[ST]"]: """ :class:`telegram.ext.JobQueue`: The :class:`JobQueue` used by the :class:`telegram.ext.Application`. diff --git a/telegram/ext/_handlers/basehandler.py b/telegram/ext/_handlers/basehandler.py index ea0d9b1640f..bf6f5f155df 100644 --- a/telegram/ext/_handlers/basehandler.py +++ b/telegram/ext/_handlers/basehandler.py @@ -32,14 +32,14 @@ UT = TypeVar("UT") -class BaseHandler(Generic[UT, CCT], ABC): +class BaseHandler(Generic[UT, CCT, RT], ABC): """The base class for all update handlers. Create custom handlers by inheriting from it. Warning: When setting :paramref:`block` to :obj:`False`, you cannot rely on adding custom attributes to :class:`telegram.ext.CallbackContext`. See its docs for more info. - This class is a :class:`~typing.Generic` class and accepts two type variables: + This class is a :class:`~typing.Generic` class and accepts three type variables: 1. The type of the updates that this handler will handle. Must coincide with the type of the first argument of :paramref:`callback`. :meth:`check_update` must only accept @@ -54,6 +54,7 @@ class BaseHandler(Generic[UT, CCT], ABC): For this type variable, one should usually provide a :class:`~typing.TypeVar` that is also used for the mentioned method arguments. That way, a type checker can check whether this handler fits the definition of the :class:`~Application`. + 3. The return type of the :paramref:`callback` function accepted by this handler. .. seealso:: :wiki:`Types of Handlers ` @@ -89,7 +90,7 @@ async def callback(update: Update, context: CallbackContext) ) def __init__( - self, + self: "BaseHandler[UT, CCT, RT]", callback: HandlerCallback[UT, CCT, RT], block: DVType[bool] = DEFAULT_TRUE, ): diff --git a/telegram/ext/_handlers/businessconnectionhandler.py b/telegram/ext/_handlers/businessconnectionhandler.py index c8cb3e843d0..e5d09c77cf1 100644 --- a/telegram/ext/_handlers/businessconnectionhandler.py +++ b/telegram/ext/_handlers/businessconnectionhandler.py @@ -29,7 +29,7 @@ RT = TypeVar("RT") -class BusinessConnectionHandler(BaseHandler[Update, CCT]): +class BusinessConnectionHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle Telegram :attr:`Business Connections `. @@ -65,7 +65,7 @@ async def callback(update: Update, context: CallbackContext) ) def __init__( - self, + self: "BusinessConnectionHandler[CCT, RT]", callback: HandlerCallback[Update, CCT, RT], user_id: Optional[SCT[int]] = None, username: Optional[SCT[str]] = None, diff --git a/telegram/ext/_handlers/businessmessagesdeletedhandler.py b/telegram/ext/_handlers/businessmessagesdeletedhandler.py index 0ceb58fe05c..c041bb0adc0 100644 --- a/telegram/ext/_handlers/businessmessagesdeletedhandler.py +++ b/telegram/ext/_handlers/businessmessagesdeletedhandler.py @@ -29,7 +29,7 @@ RT = TypeVar("RT") -class BusinessMessagesDeletedHandler(BaseHandler[Update, CCT]): +class BusinessMessagesDeletedHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle :attr:`deleted Telegram Business messages `. @@ -65,7 +65,7 @@ async def callback(update: Update, context: CallbackContext) ) def __init__( - self, + self: "BusinessMessagesDeletedHandler[CCT, RT]", callback: HandlerCallback[Update, CCT, RT], chat_id: Optional[SCT[int]] = None, username: Optional[SCT[str]] = None, diff --git a/telegram/ext/_handlers/callbackqueryhandler.py b/telegram/ext/_handlers/callbackqueryhandler.py index a149739a2fb..5e0a0a12aa0 100644 --- a/telegram/ext/_handlers/callbackqueryhandler.py +++ b/telegram/ext/_handlers/callbackqueryhandler.py @@ -33,7 +33,7 @@ RT = TypeVar("RT") -class CallbackQueryHandler(BaseHandler[Update, CCT]): +class CallbackQueryHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle Telegram :attr:`callback queries `. Optionally based on a regex. @@ -125,7 +125,7 @@ async def callback(update: Update, context: CallbackContext) __slots__ = ("game_pattern", "pattern") def __init__( - self, + self: "CallbackQueryHandler[CCT, RT]", callback: HandlerCallback[Update, CCT, RT], pattern: Optional[ Union[str, Pattern[str], type, Callable[[object], Optional[bool]]] diff --git a/telegram/ext/_handlers/chatboosthandler.py b/telegram/ext/_handlers/chatboosthandler.py index 9d7219632a1..7a2dd1be6a6 100644 --- a/telegram/ext/_handlers/chatboosthandler.py +++ b/telegram/ext/_handlers/chatboosthandler.py @@ -23,10 +23,10 @@ from telegram import Update from telegram.ext._handlers.basehandler import BaseHandler from telegram.ext._utils._update_parsing import parse_chat_id, parse_username -from telegram.ext._utils.types import CCT, HandlerCallback +from telegram.ext._utils.types import CCT, RT, HandlerCallback -class ChatBoostHandler(BaseHandler[Update, CCT]): +class ChatBoostHandler(BaseHandler[Update, CCT, RT]): """ Handler class to handle Telegram updates that contain a chat boost. @@ -84,8 +84,8 @@ async def callback(update: Update, context: CallbackContext) and :attr:`telegram.Update.removed_chat_boost`.""" def __init__( - self, - callback: HandlerCallback[Update, CCT, None], + self: "ChatBoostHandler[CCT, RT]", + callback: HandlerCallback[Update, CCT, RT], chat_boost_types: int = CHAT_BOOST, chat_id: Optional[int] = None, chat_username: Optional[str] = None, diff --git a/telegram/ext/_handlers/chatjoinrequesthandler.py b/telegram/ext/_handlers/chatjoinrequesthandler.py index 7007f61ab03..04576e63c32 100644 --- a/telegram/ext/_handlers/chatjoinrequesthandler.py +++ b/telegram/ext/_handlers/chatjoinrequesthandler.py @@ -28,7 +28,7 @@ from telegram.ext._utils.types import CCT, HandlerCallback -class ChatJoinRequestHandler(BaseHandler[Update, CCT]): +class ChatJoinRequestHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle Telegram updates that contain :attr:`telegram.Update.chat_join_request`. @@ -81,7 +81,7 @@ async def callback(update: Update, context: CallbackContext) ) def __init__( - self, + self: "ChatJoinRequestHandler[CCT, RT]", callback: HandlerCallback[Update, CCT, RT], chat_id: Optional[SCT[int]] = None, username: Optional[SCT[str]] = None, diff --git a/telegram/ext/_handlers/chatmemberhandler.py b/telegram/ext/_handlers/chatmemberhandler.py index 592361b64c5..cf8bb28db10 100644 --- a/telegram/ext/_handlers/chatmemberhandler.py +++ b/telegram/ext/_handlers/chatmemberhandler.py @@ -29,7 +29,7 @@ RT = TypeVar("RT") -class ChatMemberHandler(BaseHandler[Update, CCT]): +class ChatMemberHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle Telegram updates that contain a chat member update. Warning: @@ -87,7 +87,7 @@ async def callback(update: Update, context: CallbackContext) and :attr:`telegram.Update.chat_member`.""" def __init__( - self, + self: "ChatMemberHandler[CCT, RT]", callback: HandlerCallback[Update, CCT, RT], chat_member_types: int = MY_CHAT_MEMBER, block: DVType[bool] = DEFAULT_TRUE, diff --git a/telegram/ext/_handlers/choseninlineresulthandler.py b/telegram/ext/_handlers/choseninlineresulthandler.py index db7a8721448..feac28ba658 100644 --- a/telegram/ext/_handlers/choseninlineresulthandler.py +++ b/telegram/ext/_handlers/choseninlineresulthandler.py @@ -32,7 +32,7 @@ from telegram.ext import Application -class ChosenInlineResultHandler(BaseHandler[Update, CCT]): +class ChosenInlineResultHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle Telegram updates that contain :attr:`telegram.Update.chosen_inline_result`. @@ -76,7 +76,7 @@ async def callback(update: Update, context: CallbackContext) __slots__ = ("pattern",) def __init__( - self, + self: "ChosenInlineResultHandler[CCT, RT]", callback: HandlerCallback[Update, CCT, RT], block: DVType[bool] = DEFAULT_TRUE, pattern: Optional[Union[str, Pattern[str]]] = None, diff --git a/telegram/ext/_handlers/commandhandler.py b/telegram/ext/_handlers/commandhandler.py index edad3963aad..54d01acd6d0 100644 --- a/telegram/ext/_handlers/commandhandler.py +++ b/telegram/ext/_handlers/commandhandler.py @@ -33,7 +33,7 @@ RT = TypeVar("RT") -class CommandHandler(BaseHandler[Update, CCT]): +class CommandHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle Telegram commands. Commands are Telegram messages that start with ``/``, optionally followed by an ``@`` and the @@ -118,7 +118,7 @@ async def callback(update: Update, context: CallbackContext) __slots__ = ("commands", "filters", "has_args") def __init__( - self, + self: "CommandHandler[CCT, RT]", command: SCT[str], callback: HandlerCallback[Update, CCT, RT], filters: Optional[filters_module.BaseFilter] = None, diff --git a/telegram/ext/_handlers/conversationhandler.py b/telegram/ext/_handlers/conversationhandler.py index f3fad9f8324..83fa373fc61 100644 --- a/telegram/ext/_handlers/conversationhandler.py +++ b/telegram/ext/_handlers/conversationhandler.py @@ -55,7 +55,7 @@ if TYPE_CHECKING: from telegram.ext import Application, Job, JobQueue -_CheckUpdateType = Tuple[object, ConversationKey, BaseHandler[Update, CCT], object] +_CheckUpdateType = Tuple[object, ConversationKey, BaseHandler[Update, CCT, object], object] _LOGGER = get_logger(__name__, class_name="ConversationHandler") @@ -119,7 +119,7 @@ def resolve(self) -> object: return res -class ConversationHandler(BaseHandler[Update, CCT]): +class ConversationHandler(BaseHandler[Update, CCT, object]): """ A handler to hold a conversation with a single or multiple users through Telegram updates by managing three collections of other handlers. @@ -296,10 +296,10 @@ class ConversationHandler(BaseHandler[Update, CCT]): # pylint: disable=super-init-not-called def __init__( - self, - entry_points: List[BaseHandler[Update, CCT]], - states: Dict[object, List[BaseHandler[Update, CCT]]], - fallbacks: List[BaseHandler[Update, CCT]], + self: "ConversationHandler[CCT]", + entry_points: List[BaseHandler[Update, CCT, object]], + states: Dict[object, List[BaseHandler[Update, CCT, object]]], + fallbacks: List[BaseHandler[Update, CCT, object]], allow_reentry: bool = False, per_chat: bool = True, per_user: bool = True, @@ -324,9 +324,9 @@ def __init__( # Store the actual setting in a protected variable instead self._block: DVType[bool] = block - self._entry_points: List[BaseHandler[Update, CCT]] = entry_points - self._states: Dict[object, List[BaseHandler[Update, CCT]]] = states - self._fallbacks: List[BaseHandler[Update, CCT]] = fallbacks + self._entry_points: List[BaseHandler[Update, CCT, object]] = entry_points + self._states: Dict[object, List[BaseHandler[Update, CCT, object]]] = states + self._fallbacks: List[BaseHandler[Update, CCT, object]] = fallbacks self._allow_reentry: bool = allow_reentry self._per_user: bool = per_user @@ -359,7 +359,7 @@ def __init__( stacklevel=2, ) - all_handlers: List[BaseHandler[Update, CCT]] = [] + all_handlers: List[BaseHandler[Update, CCT, object]] = [] all_handlers.extend(entry_points) all_handlers.extend(fallbacks) @@ -466,7 +466,7 @@ def __repr__(self) -> str: ) @property - def entry_points(self) -> List[BaseHandler[Update, CCT]]: + def entry_points(self) -> List[BaseHandler[Update, CCT, object]]: """List[:class:`telegram.ext.BaseHandler`]: A list of :obj:`BaseHandler` objects that can trigger the start of the conversation. """ @@ -479,7 +479,7 @@ def entry_points(self, _: object) -> NoReturn: ) @property - def states(self) -> Dict[object, List[BaseHandler[Update, CCT]]]: + def states(self) -> Dict[object, List[BaseHandler[Update, CCT, object]]]: """Dict[:obj:`object`, List[:class:`telegram.ext.BaseHandler`]]: A :obj:`dict` that defines the different states of conversation a user can be in and one or more associated :obj:`BaseHandler` objects that should be used in that state. @@ -491,7 +491,7 @@ def states(self, _: object) -> NoReturn: raise AttributeError("You can not assign a new value to states after initialization.") @property - def fallbacks(self) -> List[BaseHandler[Update, CCT]]: + def fallbacks(self) -> List[BaseHandler[Update, CCT, object]]: """List[:class:`telegram.ext.BaseHandler`]: A list of handlers that might be used if the user is in a conversation, but every handler for their current state returned :obj:`False` on :meth:`check_update`. diff --git a/telegram/ext/_handlers/inlinequeryhandler.py b/telegram/ext/_handlers/inlinequeryhandler.py index 21a5925cb1d..9d2cbb710b2 100644 --- a/telegram/ext/_handlers/inlinequeryhandler.py +++ b/telegram/ext/_handlers/inlinequeryhandler.py @@ -32,7 +32,7 @@ RT = TypeVar("RT") -class InlineQueryHandler(BaseHandler[Update, CCT]): +class InlineQueryHandler(BaseHandler[Update, CCT, RT]): """ BaseHandler class to handle Telegram updates that contain a :attr:`telegram.Update.inline_query`. @@ -87,7 +87,7 @@ async def callback(update: Update, context: CallbackContext) __slots__ = ("chat_types", "pattern") def __init__( - self, + self: "InlineQueryHandler[CCT, RT]", callback: HandlerCallback[Update, CCT, RT], pattern: Optional[Union[str, Pattern[str]]] = None, block: DVType[bool] = DEFAULT_TRUE, diff --git a/telegram/ext/_handlers/messagehandler.py b/telegram/ext/_handlers/messagehandler.py index 0cd42884ba6..43d8c8d8115 100644 --- a/telegram/ext/_handlers/messagehandler.py +++ b/telegram/ext/_handlers/messagehandler.py @@ -32,7 +32,7 @@ RT = TypeVar("RT") -class MessageHandler(BaseHandler[Update, CCT]): +class MessageHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle Telegram messages. They might contain text, media or status updates. @@ -75,7 +75,7 @@ async def callback(update: Update, context: CallbackContext) __slots__ = ("filters",) def __init__( - self, + self: "MessageHandler[CCT, RT]", filters: Optional[filters_module.BaseFilter], callback: HandlerCallback[Update, CCT, RT], block: DVType[bool] = DEFAULT_TRUE, diff --git a/telegram/ext/_handlers/messagereactionhandler.py b/telegram/ext/_handlers/messagereactionhandler.py index 4b52a17a8ab..34a5292be96 100644 --- a/telegram/ext/_handlers/messagereactionhandler.py +++ b/telegram/ext/_handlers/messagereactionhandler.py @@ -28,7 +28,7 @@ from telegram.ext._utils.types import CCT, HandlerCallback -class MessageReactionHandler(BaseHandler[Update, CCT]): +class MessageReactionHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle Telegram updates that contain a message reaction. Note: @@ -110,7 +110,7 @@ async def callback(update: Update, context: CallbackContext) and :attr:`telegram.Update.message_reaction_count`.""" def __init__( - self, + self: "MessageReactionHandler[CCT, RT]", callback: HandlerCallback[Update, CCT, RT], chat_id: Optional[SCT[int]] = None, chat_username: Optional[SCT[str]] = None, diff --git a/telegram/ext/_handlers/pollanswerhandler.py b/telegram/ext/_handlers/pollanswerhandler.py index 91e8df82b70..5e0bb34dbea 100644 --- a/telegram/ext/_handlers/pollanswerhandler.py +++ b/telegram/ext/_handlers/pollanswerhandler.py @@ -21,10 +21,10 @@ from telegram import Update from telegram.ext._handlers.basehandler import BaseHandler -from telegram.ext._utils.types import CCT +from telegram.ext._utils.types import CCT, RT -class PollAnswerHandler(BaseHandler[Update, CCT]): +class PollAnswerHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle Telegram updates that contain a :attr:`poll answer `. diff --git a/telegram/ext/_handlers/pollhandler.py b/telegram/ext/_handlers/pollhandler.py index fb183c53805..6bb41c3237e 100644 --- a/telegram/ext/_handlers/pollhandler.py +++ b/telegram/ext/_handlers/pollhandler.py @@ -21,10 +21,10 @@ from telegram import Update from telegram.ext._handlers.basehandler import BaseHandler -from telegram.ext._utils.types import CCT +from telegram.ext._utils.types import CCT, RT -class PollHandler(BaseHandler[Update, CCT]): +class PollHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle Telegram updates that contain a :attr:`poll `. diff --git a/telegram/ext/_handlers/precheckoutqueryhandler.py b/telegram/ext/_handlers/precheckoutqueryhandler.py index 3c6f3f186df..de035364ec9 100644 --- a/telegram/ext/_handlers/precheckoutqueryhandler.py +++ b/telegram/ext/_handlers/precheckoutqueryhandler.py @@ -31,7 +31,7 @@ RT = TypeVar("RT") -class PreCheckoutQueryHandler(BaseHandler[Update, CCT]): +class PreCheckoutQueryHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle Telegram :attr:`telegram.Update.pre_checkout_query`. Warning: @@ -73,7 +73,7 @@ async def callback(update: Update, context: CallbackContext) __slots__ = ("pattern",) def __init__( - self, + self: "PreCheckoutQueryHandler[CCT, RT]", callback: HandlerCallback[Update, CCT, RT], block: DVType[bool] = DEFAULT_TRUE, pattern: Optional[Union[str, Pattern[str]]] = None, diff --git a/telegram/ext/_handlers/prefixhandler.py b/telegram/ext/_handlers/prefixhandler.py index 3b10a0a1caf..bda265e1056 100644 --- a/telegram/ext/_handlers/prefixhandler.py +++ b/telegram/ext/_handlers/prefixhandler.py @@ -33,7 +33,7 @@ RT = TypeVar("RT") -class PrefixHandler(BaseHandler[Update, CCT]): +class PrefixHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle custom prefix commands. This is an intermediate handler between :class:`MessageHandler` and :class:`CommandHandler`. @@ -123,7 +123,7 @@ async def callback(update: Update, context: CallbackContext) __slots__ = ("commands", "filters") def __init__( - self, + self: "PrefixHandler[CCT, RT]", prefix: SCT[str], command: SCT[str], callback: HandlerCallback[Update, CCT, RT], diff --git a/telegram/ext/_handlers/shippingqueryhandler.py b/telegram/ext/_handlers/shippingqueryhandler.py index f0234c2a962..c186513093a 100644 --- a/telegram/ext/_handlers/shippingqueryhandler.py +++ b/telegram/ext/_handlers/shippingqueryhandler.py @@ -21,10 +21,10 @@ from telegram import Update from telegram.ext._handlers.basehandler import BaseHandler -from telegram.ext._utils.types import CCT +from telegram.ext._utils.types import CCT, RT -class ShippingQueryHandler(BaseHandler[Update, CCT]): +class ShippingQueryHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle Telegram :attr:`telegram.Update.shipping_query`. Warning: diff --git a/telegram/ext/_handlers/stringcommandhandler.py b/telegram/ext/_handlers/stringcommandhandler.py index d5c29bf6639..ff655d9d6c4 100644 --- a/telegram/ext/_handlers/stringcommandhandler.py +++ b/telegram/ext/_handlers/stringcommandhandler.py @@ -29,7 +29,7 @@ from telegram.ext import Application -class StringCommandHandler(BaseHandler[str, CCT]): +class StringCommandHandler(BaseHandler[str, CCT, RT]): """Handler class to handle string commands. Commands are string updates that start with ``/``. The handler will add a :obj:`list` to the :class:`CallbackContext` named :attr:`CallbackContext.args`. It will contain a list of strings, @@ -71,7 +71,7 @@ async def callback(update: str, context: CallbackContext) __slots__ = ("command",) def __init__( - self, + self: "StringCommandHandler[CCT, RT]", command: str, callback: HandlerCallback[str, CCT, RT], block: DVType[bool] = DEFAULT_TRUE, diff --git a/telegram/ext/_handlers/stringregexhandler.py b/telegram/ext/_handlers/stringregexhandler.py index c2e22c10655..bd97469495a 100644 --- a/telegram/ext/_handlers/stringregexhandler.py +++ b/telegram/ext/_handlers/stringregexhandler.py @@ -32,7 +32,7 @@ RT = TypeVar("RT") -class StringRegexHandler(BaseHandler[str, CCT]): +class StringRegexHandler(BaseHandler[str, CCT, RT]): """Handler class to handle string updates based on a regex which checks the update content. Read the documentation of the :mod:`re` module for more information. The :func:`re.match` @@ -74,7 +74,7 @@ async def callback(update: str, context: CallbackContext) __slots__ = ("pattern",) def __init__( - self, + self: "StringRegexHandler[CCT, RT]", pattern: Union[str, Pattern[str]], callback: HandlerCallback[str, CCT, RT], block: DVType[bool] = DEFAULT_TRUE, diff --git a/telegram/ext/_handlers/typehandler.py b/telegram/ext/_handlers/typehandler.py index 151a7fbf137..48a4530bcfa 100644 --- a/telegram/ext/_handlers/typehandler.py +++ b/telegram/ext/_handlers/typehandler.py @@ -29,7 +29,7 @@ UT = TypeVar("UT") -class TypeHandler(BaseHandler[UT, CCT]): +class TypeHandler(BaseHandler[UT, CCT, RT]): """Handler class to handle updates of custom types. Warning: @@ -70,7 +70,7 @@ async def callback(update: object, context: CallbackContext) __slots__ = ("strict", "type") def __init__( - self, + self: "TypeHandler[UT, CCT, RT]", type: Type[UT], # pylint: disable=redefined-builtin callback: HandlerCallback[UT, CCT, RT], strict: bool = False, From 51f2fd26179e2e16208f3e690026c758ee1afec2 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Sun, 8 Sep 2024 21:10:06 +0200 Subject: [PATCH 02/16] Fix mypy for example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit the proper solution would be to make CH accept Sequences (covariant), but I ain't touching CH for this … --- examples/nestedconversationbot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/nestedconversationbot.py b/examples/nestedconversationbot.py index 918f0becf24..fdc49de2b7f 100644 --- a/examples/nestedconversationbot.py +++ b/examples/nestedconversationbot.py @@ -371,8 +371,8 @@ def main() -> None: entry_points=[CommandHandler("start", start)], states={ SHOWING: [CallbackQueryHandler(start, pattern="^" + str(END) + "$")], - SELECTING_ACTION: selection_handlers, - SELECTING_LEVEL: selection_handlers, + SELECTING_ACTION: selection_handlers, # type: ignore[dict-item] + SELECTING_LEVEL: selection_handlers, # type: ignore[dict-item] DESCRIBING_SELF: [description_conv], STOPPING: [CommandHandler("start", start)], }, From 7500927e318b04bdf57769a2fe12825a252debf8 Mon Sep 17 00:00:00 2001 From: Bibo-Joshi <22366557+Bibo-Joshi@users.noreply.github.com> Date: Sun, 8 Sep 2024 22:30:45 +0200 Subject: [PATCH 03/16] Update telegram/ext/_callbackcontext.py Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com> --- telegram/ext/_callbackcontext.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram/ext/_callbackcontext.py b/telegram/ext/_callbackcontext.py index b976c9c0bba..dfd2c3cc8d7 100644 --- a/telegram/ext/_callbackcontext.py +++ b/telegram/ext/_callbackcontext.py @@ -50,7 +50,7 @@ "/wiki/Storing-bot%2C-user-and-chat-related-data" ) -# something like poor mans "tying.Self" for py<3.11 +# something like poor mans "typing.Self" for py<3.11 ST = TypeVar("ST", bound="CallbackContext[Any, Any, Any, Any]") From 2d4f439ed25d88cc8c5d0430b53361c9f6ffd77e Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Mon, 9 Sep 2024 22:22:33 +0200 Subject: [PATCH 04/16] Add monthly run + demo --- .../workflows/type_completeness_monthly.yml | 27 +++++++++++++++++++ telegram/_bot.py | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/type_completeness_monthly.yml diff --git a/.github/workflows/type_completeness_monthly.yml b/.github/workflows/type_completeness_monthly.yml new file mode 100644 index 00000000000..312390b3d43 --- /dev/null +++ b/.github/workflows/type_completeness_monthly.yml @@ -0,0 +1,27 @@ +name: Check Type Completeness +on: + schedule: + # Run first friday of the month at 03:17 - odd time to spread load on GitHub Actions + - cron: '17 3 1-7 * 5' + pull_request: + # for demo only + branches: + - master + +jobs: + test-type-completeness: + name: test-type-completeness + runs-on: ubuntu-latest + steps: + - uses: Bibo-Joshi/pyright-type-completeness@1.0.0 + with: + package-name: telegram + python-version: 3.12 + pyright-version: ~=1.1.367 + - name: Check Output + # Fail the workflow if the "base-completeness-score" is less than 1 + run: | + if [ $(jq '.base-completeness-score' /tmp/pyright-type-completeness-output.json) -lt 1 ]; then + echo "Type completeness score is less than 1" + exit 1 + fi diff --git a/telegram/_bot.py b/telegram/_bot.py index b79df08ff17..f7fdcefb041 100644 --- a/telegram/_bot.py +++ b/telegram/_bot.py @@ -393,7 +393,7 @@ def __repr__(self) -> str: return build_repr_with_selected_attrs(self, token=self.token) @property - def token(self) -> str: + def token(self): """:obj:`str`: Bot's unique authentication token. .. versionadded:: 20.0 From db59e9f8dc2387f225288e3449ea6fda672541b5 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Mon, 9 Sep 2024 22:27:13 +0200 Subject: [PATCH 05/16] Try fixing workflow --- .github/workflows/type_completeness_monthly.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/type_completeness_monthly.yml b/.github/workflows/type_completeness_monthly.yml index 312390b3d43..e619f5fc19b 100644 --- a/.github/workflows/type_completeness_monthly.yml +++ b/.github/workflows/type_completeness_monthly.yml @@ -1,4 +1,4 @@ -name: Check Type Completeness +name: Check Type Completeness Monthly Run on: schedule: # Run first friday of the month at 03:17 - odd time to spread load on GitHub Actions @@ -19,9 +19,9 @@ jobs: python-version: 3.12 pyright-version: ~=1.1.367 - name: Check Output - # Fail the workflow if the "base-completeness-score" is less than 1 + # Fail the workflow if the "base-completeness-score" of the previous step is less than 1 run: | - if [ $(jq '.base-completeness-score' /tmp/pyright-type-completeness-output.json) -lt 1 ]; then - echo "Type completeness score is less than 1" - exit 1 + if [ $(echo "${{ steps.pyright-type-completeness.outputs.base-completeness-score }}" | bc) -lt 1 ]; then + echo "Type completeness score is less than 1" + exit 1 fi From bfb9287475b3fabaf7717c7aba16ca82a21be188 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Mon, 9 Sep 2024 22:27:52 +0200 Subject: [PATCH 06/16] Rework non-working demo --- telegram/_bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram/_bot.py b/telegram/_bot.py index f7fdcefb041..b79df08ff17 100644 --- a/telegram/_bot.py +++ b/telegram/_bot.py @@ -393,7 +393,7 @@ def __repr__(self) -> str: return build_repr_with_selected_attrs(self, token=self.token) @property - def token(self): + def token(self) -> str: """:obj:`str`: Bot's unique authentication token. .. versionadded:: 20.0 From 820a3ed823c6348b4b6663b0e767fd34a2d60e6f Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Mon, 9 Sep 2024 22:28:01 +0200 Subject: [PATCH 07/16] Revert previous commits for demo --- examples/nestedconversationbot.py | 4 +-- telegram/ext/_application.py | 12 ++++----- telegram/ext/_callbackcontext.py | 16 +++++------- telegram/ext/_handlers/basehandler.py | 7 +++-- .../_handlers/businessconnectionhandler.py | 4 +-- .../businessmessagesdeletedhandler.py | 4 +-- .../ext/_handlers/callbackqueryhandler.py | 4 +-- telegram/ext/_handlers/chatboosthandler.py | 8 +++--- .../ext/_handlers/chatjoinrequesthandler.py | 4 +-- telegram/ext/_handlers/chatmemberhandler.py | 4 +-- .../_handlers/choseninlineresulthandler.py | 4 +-- telegram/ext/_handlers/commandhandler.py | 4 +-- telegram/ext/_handlers/conversationhandler.py | 26 +++++++++---------- telegram/ext/_handlers/inlinequeryhandler.py | 4 +-- telegram/ext/_handlers/messagehandler.py | 4 +-- .../ext/_handlers/messagereactionhandler.py | 4 +-- telegram/ext/_handlers/pollanswerhandler.py | 4 +-- telegram/ext/_handlers/pollhandler.py | 4 +-- .../ext/_handlers/precheckoutqueryhandler.py | 4 +-- telegram/ext/_handlers/prefixhandler.py | 4 +-- .../ext/_handlers/shippingqueryhandler.py | 4 +-- .../ext/_handlers/stringcommandhandler.py | 4 +-- telegram/ext/_handlers/stringregexhandler.py | 4 +-- telegram/ext/_handlers/typehandler.py | 4 +-- 24 files changed, 69 insertions(+), 76 deletions(-) diff --git a/examples/nestedconversationbot.py b/examples/nestedconversationbot.py index fdc49de2b7f..918f0becf24 100644 --- a/examples/nestedconversationbot.py +++ b/examples/nestedconversationbot.py @@ -371,8 +371,8 @@ def main() -> None: entry_points=[CommandHandler("start", start)], states={ SHOWING: [CallbackQueryHandler(start, pattern="^" + str(END) + "$")], - SELECTING_ACTION: selection_handlers, # type: ignore[dict-item] - SELECTING_LEVEL: selection_handlers, # type: ignore[dict-item] + SELECTING_ACTION: selection_handlers, + SELECTING_LEVEL: selection_handlers, DESCRIBING_SELF: [description_conv], STOPPING: [CommandHandler("start", start)], }, diff --git a/telegram/ext/_application.py b/telegram/ext/_application.py index 86e38b1de9f..a1013ce570a 100644 --- a/telegram/ext/_application.py +++ b/telegram/ext/_application.py @@ -323,7 +323,7 @@ def __init__( self.update_queue: asyncio.Queue[object] = update_queue self.context_types: ContextTypes[CCT, UD, CD, BD] = context_types self.updater: Optional[Updater] = updater - self.handlers: Dict[int, List[BaseHandler[Any, CCT, Any]]] = {} + self.handlers: Dict[int, List[BaseHandler[Any, CCT]]] = {} self.error_handlers: Dict[ HandlerCallback[object, CCT, None], Union[bool, DefaultValue[bool]] ] = {} @@ -1352,7 +1352,7 @@ async def process_update(self, update: object) -> None: # (in __create_task_callback) self._mark_for_persistence_update(update=update) - def add_handler(self, handler: BaseHandler[Any, CCT, Any], group: int = DEFAULT_GROUP) -> None: + def add_handler(self, handler: BaseHandler[Any, CCT], group: int = DEFAULT_GROUP) -> None: """Register a handler. TL;DR: Order and priority counts. 0 or 1 handlers per group will be used. End handling of @@ -1420,8 +1420,8 @@ def add_handler(self, handler: BaseHandler[Any, CCT, Any], group: int = DEFAULT_ def add_handlers( self, handlers: Union[ - Union[List[BaseHandler[Any, CCT, Any]], Tuple[BaseHandler[Any, CCT, Any]]], - Dict[int, Union[List[BaseHandler[Any, CCT, Any]], Tuple[BaseHandler[Any, CCT, Any]]]], + Union[List[BaseHandler[Any, CCT]], Tuple[BaseHandler[Any, CCT]]], + Dict[int, Union[List[BaseHandler[Any, CCT]], Tuple[BaseHandler[Any, CCT]]]], ], group: Union[int, DefaultValue[int]] = _DEFAULT_0, ) -> None: @@ -1469,9 +1469,7 @@ def add_handlers( "dictionary where the keys are groups and values are sequences of handlers." ) - def remove_handler( - self, handler: BaseHandler[Any, CCT, Any], group: int = DEFAULT_GROUP - ) -> None: + def remove_handler(self, handler: BaseHandler[Any, CCT], group: int = DEFAULT_GROUP) -> None: """Remove a handler from the specified group. Args: diff --git a/telegram/ext/_callbackcontext.py b/telegram/ext/_callbackcontext.py index dfd2c3cc8d7..ea708e0e6b8 100644 --- a/telegram/ext/_callbackcontext.py +++ b/telegram/ext/_callbackcontext.py @@ -29,7 +29,6 @@ NoReturn, Optional, Type, - TypeVar, Union, ) @@ -50,9 +49,6 @@ "/wiki/Storing-bot%2C-user-and-chat-related-data" ) -# something like poor mans "typing.Self" for py<3.11 -ST = TypeVar("ST", bound="CallbackContext[Any, Any, Any, Any]") - class CallbackContext(Generic[BT, UD, CD, BD]): """ @@ -137,24 +133,24 @@ class CallbackContext(Generic[BT, UD, CD, BD]): ) def __init__( - self: ST, - application: "Application[BT, ST, UD, CD, BD, Any]", + self: "CCT", + application: "Application[BT, CCT, UD, CD, BD, Any]", chat_id: Optional[int] = None, user_id: Optional[int] = None, ): - self._application: Application[BT, ST, UD, CD, BD, Any] = application + self._application: Application[BT, CCT, UD, CD, BD, Any] = application self._chat_id: Optional[int] = chat_id self._user_id: Optional[int] = user_id self.args: Optional[List[str]] = None self.matches: Optional[List[Match[str]]] = None self.error: Optional[Exception] = None - self.job: Optional[Job[Any]] = None + self.job: Optional[Job[CCT]] = None self.coroutine: Optional[ Union[Generator[Optional[Future[object]], None, Any], Awaitable[Any]] ] = None @property - def application(self) -> "Application[BT, ST, UD, CD, BD, Any]": + def application(self) -> "Application[BT, CCT, UD, CD, BD, Any]": """:class:`telegram.ext.Application`: The application associated with this context.""" return self._application @@ -402,7 +398,7 @@ def bot(self) -> BT: return self._application.bot @property - def job_queue(self) -> Optional["JobQueue[ST]"]: + def job_queue(self) -> Optional["JobQueue[CCT]"]: """ :class:`telegram.ext.JobQueue`: The :class:`JobQueue` used by the :class:`telegram.ext.Application`. diff --git a/telegram/ext/_handlers/basehandler.py b/telegram/ext/_handlers/basehandler.py index bf6f5f155df..ea0d9b1640f 100644 --- a/telegram/ext/_handlers/basehandler.py +++ b/telegram/ext/_handlers/basehandler.py @@ -32,14 +32,14 @@ UT = TypeVar("UT") -class BaseHandler(Generic[UT, CCT, RT], ABC): +class BaseHandler(Generic[UT, CCT], ABC): """The base class for all update handlers. Create custom handlers by inheriting from it. Warning: When setting :paramref:`block` to :obj:`False`, you cannot rely on adding custom attributes to :class:`telegram.ext.CallbackContext`. See its docs for more info. - This class is a :class:`~typing.Generic` class and accepts three type variables: + This class is a :class:`~typing.Generic` class and accepts two type variables: 1. The type of the updates that this handler will handle. Must coincide with the type of the first argument of :paramref:`callback`. :meth:`check_update` must only accept @@ -54,7 +54,6 @@ class BaseHandler(Generic[UT, CCT, RT], ABC): For this type variable, one should usually provide a :class:`~typing.TypeVar` that is also used for the mentioned method arguments. That way, a type checker can check whether this handler fits the definition of the :class:`~Application`. - 3. The return type of the :paramref:`callback` function accepted by this handler. .. seealso:: :wiki:`Types of Handlers ` @@ -90,7 +89,7 @@ async def callback(update: Update, context: CallbackContext) ) def __init__( - self: "BaseHandler[UT, CCT, RT]", + self, callback: HandlerCallback[UT, CCT, RT], block: DVType[bool] = DEFAULT_TRUE, ): diff --git a/telegram/ext/_handlers/businessconnectionhandler.py b/telegram/ext/_handlers/businessconnectionhandler.py index e5d09c77cf1..c8cb3e843d0 100644 --- a/telegram/ext/_handlers/businessconnectionhandler.py +++ b/telegram/ext/_handlers/businessconnectionhandler.py @@ -29,7 +29,7 @@ RT = TypeVar("RT") -class BusinessConnectionHandler(BaseHandler[Update, CCT, RT]): +class BusinessConnectionHandler(BaseHandler[Update, CCT]): """Handler class to handle Telegram :attr:`Business Connections `. @@ -65,7 +65,7 @@ async def callback(update: Update, context: CallbackContext) ) def __init__( - self: "BusinessConnectionHandler[CCT, RT]", + self, callback: HandlerCallback[Update, CCT, RT], user_id: Optional[SCT[int]] = None, username: Optional[SCT[str]] = None, diff --git a/telegram/ext/_handlers/businessmessagesdeletedhandler.py b/telegram/ext/_handlers/businessmessagesdeletedhandler.py index c041bb0adc0..0ceb58fe05c 100644 --- a/telegram/ext/_handlers/businessmessagesdeletedhandler.py +++ b/telegram/ext/_handlers/businessmessagesdeletedhandler.py @@ -29,7 +29,7 @@ RT = TypeVar("RT") -class BusinessMessagesDeletedHandler(BaseHandler[Update, CCT, RT]): +class BusinessMessagesDeletedHandler(BaseHandler[Update, CCT]): """Handler class to handle :attr:`deleted Telegram Business messages `. @@ -65,7 +65,7 @@ async def callback(update: Update, context: CallbackContext) ) def __init__( - self: "BusinessMessagesDeletedHandler[CCT, RT]", + self, callback: HandlerCallback[Update, CCT, RT], chat_id: Optional[SCT[int]] = None, username: Optional[SCT[str]] = None, diff --git a/telegram/ext/_handlers/callbackqueryhandler.py b/telegram/ext/_handlers/callbackqueryhandler.py index 5e0a0a12aa0..a149739a2fb 100644 --- a/telegram/ext/_handlers/callbackqueryhandler.py +++ b/telegram/ext/_handlers/callbackqueryhandler.py @@ -33,7 +33,7 @@ RT = TypeVar("RT") -class CallbackQueryHandler(BaseHandler[Update, CCT, RT]): +class CallbackQueryHandler(BaseHandler[Update, CCT]): """Handler class to handle Telegram :attr:`callback queries `. Optionally based on a regex. @@ -125,7 +125,7 @@ async def callback(update: Update, context: CallbackContext) __slots__ = ("game_pattern", "pattern") def __init__( - self: "CallbackQueryHandler[CCT, RT]", + self, callback: HandlerCallback[Update, CCT, RT], pattern: Optional[ Union[str, Pattern[str], type, Callable[[object], Optional[bool]]] diff --git a/telegram/ext/_handlers/chatboosthandler.py b/telegram/ext/_handlers/chatboosthandler.py index 7a2dd1be6a6..9d7219632a1 100644 --- a/telegram/ext/_handlers/chatboosthandler.py +++ b/telegram/ext/_handlers/chatboosthandler.py @@ -23,10 +23,10 @@ from telegram import Update from telegram.ext._handlers.basehandler import BaseHandler from telegram.ext._utils._update_parsing import parse_chat_id, parse_username -from telegram.ext._utils.types import CCT, RT, HandlerCallback +from telegram.ext._utils.types import CCT, HandlerCallback -class ChatBoostHandler(BaseHandler[Update, CCT, RT]): +class ChatBoostHandler(BaseHandler[Update, CCT]): """ Handler class to handle Telegram updates that contain a chat boost. @@ -84,8 +84,8 @@ async def callback(update: Update, context: CallbackContext) and :attr:`telegram.Update.removed_chat_boost`.""" def __init__( - self: "ChatBoostHandler[CCT, RT]", - callback: HandlerCallback[Update, CCT, RT], + self, + callback: HandlerCallback[Update, CCT, None], chat_boost_types: int = CHAT_BOOST, chat_id: Optional[int] = None, chat_username: Optional[str] = None, diff --git a/telegram/ext/_handlers/chatjoinrequesthandler.py b/telegram/ext/_handlers/chatjoinrequesthandler.py index 04576e63c32..7007f61ab03 100644 --- a/telegram/ext/_handlers/chatjoinrequesthandler.py +++ b/telegram/ext/_handlers/chatjoinrequesthandler.py @@ -28,7 +28,7 @@ from telegram.ext._utils.types import CCT, HandlerCallback -class ChatJoinRequestHandler(BaseHandler[Update, CCT, RT]): +class ChatJoinRequestHandler(BaseHandler[Update, CCT]): """Handler class to handle Telegram updates that contain :attr:`telegram.Update.chat_join_request`. @@ -81,7 +81,7 @@ async def callback(update: Update, context: CallbackContext) ) def __init__( - self: "ChatJoinRequestHandler[CCT, RT]", + self, callback: HandlerCallback[Update, CCT, RT], chat_id: Optional[SCT[int]] = None, username: Optional[SCT[str]] = None, diff --git a/telegram/ext/_handlers/chatmemberhandler.py b/telegram/ext/_handlers/chatmemberhandler.py index cf8bb28db10..592361b64c5 100644 --- a/telegram/ext/_handlers/chatmemberhandler.py +++ b/telegram/ext/_handlers/chatmemberhandler.py @@ -29,7 +29,7 @@ RT = TypeVar("RT") -class ChatMemberHandler(BaseHandler[Update, CCT, RT]): +class ChatMemberHandler(BaseHandler[Update, CCT]): """Handler class to handle Telegram updates that contain a chat member update. Warning: @@ -87,7 +87,7 @@ async def callback(update: Update, context: CallbackContext) and :attr:`telegram.Update.chat_member`.""" def __init__( - self: "ChatMemberHandler[CCT, RT]", + self, callback: HandlerCallback[Update, CCT, RT], chat_member_types: int = MY_CHAT_MEMBER, block: DVType[bool] = DEFAULT_TRUE, diff --git a/telegram/ext/_handlers/choseninlineresulthandler.py b/telegram/ext/_handlers/choseninlineresulthandler.py index feac28ba658..db7a8721448 100644 --- a/telegram/ext/_handlers/choseninlineresulthandler.py +++ b/telegram/ext/_handlers/choseninlineresulthandler.py @@ -32,7 +32,7 @@ from telegram.ext import Application -class ChosenInlineResultHandler(BaseHandler[Update, CCT, RT]): +class ChosenInlineResultHandler(BaseHandler[Update, CCT]): """Handler class to handle Telegram updates that contain :attr:`telegram.Update.chosen_inline_result`. @@ -76,7 +76,7 @@ async def callback(update: Update, context: CallbackContext) __slots__ = ("pattern",) def __init__( - self: "ChosenInlineResultHandler[CCT, RT]", + self, callback: HandlerCallback[Update, CCT, RT], block: DVType[bool] = DEFAULT_TRUE, pattern: Optional[Union[str, Pattern[str]]] = None, diff --git a/telegram/ext/_handlers/commandhandler.py b/telegram/ext/_handlers/commandhandler.py index 54d01acd6d0..edad3963aad 100644 --- a/telegram/ext/_handlers/commandhandler.py +++ b/telegram/ext/_handlers/commandhandler.py @@ -33,7 +33,7 @@ RT = TypeVar("RT") -class CommandHandler(BaseHandler[Update, CCT, RT]): +class CommandHandler(BaseHandler[Update, CCT]): """Handler class to handle Telegram commands. Commands are Telegram messages that start with ``/``, optionally followed by an ``@`` and the @@ -118,7 +118,7 @@ async def callback(update: Update, context: CallbackContext) __slots__ = ("commands", "filters", "has_args") def __init__( - self: "CommandHandler[CCT, RT]", + self, command: SCT[str], callback: HandlerCallback[Update, CCT, RT], filters: Optional[filters_module.BaseFilter] = None, diff --git a/telegram/ext/_handlers/conversationhandler.py b/telegram/ext/_handlers/conversationhandler.py index 83fa373fc61..f3fad9f8324 100644 --- a/telegram/ext/_handlers/conversationhandler.py +++ b/telegram/ext/_handlers/conversationhandler.py @@ -55,7 +55,7 @@ if TYPE_CHECKING: from telegram.ext import Application, Job, JobQueue -_CheckUpdateType = Tuple[object, ConversationKey, BaseHandler[Update, CCT, object], object] +_CheckUpdateType = Tuple[object, ConversationKey, BaseHandler[Update, CCT], object] _LOGGER = get_logger(__name__, class_name="ConversationHandler") @@ -119,7 +119,7 @@ def resolve(self) -> object: return res -class ConversationHandler(BaseHandler[Update, CCT, object]): +class ConversationHandler(BaseHandler[Update, CCT]): """ A handler to hold a conversation with a single or multiple users through Telegram updates by managing three collections of other handlers. @@ -296,10 +296,10 @@ class ConversationHandler(BaseHandler[Update, CCT, object]): # pylint: disable=super-init-not-called def __init__( - self: "ConversationHandler[CCT]", - entry_points: List[BaseHandler[Update, CCT, object]], - states: Dict[object, List[BaseHandler[Update, CCT, object]]], - fallbacks: List[BaseHandler[Update, CCT, object]], + self, + entry_points: List[BaseHandler[Update, CCT]], + states: Dict[object, List[BaseHandler[Update, CCT]]], + fallbacks: List[BaseHandler[Update, CCT]], allow_reentry: bool = False, per_chat: bool = True, per_user: bool = True, @@ -324,9 +324,9 @@ def __init__( # Store the actual setting in a protected variable instead self._block: DVType[bool] = block - self._entry_points: List[BaseHandler[Update, CCT, object]] = entry_points - self._states: Dict[object, List[BaseHandler[Update, CCT, object]]] = states - self._fallbacks: List[BaseHandler[Update, CCT, object]] = fallbacks + self._entry_points: List[BaseHandler[Update, CCT]] = entry_points + self._states: Dict[object, List[BaseHandler[Update, CCT]]] = states + self._fallbacks: List[BaseHandler[Update, CCT]] = fallbacks self._allow_reentry: bool = allow_reentry self._per_user: bool = per_user @@ -359,7 +359,7 @@ def __init__( stacklevel=2, ) - all_handlers: List[BaseHandler[Update, CCT, object]] = [] + all_handlers: List[BaseHandler[Update, CCT]] = [] all_handlers.extend(entry_points) all_handlers.extend(fallbacks) @@ -466,7 +466,7 @@ def __repr__(self) -> str: ) @property - def entry_points(self) -> List[BaseHandler[Update, CCT, object]]: + def entry_points(self) -> List[BaseHandler[Update, CCT]]: """List[:class:`telegram.ext.BaseHandler`]: A list of :obj:`BaseHandler` objects that can trigger the start of the conversation. """ @@ -479,7 +479,7 @@ def entry_points(self, _: object) -> NoReturn: ) @property - def states(self) -> Dict[object, List[BaseHandler[Update, CCT, object]]]: + def states(self) -> Dict[object, List[BaseHandler[Update, CCT]]]: """Dict[:obj:`object`, List[:class:`telegram.ext.BaseHandler`]]: A :obj:`dict` that defines the different states of conversation a user can be in and one or more associated :obj:`BaseHandler` objects that should be used in that state. @@ -491,7 +491,7 @@ def states(self, _: object) -> NoReturn: raise AttributeError("You can not assign a new value to states after initialization.") @property - def fallbacks(self) -> List[BaseHandler[Update, CCT, object]]: + def fallbacks(self) -> List[BaseHandler[Update, CCT]]: """List[:class:`telegram.ext.BaseHandler`]: A list of handlers that might be used if the user is in a conversation, but every handler for their current state returned :obj:`False` on :meth:`check_update`. diff --git a/telegram/ext/_handlers/inlinequeryhandler.py b/telegram/ext/_handlers/inlinequeryhandler.py index 9d2cbb710b2..21a5925cb1d 100644 --- a/telegram/ext/_handlers/inlinequeryhandler.py +++ b/telegram/ext/_handlers/inlinequeryhandler.py @@ -32,7 +32,7 @@ RT = TypeVar("RT") -class InlineQueryHandler(BaseHandler[Update, CCT, RT]): +class InlineQueryHandler(BaseHandler[Update, CCT]): """ BaseHandler class to handle Telegram updates that contain a :attr:`telegram.Update.inline_query`. @@ -87,7 +87,7 @@ async def callback(update: Update, context: CallbackContext) __slots__ = ("chat_types", "pattern") def __init__( - self: "InlineQueryHandler[CCT, RT]", + self, callback: HandlerCallback[Update, CCT, RT], pattern: Optional[Union[str, Pattern[str]]] = None, block: DVType[bool] = DEFAULT_TRUE, diff --git a/telegram/ext/_handlers/messagehandler.py b/telegram/ext/_handlers/messagehandler.py index 43d8c8d8115..0cd42884ba6 100644 --- a/telegram/ext/_handlers/messagehandler.py +++ b/telegram/ext/_handlers/messagehandler.py @@ -32,7 +32,7 @@ RT = TypeVar("RT") -class MessageHandler(BaseHandler[Update, CCT, RT]): +class MessageHandler(BaseHandler[Update, CCT]): """Handler class to handle Telegram messages. They might contain text, media or status updates. @@ -75,7 +75,7 @@ async def callback(update: Update, context: CallbackContext) __slots__ = ("filters",) def __init__( - self: "MessageHandler[CCT, RT]", + self, filters: Optional[filters_module.BaseFilter], callback: HandlerCallback[Update, CCT, RT], block: DVType[bool] = DEFAULT_TRUE, diff --git a/telegram/ext/_handlers/messagereactionhandler.py b/telegram/ext/_handlers/messagereactionhandler.py index 34a5292be96..4b52a17a8ab 100644 --- a/telegram/ext/_handlers/messagereactionhandler.py +++ b/telegram/ext/_handlers/messagereactionhandler.py @@ -28,7 +28,7 @@ from telegram.ext._utils.types import CCT, HandlerCallback -class MessageReactionHandler(BaseHandler[Update, CCT, RT]): +class MessageReactionHandler(BaseHandler[Update, CCT]): """Handler class to handle Telegram updates that contain a message reaction. Note: @@ -110,7 +110,7 @@ async def callback(update: Update, context: CallbackContext) and :attr:`telegram.Update.message_reaction_count`.""" def __init__( - self: "MessageReactionHandler[CCT, RT]", + self, callback: HandlerCallback[Update, CCT, RT], chat_id: Optional[SCT[int]] = None, chat_username: Optional[SCT[str]] = None, diff --git a/telegram/ext/_handlers/pollanswerhandler.py b/telegram/ext/_handlers/pollanswerhandler.py index 5e0bb34dbea..91e8df82b70 100644 --- a/telegram/ext/_handlers/pollanswerhandler.py +++ b/telegram/ext/_handlers/pollanswerhandler.py @@ -21,10 +21,10 @@ from telegram import Update from telegram.ext._handlers.basehandler import BaseHandler -from telegram.ext._utils.types import CCT, RT +from telegram.ext._utils.types import CCT -class PollAnswerHandler(BaseHandler[Update, CCT, RT]): +class PollAnswerHandler(BaseHandler[Update, CCT]): """Handler class to handle Telegram updates that contain a :attr:`poll answer `. diff --git a/telegram/ext/_handlers/pollhandler.py b/telegram/ext/_handlers/pollhandler.py index 6bb41c3237e..fb183c53805 100644 --- a/telegram/ext/_handlers/pollhandler.py +++ b/telegram/ext/_handlers/pollhandler.py @@ -21,10 +21,10 @@ from telegram import Update from telegram.ext._handlers.basehandler import BaseHandler -from telegram.ext._utils.types import CCT, RT +from telegram.ext._utils.types import CCT -class PollHandler(BaseHandler[Update, CCT, RT]): +class PollHandler(BaseHandler[Update, CCT]): """Handler class to handle Telegram updates that contain a :attr:`poll `. diff --git a/telegram/ext/_handlers/precheckoutqueryhandler.py b/telegram/ext/_handlers/precheckoutqueryhandler.py index de035364ec9..3c6f3f186df 100644 --- a/telegram/ext/_handlers/precheckoutqueryhandler.py +++ b/telegram/ext/_handlers/precheckoutqueryhandler.py @@ -31,7 +31,7 @@ RT = TypeVar("RT") -class PreCheckoutQueryHandler(BaseHandler[Update, CCT, RT]): +class PreCheckoutQueryHandler(BaseHandler[Update, CCT]): """Handler class to handle Telegram :attr:`telegram.Update.pre_checkout_query`. Warning: @@ -73,7 +73,7 @@ async def callback(update: Update, context: CallbackContext) __slots__ = ("pattern",) def __init__( - self: "PreCheckoutQueryHandler[CCT, RT]", + self, callback: HandlerCallback[Update, CCT, RT], block: DVType[bool] = DEFAULT_TRUE, pattern: Optional[Union[str, Pattern[str]]] = None, diff --git a/telegram/ext/_handlers/prefixhandler.py b/telegram/ext/_handlers/prefixhandler.py index bda265e1056..3b10a0a1caf 100644 --- a/telegram/ext/_handlers/prefixhandler.py +++ b/telegram/ext/_handlers/prefixhandler.py @@ -33,7 +33,7 @@ RT = TypeVar("RT") -class PrefixHandler(BaseHandler[Update, CCT, RT]): +class PrefixHandler(BaseHandler[Update, CCT]): """Handler class to handle custom prefix commands. This is an intermediate handler between :class:`MessageHandler` and :class:`CommandHandler`. @@ -123,7 +123,7 @@ async def callback(update: Update, context: CallbackContext) __slots__ = ("commands", "filters") def __init__( - self: "PrefixHandler[CCT, RT]", + self, prefix: SCT[str], command: SCT[str], callback: HandlerCallback[Update, CCT, RT], diff --git a/telegram/ext/_handlers/shippingqueryhandler.py b/telegram/ext/_handlers/shippingqueryhandler.py index c186513093a..f0234c2a962 100644 --- a/telegram/ext/_handlers/shippingqueryhandler.py +++ b/telegram/ext/_handlers/shippingqueryhandler.py @@ -21,10 +21,10 @@ from telegram import Update from telegram.ext._handlers.basehandler import BaseHandler -from telegram.ext._utils.types import CCT, RT +from telegram.ext._utils.types import CCT -class ShippingQueryHandler(BaseHandler[Update, CCT, RT]): +class ShippingQueryHandler(BaseHandler[Update, CCT]): """Handler class to handle Telegram :attr:`telegram.Update.shipping_query`. Warning: diff --git a/telegram/ext/_handlers/stringcommandhandler.py b/telegram/ext/_handlers/stringcommandhandler.py index ff655d9d6c4..d5c29bf6639 100644 --- a/telegram/ext/_handlers/stringcommandhandler.py +++ b/telegram/ext/_handlers/stringcommandhandler.py @@ -29,7 +29,7 @@ from telegram.ext import Application -class StringCommandHandler(BaseHandler[str, CCT, RT]): +class StringCommandHandler(BaseHandler[str, CCT]): """Handler class to handle string commands. Commands are string updates that start with ``/``. The handler will add a :obj:`list` to the :class:`CallbackContext` named :attr:`CallbackContext.args`. It will contain a list of strings, @@ -71,7 +71,7 @@ async def callback(update: str, context: CallbackContext) __slots__ = ("command",) def __init__( - self: "StringCommandHandler[CCT, RT]", + self, command: str, callback: HandlerCallback[str, CCT, RT], block: DVType[bool] = DEFAULT_TRUE, diff --git a/telegram/ext/_handlers/stringregexhandler.py b/telegram/ext/_handlers/stringregexhandler.py index bd97469495a..c2e22c10655 100644 --- a/telegram/ext/_handlers/stringregexhandler.py +++ b/telegram/ext/_handlers/stringregexhandler.py @@ -32,7 +32,7 @@ RT = TypeVar("RT") -class StringRegexHandler(BaseHandler[str, CCT, RT]): +class StringRegexHandler(BaseHandler[str, CCT]): """Handler class to handle string updates based on a regex which checks the update content. Read the documentation of the :mod:`re` module for more information. The :func:`re.match` @@ -74,7 +74,7 @@ async def callback(update: str, context: CallbackContext) __slots__ = ("pattern",) def __init__( - self: "StringRegexHandler[CCT, RT]", + self, pattern: Union[str, Pattern[str]], callback: HandlerCallback[str, CCT, RT], block: DVType[bool] = DEFAULT_TRUE, diff --git a/telegram/ext/_handlers/typehandler.py b/telegram/ext/_handlers/typehandler.py index 48a4530bcfa..151a7fbf137 100644 --- a/telegram/ext/_handlers/typehandler.py +++ b/telegram/ext/_handlers/typehandler.py @@ -29,7 +29,7 @@ UT = TypeVar("UT") -class TypeHandler(BaseHandler[UT, CCT, RT]): +class TypeHandler(BaseHandler[UT, CCT]): """Handler class to handle updates of custom types. Warning: @@ -70,7 +70,7 @@ async def callback(update: object, context: CallbackContext) __slots__ = ("strict", "type") def __init__( - self: "TypeHandler[UT, CCT, RT]", + self, type: Type[UT], # pylint: disable=redefined-builtin callback: HandlerCallback[UT, CCT, RT], strict: bool = False, From eb1f3ce614c9401e8e50e8403646840d64fca5e4 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Mon, 9 Sep 2024 22:33:17 +0200 Subject: [PATCH 08/16] try fixing workflow --- .github/workflows/type_completeness_monthly.yml | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/.github/workflows/type_completeness_monthly.yml b/.github/workflows/type_completeness_monthly.yml index e619f5fc19b..16f0daece2d 100644 --- a/.github/workflows/type_completeness_monthly.yml +++ b/.github/workflows/type_completeness_monthly.yml @@ -19,9 +19,14 @@ jobs: python-version: 3.12 pyright-version: ~=1.1.367 - name: Check Output - # Fail the workflow if the "base-completeness-score" of the previous step is less than 1 - run: | - if [ $(echo "${{ steps.pyright-type-completeness.outputs.base-completeness-score }}" | bc) -lt 1 ]; then - echo "Type completeness score is less than 1" - exit 1 - fi + uses: jannekem/run-python-script-action@v1 + with: + script: | + completeness = ${{ steps.pyright-type-completeness.outputs.base-completeness-score }} + if completeness > 1: + exit(0) + + text = f"Type Completeness Decreased to {completeness}. ❌") + error(text) + set_summamry(text) + exit(1) From 7d779511f061dc032465bf4046ddd21e7a282927 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Mon, 9 Sep 2024 22:34:38 +0200 Subject: [PATCH 09/16] again --- .github/workflows/type_completeness_monthly.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/type_completeness_monthly.yml b/.github/workflows/type_completeness_monthly.yml index 16f0daece2d..45e2a6c0d18 100644 --- a/.github/workflows/type_completeness_monthly.yml +++ b/.github/workflows/type_completeness_monthly.yml @@ -26,7 +26,7 @@ jobs: if completeness > 1: exit(0) - text = f"Type Completeness Decreased to {completeness}. ❌") + text = f"Type Completeness Decreased to {completeness}. ❌" error(text) set_summamry(text) exit(1) From 69e77351fc846addf6888be7f585e44cfc7e74c0 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Mon, 9 Sep 2024 22:38:23 +0200 Subject: [PATCH 10/16] again --- .github/workflows/type_completeness_monthly.yml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/.github/workflows/type_completeness_monthly.yml b/.github/workflows/type_completeness_monthly.yml index 45e2a6c0d18..e5035a4ae17 100644 --- a/.github/workflows/type_completeness_monthly.yml +++ b/.github/workflows/type_completeness_monthly.yml @@ -20,13 +20,18 @@ jobs: pyright-version: ~=1.1.367 - name: Check Output uses: jannekem/run-python-script-action@v1 + env: + TYPE_COMPLETENESS: ${{ steps.pyright-type-completeness.outputs.base-completeness-score }} with: script: | - completeness = ${{ steps.pyright-type-completeness.outputs.base-completeness-score }} + import os + + completeness = int(os.getenv("TYPE_COMPLETENESS")) + if completeness > 1: exit(0) text = f"Type Completeness Decreased to {completeness}. ❌" error(text) - set_summamry(text) + set_summary(text) exit(1) From dfdc68cddd2235638eb2364fd3535c5d3660d706 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Mon, 9 Sep 2024 22:39:58 +0200 Subject: [PATCH 11/16] again --- .github/workflows/type_completeness_monthly.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/type_completeness_monthly.yml b/.github/workflows/type_completeness_monthly.yml index e5035a4ae17..c0f28108d7b 100644 --- a/.github/workflows/type_completeness_monthly.yml +++ b/.github/workflows/type_completeness_monthly.yml @@ -26,6 +26,8 @@ jobs: script: | import os + print(f"Type Completeness: {os.getenv('TYPE_COMPLETENESS')}") + set_summary(f"Type Completeness: {os.getenv('TYPE_COMPLETENESS')}") completeness = int(os.getenv("TYPE_COMPLETENESS")) if completeness > 1: From fee9db753882231f3025db2adca33b05071994b4 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Fri, 13 Sep 2024 19:17:27 +0200 Subject: [PATCH 12/16] Try fixing --- .github/workflows/type_completeness_monthly.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/type_completeness_monthly.yml b/.github/workflows/type_completeness_monthly.yml index c0f28108d7b..bd2214fed5c 100644 --- a/.github/workflows/type_completeness_monthly.yml +++ b/.github/workflows/type_completeness_monthly.yml @@ -14,6 +14,7 @@ jobs: runs-on: ubuntu-latest steps: - uses: Bibo-Joshi/pyright-type-completeness@1.0.0 + id: pyright-type-completeness with: package-name: telegram python-version: 3.12 @@ -25,11 +26,10 @@ jobs: with: script: | import os - - print(f"Type Completeness: {os.getenv('TYPE_COMPLETENESS')}") - set_summary(f"Type Completeness: {os.getenv('TYPE_COMPLETENESS')}") completeness = int(os.getenv("TYPE_COMPLETENESS")) + set_summary(f"Type Completeness: {completeness}") + if completeness > 1: exit(0) From 535814eda247f5d3d813bfa1f2c5d12b4f55998f Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Fri, 13 Sep 2024 19:21:52 +0200 Subject: [PATCH 13/16] Aha, we're getting closer --- .github/workflows/type_completeness_monthly.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/type_completeness_monthly.yml b/.github/workflows/type_completeness_monthly.yml index bd2214fed5c..f8e53d77ea5 100644 --- a/.github/workflows/type_completeness_monthly.yml +++ b/.github/workflows/type_completeness_monthly.yml @@ -26,11 +26,11 @@ jobs: with: script: | import os - completeness = int(os.getenv("TYPE_COMPLETENESS")) + completeness = float(os.getenv("TYPE_COMPLETENESS")) set_summary(f"Type Completeness: {completeness}") - if completeness > 1: + if completeness >= 1: exit(0) text = f"Type Completeness Decreased to {completeness}. ❌" From df486e03579b19ee248500b889c8ef31d52630ba Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Fri, 13 Sep 2024 19:25:00 +0200 Subject: [PATCH 14/16] Small improvement --- .github/workflows/type_completeness_monthly.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/type_completeness_monthly.yml b/.github/workflows/type_completeness_monthly.yml index f8e53d77ea5..71f2cbd7761 100644 --- a/.github/workflows/type_completeness_monthly.yml +++ b/.github/workflows/type_completeness_monthly.yml @@ -28,8 +28,6 @@ jobs: import os completeness = float(os.getenv("TYPE_COMPLETENESS")) - set_summary(f"Type Completeness: {completeness}") - if completeness >= 1: exit(0) From 336187180279047bab2bc9d336737a8c82996fbc Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Fri, 13 Sep 2024 19:25:40 +0200 Subject: [PATCH 15/16] Revert "Revert previous commits for demo" This reverts commit 820a3ed823c6348b4b6663b0e767fd34a2d60e6f. --- examples/nestedconversationbot.py | 4 +-- telegram/ext/_application.py | 12 +++++---- telegram/ext/_callbackcontext.py | 16 +++++++----- telegram/ext/_handlers/basehandler.py | 7 ++--- .../_handlers/businessconnectionhandler.py | 4 +-- .../businessmessagesdeletedhandler.py | 4 +-- .../ext/_handlers/callbackqueryhandler.py | 4 +-- telegram/ext/_handlers/chatboosthandler.py | 8 +++--- .../ext/_handlers/chatjoinrequesthandler.py | 4 +-- telegram/ext/_handlers/chatmemberhandler.py | 4 +-- .../_handlers/choseninlineresulthandler.py | 4 +-- telegram/ext/_handlers/commandhandler.py | 4 +-- telegram/ext/_handlers/conversationhandler.py | 26 +++++++++---------- telegram/ext/_handlers/inlinequeryhandler.py | 4 +-- telegram/ext/_handlers/messagehandler.py | 4 +-- .../ext/_handlers/messagereactionhandler.py | 4 +-- telegram/ext/_handlers/pollanswerhandler.py | 4 +-- telegram/ext/_handlers/pollhandler.py | 4 +-- .../ext/_handlers/precheckoutqueryhandler.py | 4 +-- telegram/ext/_handlers/prefixhandler.py | 4 +-- .../ext/_handlers/shippingqueryhandler.py | 4 +-- .../ext/_handlers/stringcommandhandler.py | 4 +-- telegram/ext/_handlers/stringregexhandler.py | 4 +-- telegram/ext/_handlers/typehandler.py | 4 +-- 24 files changed, 76 insertions(+), 69 deletions(-) diff --git a/examples/nestedconversationbot.py b/examples/nestedconversationbot.py index 918f0becf24..fdc49de2b7f 100644 --- a/examples/nestedconversationbot.py +++ b/examples/nestedconversationbot.py @@ -371,8 +371,8 @@ def main() -> None: entry_points=[CommandHandler("start", start)], states={ SHOWING: [CallbackQueryHandler(start, pattern="^" + str(END) + "$")], - SELECTING_ACTION: selection_handlers, - SELECTING_LEVEL: selection_handlers, + SELECTING_ACTION: selection_handlers, # type: ignore[dict-item] + SELECTING_LEVEL: selection_handlers, # type: ignore[dict-item] DESCRIBING_SELF: [description_conv], STOPPING: [CommandHandler("start", start)], }, diff --git a/telegram/ext/_application.py b/telegram/ext/_application.py index a1013ce570a..86e38b1de9f 100644 --- a/telegram/ext/_application.py +++ b/telegram/ext/_application.py @@ -323,7 +323,7 @@ def __init__( self.update_queue: asyncio.Queue[object] = update_queue self.context_types: ContextTypes[CCT, UD, CD, BD] = context_types self.updater: Optional[Updater] = updater - self.handlers: Dict[int, List[BaseHandler[Any, CCT]]] = {} + self.handlers: Dict[int, List[BaseHandler[Any, CCT, Any]]] = {} self.error_handlers: Dict[ HandlerCallback[object, CCT, None], Union[bool, DefaultValue[bool]] ] = {} @@ -1352,7 +1352,7 @@ async def process_update(self, update: object) -> None: # (in __create_task_callback) self._mark_for_persistence_update(update=update) - def add_handler(self, handler: BaseHandler[Any, CCT], group: int = DEFAULT_GROUP) -> None: + def add_handler(self, handler: BaseHandler[Any, CCT, Any], group: int = DEFAULT_GROUP) -> None: """Register a handler. TL;DR: Order and priority counts. 0 or 1 handlers per group will be used. End handling of @@ -1420,8 +1420,8 @@ def add_handler(self, handler: BaseHandler[Any, CCT], group: int = DEFAULT_GROUP def add_handlers( self, handlers: Union[ - Union[List[BaseHandler[Any, CCT]], Tuple[BaseHandler[Any, CCT]]], - Dict[int, Union[List[BaseHandler[Any, CCT]], Tuple[BaseHandler[Any, CCT]]]], + Union[List[BaseHandler[Any, CCT, Any]], Tuple[BaseHandler[Any, CCT, Any]]], + Dict[int, Union[List[BaseHandler[Any, CCT, Any]], Tuple[BaseHandler[Any, CCT, Any]]]], ], group: Union[int, DefaultValue[int]] = _DEFAULT_0, ) -> None: @@ -1469,7 +1469,9 @@ def add_handlers( "dictionary where the keys are groups and values are sequences of handlers." ) - def remove_handler(self, handler: BaseHandler[Any, CCT], group: int = DEFAULT_GROUP) -> None: + def remove_handler( + self, handler: BaseHandler[Any, CCT, Any], group: int = DEFAULT_GROUP + ) -> None: """Remove a handler from the specified group. Args: diff --git a/telegram/ext/_callbackcontext.py b/telegram/ext/_callbackcontext.py index ea708e0e6b8..dfd2c3cc8d7 100644 --- a/telegram/ext/_callbackcontext.py +++ b/telegram/ext/_callbackcontext.py @@ -29,6 +29,7 @@ NoReturn, Optional, Type, + TypeVar, Union, ) @@ -49,6 +50,9 @@ "/wiki/Storing-bot%2C-user-and-chat-related-data" ) +# something like poor mans "typing.Self" for py<3.11 +ST = TypeVar("ST", bound="CallbackContext[Any, Any, Any, Any]") + class CallbackContext(Generic[BT, UD, CD, BD]): """ @@ -133,24 +137,24 @@ class CallbackContext(Generic[BT, UD, CD, BD]): ) def __init__( - self: "CCT", - application: "Application[BT, CCT, UD, CD, BD, Any]", + self: ST, + application: "Application[BT, ST, UD, CD, BD, Any]", chat_id: Optional[int] = None, user_id: Optional[int] = None, ): - self._application: Application[BT, CCT, UD, CD, BD, Any] = application + self._application: Application[BT, ST, UD, CD, BD, Any] = application self._chat_id: Optional[int] = chat_id self._user_id: Optional[int] = user_id self.args: Optional[List[str]] = None self.matches: Optional[List[Match[str]]] = None self.error: Optional[Exception] = None - self.job: Optional[Job[CCT]] = None + self.job: Optional[Job[Any]] = None self.coroutine: Optional[ Union[Generator[Optional[Future[object]], None, Any], Awaitable[Any]] ] = None @property - def application(self) -> "Application[BT, CCT, UD, CD, BD, Any]": + def application(self) -> "Application[BT, ST, UD, CD, BD, Any]": """:class:`telegram.ext.Application`: The application associated with this context.""" return self._application @@ -398,7 +402,7 @@ def bot(self) -> BT: return self._application.bot @property - def job_queue(self) -> Optional["JobQueue[CCT]"]: + def job_queue(self) -> Optional["JobQueue[ST]"]: """ :class:`telegram.ext.JobQueue`: The :class:`JobQueue` used by the :class:`telegram.ext.Application`. diff --git a/telegram/ext/_handlers/basehandler.py b/telegram/ext/_handlers/basehandler.py index ea0d9b1640f..bf6f5f155df 100644 --- a/telegram/ext/_handlers/basehandler.py +++ b/telegram/ext/_handlers/basehandler.py @@ -32,14 +32,14 @@ UT = TypeVar("UT") -class BaseHandler(Generic[UT, CCT], ABC): +class BaseHandler(Generic[UT, CCT, RT], ABC): """The base class for all update handlers. Create custom handlers by inheriting from it. Warning: When setting :paramref:`block` to :obj:`False`, you cannot rely on adding custom attributes to :class:`telegram.ext.CallbackContext`. See its docs for more info. - This class is a :class:`~typing.Generic` class and accepts two type variables: + This class is a :class:`~typing.Generic` class and accepts three type variables: 1. The type of the updates that this handler will handle. Must coincide with the type of the first argument of :paramref:`callback`. :meth:`check_update` must only accept @@ -54,6 +54,7 @@ class BaseHandler(Generic[UT, CCT], ABC): For this type variable, one should usually provide a :class:`~typing.TypeVar` that is also used for the mentioned method arguments. That way, a type checker can check whether this handler fits the definition of the :class:`~Application`. + 3. The return type of the :paramref:`callback` function accepted by this handler. .. seealso:: :wiki:`Types of Handlers ` @@ -89,7 +90,7 @@ async def callback(update: Update, context: CallbackContext) ) def __init__( - self, + self: "BaseHandler[UT, CCT, RT]", callback: HandlerCallback[UT, CCT, RT], block: DVType[bool] = DEFAULT_TRUE, ): diff --git a/telegram/ext/_handlers/businessconnectionhandler.py b/telegram/ext/_handlers/businessconnectionhandler.py index c8cb3e843d0..e5d09c77cf1 100644 --- a/telegram/ext/_handlers/businessconnectionhandler.py +++ b/telegram/ext/_handlers/businessconnectionhandler.py @@ -29,7 +29,7 @@ RT = TypeVar("RT") -class BusinessConnectionHandler(BaseHandler[Update, CCT]): +class BusinessConnectionHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle Telegram :attr:`Business Connections `. @@ -65,7 +65,7 @@ async def callback(update: Update, context: CallbackContext) ) def __init__( - self, + self: "BusinessConnectionHandler[CCT, RT]", callback: HandlerCallback[Update, CCT, RT], user_id: Optional[SCT[int]] = None, username: Optional[SCT[str]] = None, diff --git a/telegram/ext/_handlers/businessmessagesdeletedhandler.py b/telegram/ext/_handlers/businessmessagesdeletedhandler.py index 0ceb58fe05c..c041bb0adc0 100644 --- a/telegram/ext/_handlers/businessmessagesdeletedhandler.py +++ b/telegram/ext/_handlers/businessmessagesdeletedhandler.py @@ -29,7 +29,7 @@ RT = TypeVar("RT") -class BusinessMessagesDeletedHandler(BaseHandler[Update, CCT]): +class BusinessMessagesDeletedHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle :attr:`deleted Telegram Business messages `. @@ -65,7 +65,7 @@ async def callback(update: Update, context: CallbackContext) ) def __init__( - self, + self: "BusinessMessagesDeletedHandler[CCT, RT]", callback: HandlerCallback[Update, CCT, RT], chat_id: Optional[SCT[int]] = None, username: Optional[SCT[str]] = None, diff --git a/telegram/ext/_handlers/callbackqueryhandler.py b/telegram/ext/_handlers/callbackqueryhandler.py index a149739a2fb..5e0a0a12aa0 100644 --- a/telegram/ext/_handlers/callbackqueryhandler.py +++ b/telegram/ext/_handlers/callbackqueryhandler.py @@ -33,7 +33,7 @@ RT = TypeVar("RT") -class CallbackQueryHandler(BaseHandler[Update, CCT]): +class CallbackQueryHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle Telegram :attr:`callback queries `. Optionally based on a regex. @@ -125,7 +125,7 @@ async def callback(update: Update, context: CallbackContext) __slots__ = ("game_pattern", "pattern") def __init__( - self, + self: "CallbackQueryHandler[CCT, RT]", callback: HandlerCallback[Update, CCT, RT], pattern: Optional[ Union[str, Pattern[str], type, Callable[[object], Optional[bool]]] diff --git a/telegram/ext/_handlers/chatboosthandler.py b/telegram/ext/_handlers/chatboosthandler.py index 9d7219632a1..7a2dd1be6a6 100644 --- a/telegram/ext/_handlers/chatboosthandler.py +++ b/telegram/ext/_handlers/chatboosthandler.py @@ -23,10 +23,10 @@ from telegram import Update from telegram.ext._handlers.basehandler import BaseHandler from telegram.ext._utils._update_parsing import parse_chat_id, parse_username -from telegram.ext._utils.types import CCT, HandlerCallback +from telegram.ext._utils.types import CCT, RT, HandlerCallback -class ChatBoostHandler(BaseHandler[Update, CCT]): +class ChatBoostHandler(BaseHandler[Update, CCT, RT]): """ Handler class to handle Telegram updates that contain a chat boost. @@ -84,8 +84,8 @@ async def callback(update: Update, context: CallbackContext) and :attr:`telegram.Update.removed_chat_boost`.""" def __init__( - self, - callback: HandlerCallback[Update, CCT, None], + self: "ChatBoostHandler[CCT, RT]", + callback: HandlerCallback[Update, CCT, RT], chat_boost_types: int = CHAT_BOOST, chat_id: Optional[int] = None, chat_username: Optional[str] = None, diff --git a/telegram/ext/_handlers/chatjoinrequesthandler.py b/telegram/ext/_handlers/chatjoinrequesthandler.py index 7007f61ab03..04576e63c32 100644 --- a/telegram/ext/_handlers/chatjoinrequesthandler.py +++ b/telegram/ext/_handlers/chatjoinrequesthandler.py @@ -28,7 +28,7 @@ from telegram.ext._utils.types import CCT, HandlerCallback -class ChatJoinRequestHandler(BaseHandler[Update, CCT]): +class ChatJoinRequestHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle Telegram updates that contain :attr:`telegram.Update.chat_join_request`. @@ -81,7 +81,7 @@ async def callback(update: Update, context: CallbackContext) ) def __init__( - self, + self: "ChatJoinRequestHandler[CCT, RT]", callback: HandlerCallback[Update, CCT, RT], chat_id: Optional[SCT[int]] = None, username: Optional[SCT[str]] = None, diff --git a/telegram/ext/_handlers/chatmemberhandler.py b/telegram/ext/_handlers/chatmemberhandler.py index 592361b64c5..cf8bb28db10 100644 --- a/telegram/ext/_handlers/chatmemberhandler.py +++ b/telegram/ext/_handlers/chatmemberhandler.py @@ -29,7 +29,7 @@ RT = TypeVar("RT") -class ChatMemberHandler(BaseHandler[Update, CCT]): +class ChatMemberHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle Telegram updates that contain a chat member update. Warning: @@ -87,7 +87,7 @@ async def callback(update: Update, context: CallbackContext) and :attr:`telegram.Update.chat_member`.""" def __init__( - self, + self: "ChatMemberHandler[CCT, RT]", callback: HandlerCallback[Update, CCT, RT], chat_member_types: int = MY_CHAT_MEMBER, block: DVType[bool] = DEFAULT_TRUE, diff --git a/telegram/ext/_handlers/choseninlineresulthandler.py b/telegram/ext/_handlers/choseninlineresulthandler.py index db7a8721448..feac28ba658 100644 --- a/telegram/ext/_handlers/choseninlineresulthandler.py +++ b/telegram/ext/_handlers/choseninlineresulthandler.py @@ -32,7 +32,7 @@ from telegram.ext import Application -class ChosenInlineResultHandler(BaseHandler[Update, CCT]): +class ChosenInlineResultHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle Telegram updates that contain :attr:`telegram.Update.chosen_inline_result`. @@ -76,7 +76,7 @@ async def callback(update: Update, context: CallbackContext) __slots__ = ("pattern",) def __init__( - self, + self: "ChosenInlineResultHandler[CCT, RT]", callback: HandlerCallback[Update, CCT, RT], block: DVType[bool] = DEFAULT_TRUE, pattern: Optional[Union[str, Pattern[str]]] = None, diff --git a/telegram/ext/_handlers/commandhandler.py b/telegram/ext/_handlers/commandhandler.py index edad3963aad..54d01acd6d0 100644 --- a/telegram/ext/_handlers/commandhandler.py +++ b/telegram/ext/_handlers/commandhandler.py @@ -33,7 +33,7 @@ RT = TypeVar("RT") -class CommandHandler(BaseHandler[Update, CCT]): +class CommandHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle Telegram commands. Commands are Telegram messages that start with ``/``, optionally followed by an ``@`` and the @@ -118,7 +118,7 @@ async def callback(update: Update, context: CallbackContext) __slots__ = ("commands", "filters", "has_args") def __init__( - self, + self: "CommandHandler[CCT, RT]", command: SCT[str], callback: HandlerCallback[Update, CCT, RT], filters: Optional[filters_module.BaseFilter] = None, diff --git a/telegram/ext/_handlers/conversationhandler.py b/telegram/ext/_handlers/conversationhandler.py index f3fad9f8324..83fa373fc61 100644 --- a/telegram/ext/_handlers/conversationhandler.py +++ b/telegram/ext/_handlers/conversationhandler.py @@ -55,7 +55,7 @@ if TYPE_CHECKING: from telegram.ext import Application, Job, JobQueue -_CheckUpdateType = Tuple[object, ConversationKey, BaseHandler[Update, CCT], object] +_CheckUpdateType = Tuple[object, ConversationKey, BaseHandler[Update, CCT, object], object] _LOGGER = get_logger(__name__, class_name="ConversationHandler") @@ -119,7 +119,7 @@ def resolve(self) -> object: return res -class ConversationHandler(BaseHandler[Update, CCT]): +class ConversationHandler(BaseHandler[Update, CCT, object]): """ A handler to hold a conversation with a single or multiple users through Telegram updates by managing three collections of other handlers. @@ -296,10 +296,10 @@ class ConversationHandler(BaseHandler[Update, CCT]): # pylint: disable=super-init-not-called def __init__( - self, - entry_points: List[BaseHandler[Update, CCT]], - states: Dict[object, List[BaseHandler[Update, CCT]]], - fallbacks: List[BaseHandler[Update, CCT]], + self: "ConversationHandler[CCT]", + entry_points: List[BaseHandler[Update, CCT, object]], + states: Dict[object, List[BaseHandler[Update, CCT, object]]], + fallbacks: List[BaseHandler[Update, CCT, object]], allow_reentry: bool = False, per_chat: bool = True, per_user: bool = True, @@ -324,9 +324,9 @@ def __init__( # Store the actual setting in a protected variable instead self._block: DVType[bool] = block - self._entry_points: List[BaseHandler[Update, CCT]] = entry_points - self._states: Dict[object, List[BaseHandler[Update, CCT]]] = states - self._fallbacks: List[BaseHandler[Update, CCT]] = fallbacks + self._entry_points: List[BaseHandler[Update, CCT, object]] = entry_points + self._states: Dict[object, List[BaseHandler[Update, CCT, object]]] = states + self._fallbacks: List[BaseHandler[Update, CCT, object]] = fallbacks self._allow_reentry: bool = allow_reentry self._per_user: bool = per_user @@ -359,7 +359,7 @@ def __init__( stacklevel=2, ) - all_handlers: List[BaseHandler[Update, CCT]] = [] + all_handlers: List[BaseHandler[Update, CCT, object]] = [] all_handlers.extend(entry_points) all_handlers.extend(fallbacks) @@ -466,7 +466,7 @@ def __repr__(self) -> str: ) @property - def entry_points(self) -> List[BaseHandler[Update, CCT]]: + def entry_points(self) -> List[BaseHandler[Update, CCT, object]]: """List[:class:`telegram.ext.BaseHandler`]: A list of :obj:`BaseHandler` objects that can trigger the start of the conversation. """ @@ -479,7 +479,7 @@ def entry_points(self, _: object) -> NoReturn: ) @property - def states(self) -> Dict[object, List[BaseHandler[Update, CCT]]]: + def states(self) -> Dict[object, List[BaseHandler[Update, CCT, object]]]: """Dict[:obj:`object`, List[:class:`telegram.ext.BaseHandler`]]: A :obj:`dict` that defines the different states of conversation a user can be in and one or more associated :obj:`BaseHandler` objects that should be used in that state. @@ -491,7 +491,7 @@ def states(self, _: object) -> NoReturn: raise AttributeError("You can not assign a new value to states after initialization.") @property - def fallbacks(self) -> List[BaseHandler[Update, CCT]]: + def fallbacks(self) -> List[BaseHandler[Update, CCT, object]]: """List[:class:`telegram.ext.BaseHandler`]: A list of handlers that might be used if the user is in a conversation, but every handler for their current state returned :obj:`False` on :meth:`check_update`. diff --git a/telegram/ext/_handlers/inlinequeryhandler.py b/telegram/ext/_handlers/inlinequeryhandler.py index 21a5925cb1d..9d2cbb710b2 100644 --- a/telegram/ext/_handlers/inlinequeryhandler.py +++ b/telegram/ext/_handlers/inlinequeryhandler.py @@ -32,7 +32,7 @@ RT = TypeVar("RT") -class InlineQueryHandler(BaseHandler[Update, CCT]): +class InlineQueryHandler(BaseHandler[Update, CCT, RT]): """ BaseHandler class to handle Telegram updates that contain a :attr:`telegram.Update.inline_query`. @@ -87,7 +87,7 @@ async def callback(update: Update, context: CallbackContext) __slots__ = ("chat_types", "pattern") def __init__( - self, + self: "InlineQueryHandler[CCT, RT]", callback: HandlerCallback[Update, CCT, RT], pattern: Optional[Union[str, Pattern[str]]] = None, block: DVType[bool] = DEFAULT_TRUE, diff --git a/telegram/ext/_handlers/messagehandler.py b/telegram/ext/_handlers/messagehandler.py index 0cd42884ba6..43d8c8d8115 100644 --- a/telegram/ext/_handlers/messagehandler.py +++ b/telegram/ext/_handlers/messagehandler.py @@ -32,7 +32,7 @@ RT = TypeVar("RT") -class MessageHandler(BaseHandler[Update, CCT]): +class MessageHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle Telegram messages. They might contain text, media or status updates. @@ -75,7 +75,7 @@ async def callback(update: Update, context: CallbackContext) __slots__ = ("filters",) def __init__( - self, + self: "MessageHandler[CCT, RT]", filters: Optional[filters_module.BaseFilter], callback: HandlerCallback[Update, CCT, RT], block: DVType[bool] = DEFAULT_TRUE, diff --git a/telegram/ext/_handlers/messagereactionhandler.py b/telegram/ext/_handlers/messagereactionhandler.py index 4b52a17a8ab..34a5292be96 100644 --- a/telegram/ext/_handlers/messagereactionhandler.py +++ b/telegram/ext/_handlers/messagereactionhandler.py @@ -28,7 +28,7 @@ from telegram.ext._utils.types import CCT, HandlerCallback -class MessageReactionHandler(BaseHandler[Update, CCT]): +class MessageReactionHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle Telegram updates that contain a message reaction. Note: @@ -110,7 +110,7 @@ async def callback(update: Update, context: CallbackContext) and :attr:`telegram.Update.message_reaction_count`.""" def __init__( - self, + self: "MessageReactionHandler[CCT, RT]", callback: HandlerCallback[Update, CCT, RT], chat_id: Optional[SCT[int]] = None, chat_username: Optional[SCT[str]] = None, diff --git a/telegram/ext/_handlers/pollanswerhandler.py b/telegram/ext/_handlers/pollanswerhandler.py index 91e8df82b70..5e0bb34dbea 100644 --- a/telegram/ext/_handlers/pollanswerhandler.py +++ b/telegram/ext/_handlers/pollanswerhandler.py @@ -21,10 +21,10 @@ from telegram import Update from telegram.ext._handlers.basehandler import BaseHandler -from telegram.ext._utils.types import CCT +from telegram.ext._utils.types import CCT, RT -class PollAnswerHandler(BaseHandler[Update, CCT]): +class PollAnswerHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle Telegram updates that contain a :attr:`poll answer `. diff --git a/telegram/ext/_handlers/pollhandler.py b/telegram/ext/_handlers/pollhandler.py index fb183c53805..6bb41c3237e 100644 --- a/telegram/ext/_handlers/pollhandler.py +++ b/telegram/ext/_handlers/pollhandler.py @@ -21,10 +21,10 @@ from telegram import Update from telegram.ext._handlers.basehandler import BaseHandler -from telegram.ext._utils.types import CCT +from telegram.ext._utils.types import CCT, RT -class PollHandler(BaseHandler[Update, CCT]): +class PollHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle Telegram updates that contain a :attr:`poll `. diff --git a/telegram/ext/_handlers/precheckoutqueryhandler.py b/telegram/ext/_handlers/precheckoutqueryhandler.py index 3c6f3f186df..de035364ec9 100644 --- a/telegram/ext/_handlers/precheckoutqueryhandler.py +++ b/telegram/ext/_handlers/precheckoutqueryhandler.py @@ -31,7 +31,7 @@ RT = TypeVar("RT") -class PreCheckoutQueryHandler(BaseHandler[Update, CCT]): +class PreCheckoutQueryHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle Telegram :attr:`telegram.Update.pre_checkout_query`. Warning: @@ -73,7 +73,7 @@ async def callback(update: Update, context: CallbackContext) __slots__ = ("pattern",) def __init__( - self, + self: "PreCheckoutQueryHandler[CCT, RT]", callback: HandlerCallback[Update, CCT, RT], block: DVType[bool] = DEFAULT_TRUE, pattern: Optional[Union[str, Pattern[str]]] = None, diff --git a/telegram/ext/_handlers/prefixhandler.py b/telegram/ext/_handlers/prefixhandler.py index 3b10a0a1caf..bda265e1056 100644 --- a/telegram/ext/_handlers/prefixhandler.py +++ b/telegram/ext/_handlers/prefixhandler.py @@ -33,7 +33,7 @@ RT = TypeVar("RT") -class PrefixHandler(BaseHandler[Update, CCT]): +class PrefixHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle custom prefix commands. This is an intermediate handler between :class:`MessageHandler` and :class:`CommandHandler`. @@ -123,7 +123,7 @@ async def callback(update: Update, context: CallbackContext) __slots__ = ("commands", "filters") def __init__( - self, + self: "PrefixHandler[CCT, RT]", prefix: SCT[str], command: SCT[str], callback: HandlerCallback[Update, CCT, RT], diff --git a/telegram/ext/_handlers/shippingqueryhandler.py b/telegram/ext/_handlers/shippingqueryhandler.py index f0234c2a962..c186513093a 100644 --- a/telegram/ext/_handlers/shippingqueryhandler.py +++ b/telegram/ext/_handlers/shippingqueryhandler.py @@ -21,10 +21,10 @@ from telegram import Update from telegram.ext._handlers.basehandler import BaseHandler -from telegram.ext._utils.types import CCT +from telegram.ext._utils.types import CCT, RT -class ShippingQueryHandler(BaseHandler[Update, CCT]): +class ShippingQueryHandler(BaseHandler[Update, CCT, RT]): """Handler class to handle Telegram :attr:`telegram.Update.shipping_query`. Warning: diff --git a/telegram/ext/_handlers/stringcommandhandler.py b/telegram/ext/_handlers/stringcommandhandler.py index d5c29bf6639..ff655d9d6c4 100644 --- a/telegram/ext/_handlers/stringcommandhandler.py +++ b/telegram/ext/_handlers/stringcommandhandler.py @@ -29,7 +29,7 @@ from telegram.ext import Application -class StringCommandHandler(BaseHandler[str, CCT]): +class StringCommandHandler(BaseHandler[str, CCT, RT]): """Handler class to handle string commands. Commands are string updates that start with ``/``. The handler will add a :obj:`list` to the :class:`CallbackContext` named :attr:`CallbackContext.args`. It will contain a list of strings, @@ -71,7 +71,7 @@ async def callback(update: str, context: CallbackContext) __slots__ = ("command",) def __init__( - self, + self: "StringCommandHandler[CCT, RT]", command: str, callback: HandlerCallback[str, CCT, RT], block: DVType[bool] = DEFAULT_TRUE, diff --git a/telegram/ext/_handlers/stringregexhandler.py b/telegram/ext/_handlers/stringregexhandler.py index c2e22c10655..bd97469495a 100644 --- a/telegram/ext/_handlers/stringregexhandler.py +++ b/telegram/ext/_handlers/stringregexhandler.py @@ -32,7 +32,7 @@ RT = TypeVar("RT") -class StringRegexHandler(BaseHandler[str, CCT]): +class StringRegexHandler(BaseHandler[str, CCT, RT]): """Handler class to handle string updates based on a regex which checks the update content. Read the documentation of the :mod:`re` module for more information. The :func:`re.match` @@ -74,7 +74,7 @@ async def callback(update: str, context: CallbackContext) __slots__ = ("pattern",) def __init__( - self, + self: "StringRegexHandler[CCT, RT]", pattern: Union[str, Pattern[str]], callback: HandlerCallback[str, CCT, RT], block: DVType[bool] = DEFAULT_TRUE, diff --git a/telegram/ext/_handlers/typehandler.py b/telegram/ext/_handlers/typehandler.py index 151a7fbf137..48a4530bcfa 100644 --- a/telegram/ext/_handlers/typehandler.py +++ b/telegram/ext/_handlers/typehandler.py @@ -29,7 +29,7 @@ UT = TypeVar("UT") -class TypeHandler(BaseHandler[UT, CCT]): +class TypeHandler(BaseHandler[UT, CCT, RT]): """Handler class to handle updates of custom types. Warning: @@ -70,7 +70,7 @@ async def callback(update: object, context: CallbackContext) __slots__ = ("strict", "type") def __init__( - self, + self: "TypeHandler[UT, CCT, RT]", type: Type[UT], # pylint: disable=redefined-builtin callback: HandlerCallback[UT, CCT, RT], strict: bool = False, From dbdec8f4e8dbd4e08aaff5055da037fafae271dc Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Fri, 13 Sep 2024 19:27:03 +0200 Subject: [PATCH 16/16] Remove demo pr trigger --- .github/workflows/type_completeness_monthly.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/type_completeness_monthly.yml b/.github/workflows/type_completeness_monthly.yml index 71f2cbd7761..a5492f9030c 100644 --- a/.github/workflows/type_completeness_monthly.yml +++ b/.github/workflows/type_completeness_monthly.yml @@ -3,10 +3,6 @@ on: schedule: # Run first friday of the month at 03:17 - odd time to spread load on GitHub Actions - cron: '17 3 1-7 * 5' - pull_request: - # for demo only - branches: - - master jobs: test-type-completeness: pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy