GeezerVison is showing github.com — Rename `Job.context` to `Job.data` (#3028) · MSutt/python-telegram-bot@1f7efe4
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: examples/timerbot.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -43,7 +43,7 @@ async def start(update: Update, context: CallbackContext.DEFAULT_TYPE) -> None: | 43 | 43 | async def alarm(context: CallbackContext.DEFAULT_TYPE) -> None: | 44 | 44 | """Send the alarm message.""" | 45 | 45 | 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!") | 47 | 47 |
| 48 | 48 |
| 49 | 49 | 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 | 67 | 67 | return | 68 | 68 |
| 69 | 69 | 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) | 71 | 71 |
| 72 | 72 | text = "Timer successfully set!" | 73 | 73 | if job_removed: | |
Collapse file Copy file name to clipboardExpand all lines: telegram/ext/_conversationhandler.py +3-5Lines changed: 3 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 64 | 64 | @dataclass | 65 | 65 | class _ConversationTimeoutContext(Generic[CCT]): | 66 | 66 | """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`. | 68 | 68 | """ | 69 | 69 |
| 70 | 70 | __slots__ = ("conversation_key", "update", "application", "callback_context") | @@ -667,9 +667,7 @@ def _schedule_job( | 667 | 667 | self.timeout_jobs[conversation_key] = j_queue.run_once( | 668 | 668 | self._trigger_timeout, | 669 | 669 | 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), | 673 | 671 | ) | 674 | 672 | except Exception as exc: | 675 | 673 | _logger.exception("Failed to schedule timeout.", exc_info=exc) | @@ -885,7 +883,7 @@ async def _trigger_timeout(self, context: CallbackContext) -> None: | 885 | 883 | :obj:`True` is handled. | 886 | 884 | """ | 887 | 885 | job = cast("Job", context.job) | 888 | | - ctxt = cast(_ConversationTimeoutContext, job.context) | | 886 | + ctxt = cast(_ConversationTimeoutContext, job.data) | 889 | 887 |
| 890 | 888 | _logger.debug( | 891 | 889 | "Conversation timeout was triggered for conversation %s!", ctxt.conversation_key | |
Collapse file Copy file name to clipboardExpand all lines: telegram/ext/_jobqueue.py +43-27Lines changed: 43 additions & 27 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -121,7 +121,7 @@ def run_once( | 121 | 121 | self, | 122 | 122 | callback: JobCallback, | 123 | 123 | when: Union[float, datetime.timedelta, datetime.datetime, datetime.time], | 124 | | - context: object = None, | | 124 | + data: object = None, | 125 | 125 | name: str = None, | 126 | 126 | chat_id: int = None, | 127 | 127 | user_id: int = None, | @@ -163,9 +163,12 @@ async def callback(context: CallbackContext) | 163 | 163 | be available in the callback. | 164 | 164 |
| 165 | 165 | .. 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 | 168 | 168 | :obj:`None`. | | 169 | +
| | 170 | + .. versionchanged:: 20.0 | | 171 | + Renamed the parameter ``context`` to :paramref:`data`. | 169 | 172 | name (:obj:`str`, optional): The name of the new job. Defaults to | 170 | 173 | :external:attr:`callback.__name__ <definition.__name__>`. | 171 | 174 | job_kwargs (:obj:`dict`, optional): Arbitrary keyword arguments to pass to the | @@ -180,7 +183,7 @@ async def callback(context: CallbackContext) | 180 | 183 | job_kwargs = {} | 181 | 184 |
| 182 | 185 | 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) | 184 | 187 | date_time = self._parse_time_input(when, shift_day=True) | 185 | 188 |
| 186 | 189 | j = self.scheduler.add_job( | @@ -202,7 +205,7 @@ def run_repeating( | 202 | 205 | interval: Union[float, datetime.timedelta], | 203 | 206 | first: Union[float, datetime.timedelta, datetime.datetime, datetime.time] = None, | 204 | 207 | last: Union[float, datetime.timedelta, datetime.datetime, datetime.time] = None, | 205 | | - context: object = None, | | 208 | + data: object = None, | 206 | 209 | name: str = None, | 207 | 210 | chat_id: int = None, | 208 | 211 | user_id: int = None, | @@ -254,9 +257,12 @@ async def callback(context: CallbackContext) | 254 | 257 | assumed. | 255 | 258 |
| 256 | 259 | 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 | 259 | 262 | :obj:`None`. | | 263 | +
| | 264 | + .. versionchanged:: 20.0 | | 265 | + Renamed the parameter ``context`` to :paramref:`data`. | 260 | 266 | name (:obj:`str`, optional): The name of the new job. Defaults to | 261 | 267 | :external:attr:`callback.__name__ <definition.__name__>`. | 262 | 268 | chat_id (:obj:`int`, optional): Chat id of the chat associated with this job. If | @@ -282,7 +288,7 @@ async def callback(context: CallbackContext) | 282 | 288 | job_kwargs = {} | 283 | 289 |
| 284 | 290 | 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) | 286 | 292 |
| 287 | 293 | dt_first = self._parse_time_input(first) | 288 | 294 | dt_last = self._parse_time_input(last) | @@ -312,7 +318,7 @@ def run_monthly( | 312 | 318 | callback: JobCallback, | 313 | 319 | when: datetime.time, | 314 | 320 | day: int, | 315 | | - context: object = None, | | 321 | + data: object = None, | 316 | 322 | name: str = None, | 317 | 323 | chat_id: int = None, | 318 | 324 | user_id: int = None, | @@ -336,9 +342,12 @@ async def callback(context: CallbackContext) | 336 | 342 | be within the range of 1 and 31, inclusive. If a month has fewer days than this | 337 | 343 | number, the job will not run in this month. Passing -1 leads to the job running on | 338 | 344 | 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 | 341 | 347 | :obj:`None`. | | 348 | +
| | 349 | + .. versionchanged:: 20.0 | | 350 | + Renamed the parameter ``context`` to :paramref:`data`. | 342 | 351 | name (:obj:`str`, optional): The name of the new job. Defaults to | 343 | 352 | :external:attr:`callback.__name__ <definition.__name__>`. | 344 | 353 | chat_id (:obj:`int`, optional): Chat id of the chat associated with this job. If | @@ -364,7 +373,7 @@ async def callback(context: CallbackContext) | 364 | 373 | job_kwargs = {} | 365 | 374 |
| 366 | 375 | 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) | 368 | 377 |
| 369 | 378 | j = self.scheduler.add_job( | 370 | 379 | job.run, | @@ -386,7 +395,7 @@ def run_daily( | 386 | 395 | callback: JobCallback, | 387 | 396 | time: datetime.time, | 388 | 397 | days: Tuple[int, ...] = tuple(range(7)), | 389 | | - context: object = None, | | 398 | + data: object = None, | 390 | 399 | name: str = None, | 391 | 400 | chat_id: int = None, | 392 | 401 | user_id: int = None, | @@ -411,9 +420,12 @@ async def callback(context: CallbackContext) | 411 | 420 | be used. | 412 | 421 | days (Tuple[:obj:`int`], optional): Defines on which days of the week the job should | 413 | 422 | 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 | 416 | 425 | :obj:`None`. | | 426 | +
| | 427 | + .. versionchanged:: 20.0 | | 428 | + Renamed the parameter ``context`` to :paramref:`data`. | 417 | 429 | name (:obj:`str`, optional): The name of the new job. Defaults to | 418 | 430 | :external:attr:`callback.__name__ <definition.__name__>`. | 419 | 431 | chat_id (:obj:`int`, optional): Chat id of the chat associated with this job. If | @@ -439,7 +451,7 @@ async def callback(context: CallbackContext) | 439 | 451 | job_kwargs = {} | 440 | 452 |
| 441 | 453 | 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) | 443 | 455 |
| 444 | 456 | j = self.scheduler.add_job( | 445 | 457 | job.run, | @@ -461,7 +473,7 @@ def run_custom( | 461 | 473 | self, | 462 | 474 | callback: JobCallback, | 463 | 475 | job_kwargs: JSONDict, | 464 | | - context: object = None, | | 476 | + data: object = None, | 465 | 477 | name: str = None, | 466 | 478 | chat_id: int = None, | 467 | 479 | user_id: int = None, | @@ -476,9 +488,12 @@ async def callback(context: CallbackContext) | 476 | 488 |
| 477 | 489 | job_kwargs (:obj:`dict`): Arbitrary keyword arguments. Used as arguments for | 478 | 490 | :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 | 481 | 493 | :obj:`None`. | | 494 | +
| | 495 | + .. versionchanged:: 20.0 | | 496 | + Renamed the parameter ``context`` to :paramref:`data`. | 482 | 497 | name (:obj:`str`, optional): The name of the new job. Defaults to | 483 | 498 | :external:attr:`callback.__name__ <definition.__name__>`. | 484 | 499 | chat_id (:obj:`int`, optional): Chat id of the chat associated with this job. If | @@ -499,7 +514,7 @@ async def callback(context: CallbackContext) | 499 | 514 |
| 500 | 515 | """ | 501 | 516 | 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) | 503 | 518 |
| 504 | 519 | j = self.scheduler.add_job(job.run, args=(self.application,), name=name, **job_kwargs) | 505 | 520 |
| @@ -564,16 +579,17 @@ class Job: | 564 | 579 | this :class:`telegram.ext.Job` to be useful. | 565 | 580 |
| 566 | 581 | .. 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`. | 568 | 584 |
| 569 | 585 | Args: | 570 | 586 | callback (:term:`coroutine function`): The callback function that should be executed by the | 571 | 587 | new job. Callback signature:: | 572 | 588 |
| 573 | 589 | async def callback(context: CallbackContext) | 574 | 590 |
| 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`. | 577 | 593 | name (:obj:`str`, optional): The name of the new job. Defaults to | 578 | 594 | :external:obj:`callback.__name__ <definition.__name__>`. | 579 | 595 | job (:class:`apscheduler.job.Job`, optional): The APS Job this job is a wrapper for. | @@ -587,7 +603,7 @@ async def callback(context: CallbackContext) | 587 | 603 | Attributes: | 588 | 604 | callback (:term:`coroutine function`): The callback function that should be executed by the | 589 | 605 | 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. | 591 | 607 | name (:obj:`str`): Optional. The name of the new job. | 592 | 608 | job (:class:`apscheduler.job.Job`): Optional. The APS Job this job is a wrapper for. | 593 | 609 | 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) | 600 | 616 |
| 601 | 617 | __slots__ = ( | 602 | 618 | "callback", | 603 | | - "context", | | 619 | + "data", | 604 | 620 | "name", | 605 | 621 | "_removed", | 606 | 622 | "_enabled", | @@ -612,15 +628,15 @@ async def callback(context: CallbackContext) | 612 | 628 | def __init__( | 613 | 629 | self, | 614 | 630 | callback: JobCallback, | 615 | | - context: object = None, | | 631 | + data: object = None, | 616 | 632 | name: str = None, | 617 | 633 | job: APSJob = None, | 618 | 634 | chat_id: int = None, | 619 | 635 | user_id: int = None, | 620 | 636 | ): | 621 | 637 |
| 622 | 638 | self.callback = callback | 623 | | - self.context = context | | 639 | + self.data = data | 624 | 640 | self.name = name or callback.__name__ | 625 | 641 | self.chat_id = chat_id | 626 | 642 | self.user_id = user_id | |
Collapse file Copy file name to clipboardExpand all lines: tests/test_jobqueue.py +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -65,7 +65,7 @@ async def job_run_once(self, context): | 65 | 65 | isinstance(context, CallbackContext) | 66 | 66 | and isinstance(context.job, Job) | 67 | 67 | and isinstance(context.update_queue, asyncio.Queue) | 68 | | - and context.job.context is None | | 68 | + and context.job.data is None | 69 | 69 | and context.chat_data is None | 70 | 70 | and context.user_data is None | 71 | 71 | and isinstance(context.bot_data, dict) | @@ -79,8 +79,8 @@ async def job_remove_self(self, context): | 79 | 79 | self.result += 1 | 80 | 80 | context.job.schedule_removal() | 81 | 81 |
| 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 | 84 | 84 |
| 85 | 85 | async def job_datetime_tests(self, context): | 86 | 86 | self.job_time = time.time() | @@ -121,8 +121,8 @@ async def test_run_once_timezone(self, job_queue, timezone): | 121 | 121 | await asyncio.sleep(0.1) | 122 | 122 | assert self.result == 1 | 123 | 123 |
| 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) | 126 | 126 | await asyncio.sleep(0.2) | 127 | 127 | assert self.result == 5 | 128 | 128 |
| |
|