Skip to content

Commit 5769e10

Browse files
authored
refactor: refactor all asserts into raise <exception>, close #371 (#379)
1 parent b59e716 commit 5769e10

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

arq/connections.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ class RedisSettings:
5050
@classmethod
5151
def from_dsn(cls, dsn: str) -> 'RedisSettings':
5252
conf = urlparse(dsn)
53-
assert conf.scheme in {'redis', 'rediss', 'unix'}, 'invalid DSN scheme'
53+
if conf.scheme not in {'redis', 'rediss', 'unix'}:
54+
raise RuntimeError('invalid DSN scheme')
5455
query_db = parse_qs(conf.query).get('db')
5556
if query_db:
5657
# e.g. redis://localhost:6379?db=1
@@ -138,7 +139,8 @@ async def enqueue_job(
138139
_queue_name = self.default_queue_name
139140
job_id = _job_id or uuid4().hex
140141
job_key = job_key_prefix + job_id
141-
assert not (_defer_until and _defer_by), "use either 'defer_until' or 'defer_by' or neither, not both"
142+
if _defer_until and _defer_by:
143+
raise RuntimeError("use either 'defer_until' or 'defer_by' or neither, not both")
142144

143145
defer_by_ms = to_ms(_defer_by)
144146
expires_ms = to_ms(_expires)
@@ -190,7 +192,8 @@ async def all_job_results(self) -> List[JobResult]:
190192
async def _get_job_def(self, job_id: bytes, score: int) -> JobDef:
191193
key = job_key_prefix + job_id.decode()
192194
v = await self.get(key)
193-
assert v is not None, f'job "{key}" not found'
195+
if v is None:
196+
raise RuntimeError(f'job "{key}" not found')
194197
jd = deserialize_job(v, deserializer=self.job_deserializer)
195198
jd.score = score
196199
jd.job_id = job_id.decode()
@@ -222,9 +225,8 @@ async def create_pool(
222225
"""
223226
settings: RedisSettings = RedisSettings() if settings_ is None else settings_
224227

225-
assert not (
226-
type(settings.host) is str and settings.sentinel
227-
), "str provided for 'host' but 'sentinel' is true; list of sentinels expected"
228+
if isinstance(settings.host, str) and settings.sentinel:
229+
raise RuntimeError("str provided for 'host' but 'sentinel' is true; list of sentinels expected")
228230

229231
if settings.sentinel:
230232

arq/cron.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ def _get_next_dt(dt_: datetime, options: Options) -> Optional[datetime]: # noqa
5858
next_v = getattr(dt_, field)
5959
if isinstance(v, int):
6060
mismatch = next_v != v
61-
else:
62-
assert isinstance(v, (set, list, tuple)), v
61+
elif isinstance(v, (set, list, tuple)):
6362
mismatch = next_v not in v
63+
else:
64+
raise RuntimeError(v)
6465
# print(field, v, next_v, mismatch)
6566
if mismatch:
6667
micro = max(dt_.microsecond - options.microsecond, 0)
@@ -82,7 +83,8 @@ def _get_next_dt(dt_: datetime, options: Options) -> Optional[datetime]: # noqa
8283
elif field == 'second':
8384
return dt_ + timedelta(seconds=1) - timedelta(microseconds=micro)
8485
else:
85-
assert field == 'microsecond', field
86+
if field != 'microsecond':
87+
raise RuntimeError(field)
8688
return dt_ + timedelta(microseconds=options.microsecond - dt_.microsecond)
8789
return None
8890

@@ -173,7 +175,8 @@ def cron(
173175
else:
174176
coroutine_ = coroutine
175177

176-
assert asyncio.iscoroutinefunction(coroutine_), f'{coroutine_} is not a coroutine function'
178+
if not asyncio.iscoroutinefunction(coroutine_):
179+
raise RuntimeError(f'{coroutine_} is not a coroutine function')
177180
timeout = to_seconds(timeout)
178181
keep_result = to_seconds(keep_result)
179182

arq/worker.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ def func(
8585
else:
8686
coroutine_ = coroutine
8787

88-
assert asyncio.iscoroutinefunction(coroutine_), f'{coroutine_} is not a coroutine function'
88+
if not asyncio.iscoroutinefunction(coroutine_):
89+
raise RuntimeError(f'{coroutine_} is not a coroutine function')
8990
timeout = to_seconds(timeout)
9091
keep_result = to_seconds(keep_result)
9192

@@ -226,10 +227,12 @@ def __init__(
226227
self.queue_name = queue_name
227228
self.cron_jobs: List[CronJob] = []
228229
if cron_jobs is not None:
229-
assert all(isinstance(cj, CronJob) for cj in cron_jobs), 'cron_jobs, must be instances of CronJob'
230+
if not all(isinstance(cj, CronJob) for cj in cron_jobs):
231+
raise RuntimeError('cron_jobs, must be instances of CronJob')
230232
self.cron_jobs = list(cron_jobs)
231233
self.functions.update({cj.name: cj for cj in self.cron_jobs})
232-
assert len(self.functions) > 0, 'at least one function or cron_job must be registered'
234+
if len(self.functions) == 0:
235+
raise RuntimeError('at least one function or cron_job must be registered')
233236
self.burst = burst
234237
self.on_startup = on_startup
235238
self.on_shutdown = on_shutdown

0 commit comments

Comments
 (0)