From 987673315040d3a15bb4eee9016e8085a42825c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Kr=C3=BCger=20Svensson?= Date: Thu, 15 Dec 2022 21:57:46 +0100 Subject: [PATCH] refactor: refactor all asserts into raise , close #371 --- arq/connections.py | 14 ++++++++------ arq/cron.py | 11 +++++++---- arq/worker.py | 9 ++++++--- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/arq/connections.py b/arq/connections.py index d4fc4434..56a3fd1b 100644 --- a/arq/connections.py +++ b/arq/connections.py @@ -50,7 +50,8 @@ class RedisSettings: @classmethod def from_dsn(cls, dsn: str) -> 'RedisSettings': conf = urlparse(dsn) - assert conf.scheme in {'redis', 'rediss', 'unix'}, 'invalid DSN scheme' + if conf.scheme not in {'redis', 'rediss', 'unix'}: + raise RuntimeError('invalid DSN scheme') query_db = parse_qs(conf.query).get('db') if query_db: # e.g. redis://localhost:6379?db=1 @@ -138,7 +139,8 @@ async def enqueue_job( _queue_name = self.default_queue_name job_id = _job_id or uuid4().hex job_key = job_key_prefix + job_id - assert not (_defer_until and _defer_by), "use either 'defer_until' or 'defer_by' or neither, not both" + if _defer_until and _defer_by: + raise RuntimeError("use either 'defer_until' or 'defer_by' or neither, not both") defer_by_ms = to_ms(_defer_by) expires_ms = to_ms(_expires) @@ -190,7 +192,8 @@ async def all_job_results(self) -> List[JobResult]: async def _get_job_def(self, job_id: bytes, score: int) -> JobDef: key = job_key_prefix + job_id.decode() v = await self.get(key) - assert v is not None, f'job "{key}" not found' + if v is None: + raise RuntimeError(f'job "{key}" not found') jd = deserialize_job(v, deserializer=self.job_deserializer) jd.score = score return jd @@ -221,9 +224,8 @@ async def create_pool( """ settings: RedisSettings = RedisSettings() if settings_ is None else settings_ - assert not ( - type(settings.host) is str and settings.sentinel - ), "str provided for 'host' but 'sentinel' is true; list of sentinels expected" + if isinstance(settings.host, str) and settings.sentinel: + raise RuntimeError("str provided for 'host' but 'sentinel' is true; list of sentinels expected") if settings.sentinel: diff --git a/arq/cron.py b/arq/cron.py index 2eca6c75..f62ea0bd 100644 --- a/arq/cron.py +++ b/arq/cron.py @@ -58,9 +58,10 @@ def _get_next_dt(dt_: datetime, options: Options) -> Optional[datetime]: # noqa next_v = getattr(dt_, field) if isinstance(v, int): mismatch = next_v != v - else: - assert isinstance(v, (set, list, tuple)), v + elif isinstance(v, (set, list, tuple)): mismatch = next_v not in v + else: + raise RuntimeError(v) # print(field, v, next_v, mismatch) if mismatch: micro = max(dt_.microsecond - options.microsecond, 0) @@ -82,7 +83,8 @@ def _get_next_dt(dt_: datetime, options: Options) -> Optional[datetime]: # noqa elif field == 'second': return dt_ + timedelta(seconds=1) - timedelta(microseconds=micro) else: - assert field == 'microsecond', field + if field != 'microsecond': + raise RuntimeError(field) return dt_ + timedelta(microseconds=options.microsecond - dt_.microsecond) return None @@ -173,7 +175,8 @@ def cron( else: coroutine_ = coroutine - assert asyncio.iscoroutinefunction(coroutine_), f'{coroutine_} is not a coroutine function' + if not asyncio.iscoroutinefunction(coroutine_): + raise RuntimeError(f'{coroutine_} is not a coroutine function') timeout = to_seconds(timeout) keep_result = to_seconds(keep_result) diff --git a/arq/worker.py b/arq/worker.py index 81afd5b7..643ea481 100644 --- a/arq/worker.py +++ b/arq/worker.py @@ -85,7 +85,8 @@ def func( else: coroutine_ = coroutine - assert asyncio.iscoroutinefunction(coroutine_), f'{coroutine_} is not a coroutine function' + if not asyncio.iscoroutinefunction(coroutine_): + raise RuntimeError(f'{coroutine_} is not a coroutine function') timeout = to_seconds(timeout) keep_result = to_seconds(keep_result) @@ -226,10 +227,12 @@ def __init__( self.queue_name = queue_name self.cron_jobs: List[CronJob] = [] if cron_jobs is not None: - assert all(isinstance(cj, CronJob) for cj in cron_jobs), 'cron_jobs, must be instances of CronJob' + if not all(isinstance(cj, CronJob) for cj in cron_jobs): + raise RuntimeError('cron_jobs, must be instances of CronJob') self.cron_jobs = list(cron_jobs) self.functions.update({cj.name: cj for cj in self.cron_jobs}) - assert len(self.functions) > 0, 'at least one function or cron_job must be registered' + if len(self.functions) == 0: + raise RuntimeError('at least one function or cron_job must be registered') self.burst = burst self.on_startup = on_startup self.on_shutdown = on_shutdown