Improve Type Hinting for CallbackContext (#2587)

Co-authored-by: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com>
This commit is contained in:
Iulian Onofrei 2021-08-12 09:11:00 +03:00 committed by Hinrich Mahler
parent 97a585f240
commit 8a7aeaf86f

View file

@ -30,7 +30,6 @@ from typing import (
Union,
Generic,
Type,
TypeVar,
)
from telegram import Update, CallbackQuery
@ -40,8 +39,7 @@ from telegram.ext.utils.types import UD, CD, BD
if TYPE_CHECKING:
from telegram import Bot
from telegram.ext import Dispatcher, Job, JobQueue
CC = TypeVar('CC', bound='CallbackContext')
from telegram.ext.utils.types import CCT
class CallbackContext(Generic[UD, CD, BD]):
@ -105,7 +103,7 @@ class CallbackContext(Generic[UD, CD, BD]):
'__dict__',
)
def __init__(self, dispatcher: 'Dispatcher'):
def __init__(self: 'CCT', dispatcher: 'Dispatcher[CCT, UD, CD, BD]'):
"""
Args:
dispatcher (:class:`telegram.ext.Dispatcher`):
@ -125,7 +123,7 @@ class CallbackContext(Generic[UD, CD, BD]):
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
@ -225,13 +223,13 @@ class CallbackContext(Generic[UD, CD, BD]):
@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.
@ -261,7 +259,9 @@ class CallbackContext(Generic[UD, CD, BD]):
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.
@ -276,7 +276,7 @@ class CallbackContext(Generic[UD, CD, BD]):
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
@ -295,7 +295,7 @@ class CallbackContext(Generic[UD, CD, BD]):
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.
@ -310,7 +310,7 @@ class CallbackContext(Generic[UD, CD, BD]):
Returns:
:class:`telegram.ext.CallbackContext`
"""
self = cls(dispatcher)
self = cls(dispatcher) # type: ignore[arg-type]
self.job = job
return self