From 46cd99fb2c3f72e9f2cf2a51bb6790934e238671 Mon Sep 17 00:00:00 2001 From: Iulian Onofrei Date: Sun, 11 Jul 2021 20:51:14 +0100 Subject: [PATCH 1/2] Fix incomplete type annotations for CallbackContext --- telegram/ext/callbackcontext.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/telegram/ext/callbackcontext.py b/telegram/ext/callbackcontext.py index 5c5e9bedfe2..2e40aa0e849 100644 --- a/telegram/ext/callbackcontext.py +++ b/telegram/ext/callbackcontext.py @@ -40,6 +40,7 @@ if TYPE_CHECKING: from telegram import Bot from telegram.ext import Dispatcher, Job, JobQueue + from telegram.ext.utils.types import CCT CC = TypeVar('CC', bound='CallbackContext') @@ -105,7 +106,7 @@ class CallbackContext(Generic[UD, CD, BD]): '__dict__', ) - def __init__(self, dispatcher: 'Dispatcher'): + def __init__(self: 'CallbackContext[UD, CD, BD]', dispatcher: 'Dispatcher[CCT, UD, CD, BD]'): """ Args: dispatcher (:class:`telegram.ext.Dispatcher`): @@ -125,7 +126,7 @@ def __init__(self, dispatcher: 'Dispatcher'): self.async_kwargs: Optional[Dict[str, object]] = None @property - def dispatcher(self) -> 'Dispatcher': + def dispatcher(self) -> 'Dispatcher[CCT, UD, CD, BD]': """:class:`telegram.ext.Dispatcher`: The dispatcher associated with this context.""" return self._dispatcher From c4c0768874e9fbfa4d2250ffacfc49efd7ec9a88 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Wed, 11 Aug 2021 21:47:08 +0200 Subject: [PATCH 2/2] Fine tune type hinting --- telegram/ext/callbackcontext.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/telegram/ext/callbackcontext.py b/telegram/ext/callbackcontext.py index 2e40aa0e849..501a62fbf82 100644 --- a/telegram/ext/callbackcontext.py +++ b/telegram/ext/callbackcontext.py @@ -30,7 +30,6 @@ Union, Generic, Type, - TypeVar, ) from telegram import Update, CallbackQuery @@ -42,8 +41,6 @@ from telegram.ext import Dispatcher, Job, JobQueue from telegram.ext.utils.types import CCT -CC = TypeVar('CC', bound='CallbackContext') - class CallbackContext(Generic[UD, CD, BD]): """ @@ -106,7 +103,7 @@ class CallbackContext(Generic[UD, CD, BD]): '__dict__', ) - def __init__(self: 'CallbackContext[UD, CD, BD]', dispatcher: 'Dispatcher[CCT, UD, CD, BD]'): + def __init__(self: 'CCT', dispatcher: 'Dispatcher[CCT, UD, CD, BD]'): """ Args: dispatcher (:class:`telegram.ext.Dispatcher`): @@ -226,13 +223,13 @@ def drop_callback_data(self, callback_query: CallbackQuery) -> None: @classmethod def from_error( - cls: Type[CC], + cls: Type['CCT'], update: object, error: Exception, - dispatcher: 'Dispatcher', + dispatcher: 'Dispatcher[CCT, UD, CD, BD]', async_args: Union[List, Tuple] = None, async_kwargs: Dict[str, object] = None, - ) -> CC: + ) -> 'CCT': """ Constructs an instance of :class:`telegram.ext.CallbackContext` to be passed to the error handlers. @@ -262,7 +259,9 @@ def from_error( return self @classmethod - def from_update(cls: Type[CC], update: object, dispatcher: 'Dispatcher') -> CC: + def from_update( + cls: Type['CCT'], update: object, dispatcher: 'Dispatcher[CCT, UD, CD, BD]' + ) -> 'CCT': """ Constructs an instance of :class:`telegram.ext.CallbackContext` to be passed to the handlers. @@ -277,7 +276,7 @@ def from_update(cls: Type[CC], update: object, dispatcher: 'Dispatcher') -> CC: Returns: :class:`telegram.ext.CallbackContext` """ - self = cls(dispatcher) + self = cls(dispatcher) # type: ignore[arg-type] if update is not None and isinstance(update, Update): chat = update.effective_chat @@ -296,7 +295,7 @@ def from_update(cls: Type[CC], update: object, dispatcher: 'Dispatcher') -> CC: return self @classmethod - def from_job(cls: Type[CC], job: 'Job', dispatcher: 'Dispatcher') -> CC: + def from_job(cls: Type['CCT'], job: 'Job', dispatcher: 'Dispatcher[CCT, UD, CD, BD]') -> 'CCT': """ Constructs an instance of :class:`telegram.ext.CallbackContext` to be passed to a job callback. @@ -311,7 +310,7 @@ def from_job(cls: Type[CC], job: 'Job', dispatcher: 'Dispatcher') -> CC: Returns: :class:`telegram.ext.CallbackContext` """ - self = cls(dispatcher) + self = cls(dispatcher) # type: ignore[arg-type] self.job = job return self