Skip to content

Fix incomplete type annotations for CallbackContext #2587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions telegram/ext/callbackcontext.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
Union,
Generic,
Type,
TypeVar,
)

from telegram import Update, CallbackQuery
Expand All @@ -40,8 +39,7 @@
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]):
Expand Down Expand Up @@ -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`):
Expand All @@ -125,7 +123,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

Expand Down Expand Up @@ -225,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.
Expand Down Expand Up @@ -261,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.
Expand All @@ -276,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
Expand All @@ -295,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.
Expand All @@ -310,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

Expand Down