mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-11-21 22:56:38 +01:00
Rename Job.context
to Job.data
(#3028)
This commit is contained in:
parent
e47d18c9ec
commit
1f7efe4519
4 changed files with 53 additions and 39 deletions
|
@ -43,7 +43,7 @@ async def start(update: Update, context: CallbackContext.DEFAULT_TYPE) -> None:
|
|||
async def alarm(context: CallbackContext.DEFAULT_TYPE) -> None:
|
||||
"""Send the alarm message."""
|
||||
job = context.job
|
||||
await context.bot.send_message(job.chat_id, text=f"Beep! {job.context} seconds are over!")
|
||||
await context.bot.send_message(job.chat_id, text=f"Beep! {job.data} seconds are over!")
|
||||
|
||||
|
||||
def remove_job_if_exists(name: str, context: CallbackContext.DEFAULT_TYPE) -> bool:
|
||||
|
@ -67,7 +67,7 @@ async def set_timer(update: Update, context: CallbackContext.DEFAULT_TYPE) -> No
|
|||
return
|
||||
|
||||
job_removed = remove_job_if_exists(str(chat_id), context)
|
||||
context.job_queue.run_once(alarm, due, chat_id=chat_id, name=str(chat_id), context=due)
|
||||
context.job_queue.run_once(alarm, due, chat_id=chat_id, name=str(chat_id), data=due)
|
||||
|
||||
text = "Timer successfully set!"
|
||||
if job_removed:
|
||||
|
|
|
@ -64,7 +64,7 @@ _logger = logging.getLogger(__name__)
|
|||
@dataclass
|
||||
class _ConversationTimeoutContext(Generic[CCT]):
|
||||
"""Used as a datastore for conversation timeouts. Passed in the
|
||||
:paramref:`JobQueue.run_once.context` parameter. See :meth:`_trigger_timeout`.
|
||||
:paramref:`JobQueue.run_once.data` parameter. See :meth:`_trigger_timeout`.
|
||||
"""
|
||||
|
||||
__slots__ = ("conversation_key", "update", "application", "callback_context")
|
||||
|
@ -667,9 +667,7 @@ class ConversationHandler(BaseHandler[Update, CCT]):
|
|||
self.timeout_jobs[conversation_key] = j_queue.run_once(
|
||||
self._trigger_timeout,
|
||||
self.conversation_timeout, # type: ignore[arg-type]
|
||||
context=_ConversationTimeoutContext(
|
||||
conversation_key, update, application, context
|
||||
),
|
||||
data=_ConversationTimeoutContext(conversation_key, update, application, context),
|
||||
)
|
||||
except Exception as exc:
|
||||
_logger.exception("Failed to schedule timeout.", exc_info=exc)
|
||||
|
@ -885,7 +883,7 @@ class ConversationHandler(BaseHandler[Update, CCT]):
|
|||
:obj:`True` is handled.
|
||||
"""
|
||||
job = cast("Job", context.job)
|
||||
ctxt = cast(_ConversationTimeoutContext, job.context)
|
||||
ctxt = cast(_ConversationTimeoutContext, job.data)
|
||||
|
||||
_logger.debug(
|
||||
"Conversation timeout was triggered for conversation %s!", ctxt.conversation_key
|
||||
|
|
|
@ -121,7 +121,7 @@ class JobQueue:
|
|||
self,
|
||||
callback: JobCallback,
|
||||
when: Union[float, datetime.timedelta, datetime.datetime, datetime.time],
|
||||
context: object = None,
|
||||
data: object = None,
|
||||
name: str = None,
|
||||
chat_id: int = None,
|
||||
user_id: int = None,
|
||||
|
@ -163,9 +163,12 @@ class JobQueue:
|
|||
be available in the callback.
|
||||
|
||||
.. versionadded:: 20.0
|
||||
context (:obj:`object`, optional): Additional data needed for the callback function.
|
||||
Can be accessed through :attr:`Job.context` in the callback. Defaults to
|
||||
data (:obj:`object`, optional): Additional data needed for the callback function.
|
||||
Can be accessed through :attr:`Job.data` in the callback. Defaults to
|
||||
:obj:`None`.
|
||||
|
||||
.. versionchanged:: 20.0
|
||||
Renamed the parameter ``context`` to :paramref:`data`.
|
||||
name (:obj:`str`, optional): The name of the new job. Defaults to
|
||||
:external:attr:`callback.__name__ <definition.__name__>`.
|
||||
job_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to pass to the
|
||||
|
@ -180,7 +183,7 @@ class JobQueue:
|
|||
job_kwargs = {}
|
||||
|
||||
name = name or callback.__name__
|
||||
job = Job(callback=callback, context=context, name=name, chat_id=chat_id, user_id=user_id)
|
||||
job = Job(callback=callback, data=data, name=name, chat_id=chat_id, user_id=user_id)
|
||||
date_time = self._parse_time_input(when, shift_day=True)
|
||||
|
||||
j = self.scheduler.add_job(
|
||||
|
@ -202,7 +205,7 @@ class JobQueue:
|
|||
interval: Union[float, datetime.timedelta],
|
||||
first: Union[float, datetime.timedelta, datetime.datetime, datetime.time] = None,
|
||||
last: Union[float, datetime.timedelta, datetime.datetime, datetime.time] = None,
|
||||
context: object = None,
|
||||
data: object = None,
|
||||
name: str = None,
|
||||
chat_id: int = None,
|
||||
user_id: int = None,
|
||||
|
@ -254,9 +257,12 @@ class JobQueue:
|
|||
assumed.
|
||||
|
||||
Defaults to :obj:`None`.
|
||||
context (:obj:`object`, optional): Additional data needed for the callback function.
|
||||
Can be accessed through :attr:`Job.context` in the callback. Defaults to
|
||||
data (:obj:`object`, optional): Additional data needed for the callback function.
|
||||
Can be accessed through :attr:`Job.data` in the callback. Defaults to
|
||||
:obj:`None`.
|
||||
|
||||
.. versionchanged:: 20.0
|
||||
Renamed the parameter ``context`` to :paramref:`data`.
|
||||
name (:obj:`str`, optional): The name of the new job. Defaults to
|
||||
:external:attr:`callback.__name__ <definition.__name__>`.
|
||||
chat_id (:obj:`int`, optional): Chat id of the chat associated with this job. If
|
||||
|
@ -282,7 +288,7 @@ class JobQueue:
|
|||
job_kwargs = {}
|
||||
|
||||
name = name or callback.__name__
|
||||
job = Job(callback=callback, context=context, name=name, chat_id=chat_id, user_id=user_id)
|
||||
job = Job(callback=callback, data=data, name=name, chat_id=chat_id, user_id=user_id)
|
||||
|
||||
dt_first = self._parse_time_input(first)
|
||||
dt_last = self._parse_time_input(last)
|
||||
|
@ -312,7 +318,7 @@ class JobQueue:
|
|||
callback: JobCallback,
|
||||
when: datetime.time,
|
||||
day: int,
|
||||
context: object = None,
|
||||
data: object = None,
|
||||
name: str = None,
|
||||
chat_id: int = None,
|
||||
user_id: int = None,
|
||||
|
@ -336,9 +342,12 @@ class JobQueue:
|
|||
be within the range of 1 and 31, inclusive. If a month has fewer days than this
|
||||
number, the job will not run in this month. Passing -1 leads to the job running on
|
||||
the last day of the month.
|
||||
context (:obj:`object`, optional): Additional data needed for the callback function.
|
||||
Can be accessed through :attr:`Job.context` in the callback. Defaults to
|
||||
data (:obj:`object`, optional): Additional data needed for the callback function.
|
||||
Can be accessed through :attr:`Job.data` in the callback. Defaults to
|
||||
:obj:`None`.
|
||||
|
||||
.. versionchanged:: 20.0
|
||||
Renamed the parameter ``context`` to :paramref:`data`.
|
||||
name (:obj:`str`, optional): The name of the new job. Defaults to
|
||||
:external:attr:`callback.__name__ <definition.__name__>`.
|
||||
chat_id (:obj:`int`, optional): Chat id of the chat associated with this job. If
|
||||
|
@ -364,7 +373,7 @@ class JobQueue:
|
|||
job_kwargs = {}
|
||||
|
||||
name = name or callback.__name__
|
||||
job = Job(callback=callback, context=context, name=name, chat_id=chat_id, user_id=user_id)
|
||||
job = Job(callback=callback, data=data, name=name, chat_id=chat_id, user_id=user_id)
|
||||
|
||||
j = self.scheduler.add_job(
|
||||
job.run,
|
||||
|
@ -386,7 +395,7 @@ class JobQueue:
|
|||
callback: JobCallback,
|
||||
time: datetime.time,
|
||||
days: Tuple[int, ...] = tuple(range(7)),
|
||||
context: object = None,
|
||||
data: object = None,
|
||||
name: str = None,
|
||||
chat_id: int = None,
|
||||
user_id: int = None,
|
||||
|
@ -411,9 +420,12 @@ class JobQueue:
|
|||
be used.
|
||||
days (Tuple[:obj:`int`], optional): Defines on which days of the week the job should
|
||||
run (where ``0-6`` correspond to monday - sunday). Defaults to ``EVERY_DAY``
|
||||
context (:obj:`object`, optional): Additional data needed for the callback function.
|
||||
Can be accessed through :attr:`Job.context` in the callback. Defaults to
|
||||
data (:obj:`object`, optional): Additional data needed for the callback function.
|
||||
Can be accessed through :attr:`Job.data` in the callback. Defaults to
|
||||
:obj:`None`.
|
||||
|
||||
.. versionchanged:: 20.0
|
||||
Renamed the parameter ``context`` to :paramref:`data`.
|
||||
name (:obj:`str`, optional): The name of the new job. Defaults to
|
||||
:external:attr:`callback.__name__ <definition.__name__>`.
|
||||
chat_id (:obj:`int`, optional): Chat id of the chat associated with this job. If
|
||||
|
@ -439,7 +451,7 @@ class JobQueue:
|
|||
job_kwargs = {}
|
||||
|
||||
name = name or callback.__name__
|
||||
job = Job(callback=callback, context=context, name=name, chat_id=chat_id, user_id=user_id)
|
||||
job = Job(callback=callback, data=data, name=name, chat_id=chat_id, user_id=user_id)
|
||||
|
||||
j = self.scheduler.add_job(
|
||||
job.run,
|
||||
|
@ -461,7 +473,7 @@ class JobQueue:
|
|||
self,
|
||||
callback: JobCallback,
|
||||
job_kwargs: JSONDict,
|
||||
context: object = None,
|
||||
data: object = None,
|
||||
name: str = None,
|
||||
chat_id: int = None,
|
||||
user_id: int = None,
|
||||
|
@ -476,9 +488,12 @@ class JobQueue:
|
|||
|
||||
job_kwargs (:obj:`dict`): Arbitrary keyword arguments. Used as arguments for
|
||||
:meth:`apscheduler.schedulers.base.BaseScheduler.add_job`.
|
||||
context (:obj:`object`, optional): Additional data needed for the callback function.
|
||||
Can be accessed through :attr:`Job.context` in the callback. Defaults to
|
||||
data (:obj:`object`, optional): Additional data needed for the callback function.
|
||||
Can be accessed through :attr:`Job.data` in the callback. Defaults to
|
||||
:obj:`None`.
|
||||
|
||||
.. versionchanged:: 20.0
|
||||
Renamed the parameter ``context`` to :paramref:`data`.
|
||||
name (:obj:`str`, optional): The name of the new job. Defaults to
|
||||
:external:attr:`callback.__name__ <definition.__name__>`.
|
||||
chat_id (:obj:`int`, optional): Chat id of the chat associated with this job. If
|
||||
|
@ -499,7 +514,7 @@ class JobQueue:
|
|||
|
||||
"""
|
||||
name = name or callback.__name__
|
||||
job = Job(callback=callback, context=context, name=name, chat_id=chat_id, user_id=user_id)
|
||||
job = Job(callback=callback, data=data, name=name, chat_id=chat_id, user_id=user_id)
|
||||
|
||||
j = self.scheduler.add_job(job.run, args=(self.application,), name=name, **job_kwargs)
|
||||
|
||||
|
@ -564,7 +579,8 @@ class Job:
|
|||
this :class:`telegram.ext.Job` to be useful.
|
||||
|
||||
.. versionchanged:: 20.0
|
||||
Removed argument and attribute ``job_queue``.
|
||||
* Removed argument and attribute ``job_queue``.
|
||||
* Renamed ``Job.context`` to :attr:`Job.data`.
|
||||
|
||||
Args:
|
||||
callback (:term:`coroutine function`): The callback function that should be executed by the
|
||||
|
@ -572,8 +588,8 @@ class Job:
|
|||
|
||||
async def callback(context: CallbackContext)
|
||||
|
||||
context (:obj:`object`, optional): Additional data needed for the callback function. Can be
|
||||
accessed through :attr:`Job.context` in the callback. Defaults to :obj:`None`.
|
||||
data (:obj:`object`, optional): Additional data needed for the callback function. Can be
|
||||
accessed through :attr:`Job.data` in the callback. Defaults to :obj:`None`.
|
||||
name (:obj:`str`, optional): The name of the new job. Defaults to
|
||||
:external:obj:`callback.__name__ <definition.__name__>`.
|
||||
job (:class:`apscheduler.job.Job`, optional): The APS Job this job is a wrapper for.
|
||||
|
@ -587,7 +603,7 @@ class Job:
|
|||
Attributes:
|
||||
callback (:term:`coroutine function`): The callback function that should be executed by the
|
||||
new job.
|
||||
context (:obj:`object`): Optional. Additional data needed for the callback function.
|
||||
data (:obj:`object`): Optional. Additional data needed for the callback function.
|
||||
name (:obj:`str`): Optional. The name of the new job.
|
||||
job (:class:`apscheduler.job.Job`): Optional. The APS Job this job is a wrapper for.
|
||||
chat_id (:obj:`int`): Optional. Chat id of the chat that this job is associated with.
|
||||
|
@ -600,7 +616,7 @@ class Job:
|
|||
|
||||
__slots__ = (
|
||||
"callback",
|
||||
"context",
|
||||
"data",
|
||||
"name",
|
||||
"_removed",
|
||||
"_enabled",
|
||||
|
@ -612,7 +628,7 @@ class Job:
|
|||
def __init__(
|
||||
self,
|
||||
callback: JobCallback,
|
||||
context: object = None,
|
||||
data: object = None,
|
||||
name: str = None,
|
||||
job: APSJob = None,
|
||||
chat_id: int = None,
|
||||
|
@ -620,7 +636,7 @@ class Job:
|
|||
):
|
||||
|
||||
self.callback = callback
|
||||
self.context = context
|
||||
self.data = data
|
||||
self.name = name or callback.__name__
|
||||
self.chat_id = chat_id
|
||||
self.user_id = user_id
|
||||
|
|
|
@ -65,7 +65,7 @@ class TestJobQueue:
|
|||
isinstance(context, CallbackContext)
|
||||
and isinstance(context.job, Job)
|
||||
and isinstance(context.update_queue, asyncio.Queue)
|
||||
and context.job.context is None
|
||||
and context.job.data is None
|
||||
and context.chat_data is None
|
||||
and context.user_data is None
|
||||
and isinstance(context.bot_data, dict)
|
||||
|
@ -79,8 +79,8 @@ class TestJobQueue:
|
|||
self.result += 1
|
||||
context.job.schedule_removal()
|
||||
|
||||
async def job_run_once_with_context(self, context):
|
||||
self.result += context.job.context
|
||||
async def job_run_once_with_data(self, context):
|
||||
self.result += context.job.data
|
||||
|
||||
async def job_datetime_tests(self, context):
|
||||
self.job_time = time.time()
|
||||
|
@ -121,8 +121,8 @@ class TestJobQueue:
|
|||
await asyncio.sleep(0.1)
|
||||
assert self.result == 1
|
||||
|
||||
async def test_job_with_context(self, job_queue):
|
||||
job_queue.run_once(self.job_run_once_with_context, 0.1, context=5)
|
||||
async def test_job_with_data(self, job_queue):
|
||||
job_queue.run_once(self.job_run_once_with_data, 0.1, data=5)
|
||||
await asyncio.sleep(0.2)
|
||||
assert self.result == 5
|
||||
|
||||
|
|
Loading…
Reference in a new issue