GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Rename `Job.context` to `Job.data` (#3028) · MSutt/python-telegram-bot@1f7efe4

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • examples
    • timerbot.py
  • telegram/ext
    • _conversationhandler.py
    • _jobqueue.py
  • tests
    • test_jobqueue.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎examples/timerbot.py‎

Copy file name to clipboardExpand all lines: examples/timerbot.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ async def start(update: Update, context: CallbackContext.DEFAULT_TYPE) -> None:
4343
async def alarm(context: CallbackContext.DEFAULT_TYPE) -> None:
4444
"""Send the alarm message."""
4545
job = context.job
46-
await context.bot.send_message(job.chat_id, text=f"Beep! {job.context} seconds are over!")
46+
await context.bot.send_message(job.chat_id, text=f"Beep! {job.data} seconds are over!")
4747

4848

4949
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
6767
return
6868

6969
job_removed = remove_job_if_exists(str(chat_id), context)
70-
context.job_queue.run_once(alarm, due, chat_id=chat_id, name=str(chat_id), context=due)
70+
context.job_queue.run_once(alarm, due, chat_id=chat_id, name=str(chat_id), data=due)
7171

7272
text = "Timer successfully set!"
7373
if job_removed:
Collapse file

‎telegram/ext/_conversationhandler.py‎

Copy file name to clipboardExpand all lines: telegram/ext/_conversationhandler.py
+3-5Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
@dataclass
6565
class _ConversationTimeoutContext(Generic[CCT]):
6666
"""Used as a datastore for conversation timeouts. Passed in the
67-
:paramref:`JobQueue.run_once.context` parameter. See :meth:`_trigger_timeout`.
67+
:paramref:`JobQueue.run_once.data` parameter. See :meth:`_trigger_timeout`.
6868
"""
6969

7070
__slots__ = ("conversation_key", "update", "application", "callback_context")
@@ -667,9 +667,7 @@ def _schedule_job(
667667
self.timeout_jobs[conversation_key] = j_queue.run_once(
668668
self._trigger_timeout,
669669
self.conversation_timeout, # type: ignore[arg-type]
670-
context=_ConversationTimeoutContext(
671-
conversation_key, update, application, context
672-
),
670+
data=_ConversationTimeoutContext(conversation_key, update, application, context),
673671
)
674672
except Exception as exc:
675673
_logger.exception("Failed to schedule timeout.", exc_info=exc)
@@ -885,7 +883,7 @@ async def _trigger_timeout(self, context: CallbackContext) -> None:
885883
:obj:`True` is handled.
886884
"""
887885
job = cast("Job", context.job)
888-
ctxt = cast(_ConversationTimeoutContext, job.context)
886+
ctxt = cast(_ConversationTimeoutContext, job.data)
889887

890888
_logger.debug(
891889
"Conversation timeout was triggered for conversation %s!", ctxt.conversation_key
Collapse file

‎telegram/ext/_jobqueue.py‎

Copy file name to clipboardExpand all lines: telegram/ext/_jobqueue.py
+43-27Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def run_once(
121121
self,
122122
callback: JobCallback,
123123
when: Union[float, datetime.timedelta, datetime.datetime, datetime.time],
124-
context: object = None,
124+
data: object = None,
125125
name: str = None,
126126
chat_id: int = None,
127127
user_id: int = None,
@@ -163,9 +163,12 @@ async def callback(context: CallbackContext)
163163
be available in the callback.
164164
165165
.. versionadded:: 20.0
166-
context (:obj:`object`, optional): Additional data needed for the callback function.
167-
Can be accessed through :attr:`Job.context` in the callback. Defaults to
166+
data (:obj:`object`, optional): Additional data needed for the callback function.
167+
Can be accessed through :attr:`Job.data` in the callback. Defaults to
168168
:obj:`None`.
169+
170+
.. versionchanged:: 20.0
171+
Renamed the parameter ``context`` to :paramref:`data`.
169172
name (:obj:`str`, optional): The name of the new job. Defaults to
170173
:external:attr:`callback.__name__ <definition.__name__>`.
171174
job_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to pass to the
@@ -180,7 +183,7 @@ async def callback(context: CallbackContext)
180183
job_kwargs = {}
181184

182185
name = name or callback.__name__
183-
job = Job(callback=callback, context=context, name=name, chat_id=chat_id, user_id=user_id)
186+
job = Job(callback=callback, data=data, name=name, chat_id=chat_id, user_id=user_id)
184187
date_time = self._parse_time_input(when, shift_day=True)
185188

186189
j = self.scheduler.add_job(
@@ -202,7 +205,7 @@ def run_repeating(
202205
interval: Union[float, datetime.timedelta],
203206
first: Union[float, datetime.timedelta, datetime.datetime, datetime.time] = None,
204207
last: Union[float, datetime.timedelta, datetime.datetime, datetime.time] = None,
205-
context: object = None,
208+
data: object = None,
206209
name: str = None,
207210
chat_id: int = None,
208211
user_id: int = None,
@@ -254,9 +257,12 @@ async def callback(context: CallbackContext)
254257
assumed.
255258
256259
Defaults to :obj:`None`.
257-
context (:obj:`object`, optional): Additional data needed for the callback function.
258-
Can be accessed through :attr:`Job.context` in the callback. Defaults to
260+
data (:obj:`object`, optional): Additional data needed for the callback function.
261+
Can be accessed through :attr:`Job.data` in the callback. Defaults to
259262
:obj:`None`.
263+
264+
.. versionchanged:: 20.0
265+
Renamed the parameter ``context`` to :paramref:`data`.
260266
name (:obj:`str`, optional): The name of the new job. Defaults to
261267
:external:attr:`callback.__name__ <definition.__name__>`.
262268
chat_id (:obj:`int`, optional): Chat id of the chat associated with this job. If
@@ -282,7 +288,7 @@ async def callback(context: CallbackContext)
282288
job_kwargs = {}
283289

284290
name = name or callback.__name__
285-
job = Job(callback=callback, context=context, name=name, chat_id=chat_id, user_id=user_id)
291+
job = Job(callback=callback, data=data, name=name, chat_id=chat_id, user_id=user_id)
286292

287293
dt_first = self._parse_time_input(first)
288294
dt_last = self._parse_time_input(last)
@@ -312,7 +318,7 @@ def run_monthly(
312318
callback: JobCallback,
313319
when: datetime.time,
314320
day: int,
315-
context: object = None,
321+
data: object = None,
316322
name: str = None,
317323
chat_id: int = None,
318324
user_id: int = None,
@@ -336,9 +342,12 @@ async def callback(context: CallbackContext)
336342
be within the range of 1 and 31, inclusive. If a month has fewer days than this
337343
number, the job will not run in this month. Passing -1 leads to the job running on
338344
the last day of the month.
339-
context (:obj:`object`, optional): Additional data needed for the callback function.
340-
Can be accessed through :attr:`Job.context` in the callback. Defaults to
345+
data (:obj:`object`, optional): Additional data needed for the callback function.
346+
Can be accessed through :attr:`Job.data` in the callback. Defaults to
341347
:obj:`None`.
348+
349+
.. versionchanged:: 20.0
350+
Renamed the parameter ``context`` to :paramref:`data`.
342351
name (:obj:`str`, optional): The name of the new job. Defaults to
343352
:external:attr:`callback.__name__ <definition.__name__>`.
344353
chat_id (:obj:`int`, optional): Chat id of the chat associated with this job. If
@@ -364,7 +373,7 @@ async def callback(context: CallbackContext)
364373
job_kwargs = {}
365374

366375
name = name or callback.__name__
367-
job = Job(callback=callback, context=context, name=name, chat_id=chat_id, user_id=user_id)
376+
job = Job(callback=callback, data=data, name=name, chat_id=chat_id, user_id=user_id)
368377

369378
j = self.scheduler.add_job(
370379
job.run,
@@ -386,7 +395,7 @@ def run_daily(
386395
callback: JobCallback,
387396
time: datetime.time,
388397
days: Tuple[int, ...] = tuple(range(7)),
389-
context: object = None,
398+
data: object = None,
390399
name: str = None,
391400
chat_id: int = None,
392401
user_id: int = None,
@@ -411,9 +420,12 @@ async def callback(context: CallbackContext)
411420
be used.
412421
days (Tuple[:obj:`int`], optional): Defines on which days of the week the job should
413422
run (where ``0-6`` correspond to monday - sunday). Defaults to ``EVERY_DAY``
414-
context (:obj:`object`, optional): Additional data needed for the callback function.
415-
Can be accessed through :attr:`Job.context` in the callback. Defaults to
423+
data (:obj:`object`, optional): Additional data needed for the callback function.
424+
Can be accessed through :attr:`Job.data` in the callback. Defaults to
416425
:obj:`None`.
426+
427+
.. versionchanged:: 20.0
428+
Renamed the parameter ``context`` to :paramref:`data`.
417429
name (:obj:`str`, optional): The name of the new job. Defaults to
418430
:external:attr:`callback.__name__ <definition.__name__>`.
419431
chat_id (:obj:`int`, optional): Chat id of the chat associated with this job. If
@@ -439,7 +451,7 @@ async def callback(context: CallbackContext)
439451
job_kwargs = {}
440452

441453
name = name or callback.__name__
442-
job = Job(callback=callback, context=context, name=name, chat_id=chat_id, user_id=user_id)
454+
job = Job(callback=callback, data=data, name=name, chat_id=chat_id, user_id=user_id)
443455

444456
j = self.scheduler.add_job(
445457
job.run,
@@ -461,7 +473,7 @@ def run_custom(
461473
self,
462474
callback: JobCallback,
463475
job_kwargs: JSONDict,
464-
context: object = None,
476+
data: object = None,
465477
name: str = None,
466478
chat_id: int = None,
467479
user_id: int = None,
@@ -476,9 +488,12 @@ async def callback(context: CallbackContext)
476488
477489
job_kwargs (:obj:`dict`): Arbitrary keyword arguments. Used as arguments for
478490
:meth:`apscheduler.schedulers.base.BaseScheduler.add_job`.
479-
context (:obj:`object`, optional): Additional data needed for the callback function.
480-
Can be accessed through :attr:`Job.context` in the callback. Defaults to
491+
data (:obj:`object`, optional): Additional data needed for the callback function.
492+
Can be accessed through :attr:`Job.data` in the callback. Defaults to
481493
:obj:`None`.
494+
495+
.. versionchanged:: 20.0
496+
Renamed the parameter ``context`` to :paramref:`data`.
482497
name (:obj:`str`, optional): The name of the new job. Defaults to
483498
:external:attr:`callback.__name__ <definition.__name__>`.
484499
chat_id (:obj:`int`, optional): Chat id of the chat associated with this job. If
@@ -499,7 +514,7 @@ async def callback(context: CallbackContext)
499514
500515
"""
501516
name = name or callback.__name__
502-
job = Job(callback=callback, context=context, name=name, chat_id=chat_id, user_id=user_id)
517+
job = Job(callback=callback, data=data, name=name, chat_id=chat_id, user_id=user_id)
503518

504519
j = self.scheduler.add_job(job.run, args=(self.application,), name=name, **job_kwargs)
505520

@@ -564,16 +579,17 @@ class Job:
564579
this :class:`telegram.ext.Job` to be useful.
565580
566581
.. versionchanged:: 20.0
567-
Removed argument and attribute ``job_queue``.
582+
* Removed argument and attribute ``job_queue``.
583+
* Renamed ``Job.context`` to :attr:`Job.data`.
568584
569585
Args:
570586
callback (:term:`coroutine function`): The callback function that should be executed by the
571587
new job. Callback signature::
572588
573589
async def callback(context: CallbackContext)
574590
575-
context (:obj:`object`, optional): Additional data needed for the callback function. Can be
576-
accessed through :attr:`Job.context` in the callback. Defaults to :obj:`None`.
591+
data (:obj:`object`, optional): Additional data needed for the callback function. Can be
592+
accessed through :attr:`Job.data` in the callback. Defaults to :obj:`None`.
577593
name (:obj:`str`, optional): The name of the new job. Defaults to
578594
:external:obj:`callback.__name__ <definition.__name__>`.
579595
job (:class:`apscheduler.job.Job`, optional): The APS Job this job is a wrapper for.
@@ -587,7 +603,7 @@ async def callback(context: CallbackContext)
587603
Attributes:
588604
callback (:term:`coroutine function`): The callback function that should be executed by the
589605
new job.
590-
context (:obj:`object`): Optional. Additional data needed for the callback function.
606+
data (:obj:`object`): Optional. Additional data needed for the callback function.
591607
name (:obj:`str`): Optional. The name of the new job.
592608
job (:class:`apscheduler.job.Job`): Optional. The APS Job this job is a wrapper for.
593609
chat_id (:obj:`int`): Optional. Chat id of the chat that this job is associated with.
@@ -600,7 +616,7 @@ async def callback(context: CallbackContext)
600616

601617
__slots__ = (
602618
"callback",
603-
"context",
619+
"data",
604620
"name",
605621
"_removed",
606622
"_enabled",
@@ -612,15 +628,15 @@ async def callback(context: CallbackContext)
612628
def __init__(
613629
self,
614630
callback: JobCallback,
615-
context: object = None,
631+
data: object = None,
616632
name: str = None,
617633
job: APSJob = None,
618634
chat_id: int = None,
619635
user_id: int = None,
620636
):
621637

622638
self.callback = callback
623-
self.context = context
639+
self.data = data
624640
self.name = name or callback.__name__
625641
self.chat_id = chat_id
626642
self.user_id = user_id
Collapse file

‎tests/test_jobqueue.py‎

Copy file name to clipboardExpand all lines: tests/test_jobqueue.py
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ async def job_run_once(self, context):
6565
isinstance(context, CallbackContext)
6666
and isinstance(context.job, Job)
6767
and isinstance(context.update_queue, asyncio.Queue)
68-
and context.job.context is None
68+
and context.job.data is None
6969
and context.chat_data is None
7070
and context.user_data is None
7171
and isinstance(context.bot_data, dict)
@@ -79,8 +79,8 @@ async def job_remove_self(self, context):
7979
self.result += 1
8080
context.job.schedule_removal()
8181

82-
async def job_run_once_with_context(self, context):
83-
self.result += context.job.context
82+
async def job_run_once_with_data(self, context):
83+
self.result += context.job.data
8484

8585
async def job_datetime_tests(self, context):
8686
self.job_time = time.time()
@@ -121,8 +121,8 @@ async def test_run_once_timezone(self, job_queue, timezone):
121121
await asyncio.sleep(0.1)
122122
assert self.result == 1
123123

124-
async def test_job_with_context(self, job_queue):
125-
job_queue.run_once(self.job_run_once_with_context, 0.1, context=5)
124+
async def test_job_with_data(self, job_queue):
125+
job_queue.run_once(self.job_run_once_with_data, 0.1, data=5)
126126
await asyncio.sleep(0.2)
127127
assert self.result == 5
128128

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.