Improve Job.__getattr__ (#2832)

This commit is contained in:
Bibo-Joshi 2022-01-03 09:07:18 +01:00 committed by Hinrich Mahler
parent 5891db2f6b
commit 6382361161
2 changed files with 20 additions and 4 deletions

View file

@ -568,9 +568,13 @@ class Job:
@property @property
def next_t(self) -> Optional[datetime.datetime]: def next_t(self) -> Optional[datetime.datetime]:
""" """
:obj:`datetime.datetime`: Datetime for the next job execution. :class:`datetime.datetime`: Datetime for the next job execution.
Datetime is localized according to :attr:`datetime.datetime.tzinfo`. Datetime is localized according to :attr:`datetime.datetime.tzinfo`.
If job is removed or already ran it equals to :obj:`None`. 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 return self.job.next_run_time
@ -579,7 +583,12 @@ class Job:
return job.func return job.func
def __getattr__(self, item: str) -> object: 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: def __lt__(self, other: object) -> bool:
return False return False

View file

@ -519,3 +519,10 @@ class TestJobQueue:
job_queue.run_once(callback, 0.1) job_queue.run_once(callback, 0.1)
sleep(0.15) sleep(0.15)
assert self.result == (CustomContext, None, None, int) 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