Skip to content

Commit 2b96979

Browse files
Replace asserts with other exceptions (#465)
1 parent c760137 commit 2b96979

File tree

10 files changed

+51
-30
lines changed

10 files changed

+51
-30
lines changed

supriya/contexts/allocators.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ def __setstate__(self, state):
8282

8383
def allocate(self, desired_block_size: int = 1) -> Optional[int]:
8484
desired_block_size = int(desired_block_size)
85-
assert 0 < desired_block_size
85+
if desired_block_size <= 0:
86+
raise ValueError("desired_block_size must be greater than zero")
8687
block_id = None
8788
with self._lock:
8889
free_block = None
@@ -125,7 +126,8 @@ def allocate_at(self, index: int, desired_block_size: int = 1) -> Optional[int]:
125126
if starting_blocks == stop_blocks:
126127
if len(starting_blocks) == 0:
127128
return None
128-
assert len(starting_blocks) == 1
129+
if len(starting_blocks) != 1:
130+
raise RuntimeError
129131
free_block = starting_blocks[0]
130132
used_block = Block(
131133
start_offset=start_offset, stop_offset=stop_offset, used=True
@@ -149,20 +151,23 @@ def free(self, block_id: int) -> None:
149151
)
150152
if len(blocks) == 0:
151153
return None
152-
assert len(blocks) == 1
154+
if len(blocks) != 1:
155+
raise RuntimeError
153156
used_block = blocks[0]
154157
self._used_heap.remove(used_block)
155158
start_offset = used_block.start_offset
156159
stopping_blocks = self._free_heap.find_intervals_stopping_at(start_offset)
157160
if stopping_blocks:
158-
assert len(stopping_blocks) == 1
161+
if len(stopping_blocks) != 1:
162+
raise RuntimeError
159163
stopping_block = stopping_blocks[0]
160164
self._free_heap.remove(stopping_block)
161165
start_offset = stopping_block.start_offset
162166
stop_offset = used_block.stop_offset
163167
starting_blocks = self._free_heap.find_intervals_starting_at(stop_offset)
164168
if starting_blocks:
165-
assert len(starting_blocks) == 1
169+
if len(starting_blocks) != 1:
170+
raise RuntimeError
166171
starting_block = starting_blocks[0]
167172
self._free_heap.remove(starting_block)
168173
stop_offset = starting_block.stop_offset

supriya/contexts/nonrealtime.py

-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ async def render(
191191
protocol = AsyncNonrealtimeProcessProtocol()
192192
await protocol.run(command, render_directory_path_)
193193
exit_code: int = await protocol.exit_future
194-
assert render_directory_path_ / render_file_name
195194
if output_file_path_:
196195
shutil.copy(
197196
render_directory_path_ / render_file_name, output_file_path_

supriya/ext/mypy.py

-4
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ def transform(self) -> bool:
5555
"supriya.ugens.core.UGenVectorInput"
5656
)
5757

58-
assert UGenRecursiveInputTypeSym.node is not None
59-
assert UGenScalarInputTypeSym.node is not None
60-
assert UGenVectorInputTypeSym.node is not None
61-
6258
UGenRecursiveInputType = getattr(UGenRecursiveInputTypeSym.node, "target")
6359
UGenScalarInputType = getattr(UGenScalarInputTypeSym.node, "target")
6460
UGenVectorInputType = getattr(UGenVectorInputTypeSym.node, "target")

supriya/ugens/core.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -4882,8 +4882,13 @@ def __init__(
48824882
special_index: SupportsInt = 0,
48834883
**kwargs: Union["UGenScalarInput", "UGenVectorInput"],
48844884
) -> None:
4885-
if self._valid_calculation_rates:
4886-
assert calculation_rate in self._valid_calculation_rates
4885+
if (
4886+
self._valid_calculation_rates
4887+
and calculation_rate not in self._valid_calculation_rates
4888+
):
4889+
raise ValueError(
4890+
f"{calculation_rate} not in {self._valid_calculation_rates}"
4891+
)
48874892
calculation_rate, kwargs = self._postprocess_kwargs(
48884893
calculation_rate=calculation_rate, **kwargs
48894894
)
@@ -5618,7 +5623,8 @@ def __str__(self) -> str:
56185623
inputs: Dict[str, str] = {}
56195624
if isinstance(ugen, Control):
56205625
for parameter in ugen.parameters:
5621-
assert parameter.name is not None
5626+
if parameter.name is None:
5627+
raise ValueError(parameter)
56225628
if len(parameter.value) == 1:
56235629
inputs[parameter.name] = str(parameter.value[0])
56245630
else:
@@ -5664,7 +5670,8 @@ def _collect_indexed_parameters(
56645670
for control in controls:
56655671
index = control.special_index
56665672
for parameter in control.parameters:
5667-
assert parameter.name is not None
5673+
if parameter.name is None:
5674+
raise ValueError(parameter)
56685675
mapping[parameter.name] = (parameter, index)
56695676
index += len(parameter)
56705677
return mapping

supriya/ugens/envelopes.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ def __init__(
3434
loop_node: Optional[int] = None,
3535
offset: Union[UGenOperable, float] = 0.0,
3636
) -> None:
37-
assert len(amplitudes) > 1
38-
assert len(durations) == (len(amplitudes) - 1)
37+
if len(amplitudes) <= 1:
38+
raise ValueError(amplitudes)
39+
if not (len(durations) == (len(amplitudes) - 1)):
40+
raise ValueError(durations, amplitudes)
3941
if isinstance(curves, (int, float, str, EnvelopeShape, UGenOperable)):
4042
curves = [curves]
4143
elif curves is None:

supriya/ugens/factories.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ class SynthDefFactory:
230230

231231
def __init__(self, channel_count=1, **kwargs):
232232
channel_count = int(channel_count)
233-
assert channel_count > 0
233+
if channel_count <= 0:
234+
raise ValueError(channel_count)
234235
self._channel_count = channel_count
235236
self._feedback_loop = None
236237
self._gate = {}
@@ -556,7 +557,8 @@ def with_channel_count(self, channel_count):
556557
source[2]: AllpassC.ar/5[0]
557558
"""
558559
channel_count = int(channel_count)
559-
assert channel_count > 0
560+
if channel_count <= 0:
561+
raise ValueError(channel_count)
560562
clone = self._clone()
561563
clone._channel_count = channel_count
562564
return clone
@@ -1585,7 +1587,8 @@ def with_output(
15851587
crossfade: BinaryOpUGen(MULTIPLICATION).kr[0]
15861588
source[0]: AllpassC.ar/1[0]
15871589
"""
1588-
assert not (replacing and crossfaded)
1590+
if replacing and crossfaded:
1591+
raise ValueError(replacing, crossfaded)
15891592
clone = self._clone()
15901593
clone._output.update(
15911594
crossfaded=bool(crossfaded),

supriya/ugens/lines.py

-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ def ar(cls, channel_count=1):
205205
from . import DC
206206

207207
channel_count = int(channel_count)
208-
assert 0 <= channel_count
209208
silence = DC.ar(source=0)
210209
if channel_count == 1:
211210
return silence

supriya/ugens/pv.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ def fft_size(self) -> UGenOperable:
2121
Returns ugen input.
2222
"""
2323
input_ = self.inputs[0]
24-
assert isinstance(input_, OutputProxy)
25-
assert isinstance(input_.ugen, PV_ChainUGen)
24+
if not isinstance(input_, OutputProxy):
25+
raise ValueError(input_)
26+
if not isinstance(input_.ugen, PV_ChainUGen):
27+
raise ValueError(input_.ugen)
2628
return input_.ugen.fft_size
2729

2830

supriya/utils/_intervals.pyx

+4-2
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,8 @@ cdef class IntervalTreeDriverEx:
644644

645645
def find_intervals_intersecting_interval(self, interval):
646646
cinterval = _CInterval.from_interval(interval)
647-
assert isinstance(cinterval, _CInterval)
647+
if not isinstance(cinterval, _CInterval):
648+
raise ValueError(cinterval)
648649
cintervals = self._recurse_find_intervals_intersecting_interval(
649650
self._root_node, cinterval)
650651
return self._unbox_cintervals(cintervals)
@@ -671,7 +672,8 @@ cdef class IntervalTreeDriverEx:
671672
return node.start_offset
672673

673674
def index(self, interval):
674-
assert self._is_interval(interval)
675+
if not self._is_interval(interval):
676+
raise ValueError(interval)
675677
cinterval = _CInterval.from_interval(interval)
676678
node = self._search(self._root_node, cinterval.start_offset)
677679
if node is None:

supriya/utils/intervals.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,8 @@ def iterate_moments_nwise(self, n=3, reverse=False):
13001300
Returns generator.
13011301
"""
13021302
n = int(n)
1303-
assert 0 < n
1303+
if n < 1:
1304+
raise ValueError(n)
13041305
if reverse:
13051306
for moment in self.iterate_moments(reverse=True):
13061307
moments = [moment]
@@ -1432,7 +1433,8 @@ def __init__(self, start_offset, stop_offset, original_interval):
14321433
self.original_interval = original_interval
14331434

14341435
def __eq__(self, cinterval):
1435-
assert isinstance(cinterval, type(self)), cinterval
1436+
if not isinstance(cinterval, type(self)):
1437+
raise ValueError(cinterval)
14361438
if self.start_offset != cinterval.start_offset:
14371439
return False
14381440
if self.stop_offset != cinterval.stop_offset:
@@ -1442,11 +1444,13 @@ def __eq__(self, cinterval):
14421444
return True
14431445

14441446
def __ne__(self, cinterval):
1445-
assert isinstance(cinterval, type(self)), cinterval
1447+
if not isinstance(cinterval, type(self)):
1448+
raise ValueError(cinterval)
14461449
return not self.__eq__(cinterval)
14471450

14481451
def __lt__(self, cinterval):
1449-
assert isinstance(cinterval, type(self)), cinterval
1452+
if not isinstance(cinterval, type(self)):
1453+
raise ValueError(cinterval)
14501454
if self.start_offset < cinterval.start_offset:
14511455
return True
14521456
if self.start_offset > cinterval.start_offset:
@@ -1730,7 +1734,8 @@ def _recurse_getitem_by_slice(self, node, start, stop):
17301734
return result
17311735

17321736
def _remove_interval(self, cinterval, old_start_offset=None):
1733-
assert isinstance(cinterval, _CInterval)
1737+
if not isinstance(cinterval, _CInterval):
1738+
raise ValueError(cinterval)
17341739
start_offset = cinterval.start_offset
17351740
if old_start_offset is not None:
17361741
start_offset = old_start_offset
@@ -1887,7 +1892,8 @@ def get_start_offset_before(self, offset):
18871892
return node.start_offset
18881893

18891894
def index(self, interval):
1890-
assert self._is_interval(interval)
1895+
if not self._is_interval(interval):
1896+
raise ValueError(interval)
18911897
cinterval = _CInterval.from_interval(interval)
18921898
node = self._search(self._root_node, cinterval.start_offset)
18931899
if node is None:

0 commit comments

Comments
 (0)