From 29ad4cf3c029dda0b5901e68c18bdbde1fc62ac1 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Tue, 28 Dec 2021 19:28:40 +0100 Subject: [PATCH 1/2] Improve Job.__getattr__ --- telegram/ext/_jobqueue.py | 7 ++++++- tests/test_jobqueue.py | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/telegram/ext/_jobqueue.py b/telegram/ext/_jobqueue.py index 7561bb34186..713f7e1e875 100644 --- a/telegram/ext/_jobqueue.py +++ b/telegram/ext/_jobqueue.py @@ -579,7 +579,12 @@ def _from_aps_job(cls, job: APSJob) -> 'Job': return job.func def __getattr__(self, item: str) -> object: - return getattr(self.job, item) + try: + return getattr(self.job, item) + except AttributeError as exc: + raise AttributeError( + f"Neither 'telegram.ext.Job' nor 'apscheduler.job.Job' has attribute '{item}'" + ) from exc def __lt__(self, other: object) -> bool: return False diff --git a/tests/test_jobqueue.py b/tests/test_jobqueue.py index 294eb7dddb2..9a81a8eb02b 100644 --- a/tests/test_jobqueue.py +++ b/tests/test_jobqueue.py @@ -519,3 +519,10 @@ def callback(context): job_queue.run_once(callback, 0.1) sleep(0.15) assert self.result == (CustomContext, None, None, int) + + def test_attribute_error(self): + job = Job(self.job_run_once) + with pytest.raises( + AttributeError, match="nor 'apscheduler.job.Job' has attribute 'error'" + ): + job.error From 011b1eab96f648b0ebe3591d25f61a38c1ede6e4 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Mon, 3 Jan 2022 09:02:28 +0100 Subject: [PATCH 2/2] Add warning about having to be started for next_t --- telegram/ext/_jobqueue.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/telegram/ext/_jobqueue.py b/telegram/ext/_jobqueue.py index 713f7e1e875..f5587ca9d52 100644 --- a/telegram/ext/_jobqueue.py +++ b/telegram/ext/_jobqueue.py @@ -568,9 +568,13 @@ def enabled(self, status: bool) -> None: @property def next_t(self) -> Optional[datetime.datetime]: """ - :obj:`datetime.datetime`: Datetime for the next job execution. - Datetime is localized according to :attr:`datetime.datetime.tzinfo`. - If job is removed or already ran it equals to :obj:`None`. + :class:`datetime.datetime`: Datetime for the next job execution. + Datetime is localized according to :attr:`datetime.datetime.tzinfo`. + If job is removed or already ran it equals to :obj:`None`. + + Warning: + This attribute is only available, if the :class:`telegram.ext.JobQueue` this job + belongs to is already started. Otherwise APScheduler raises an :exc:`AttributeError`. """ return self.job.next_run_time