Skip to content

Commit 65a59e3

Browse files
gramalingamCopilot
andauthored
Replace input-constraint asserts with TranslationError in converter (#2845)
Replace 5 assert statements that check input AST constraints with TranslationError (via self._fail()) to provide meaningful error messages instead of bare AssertionErrors: - Compare expressions: require single comparison operator - Return statements: require a return value - For loop bounds: require a function call, no keyword arguments Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 46e6f69 commit 65a59e3

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

onnxscript/_internal/converter.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -930,8 +930,8 @@ def _translate_unary_op_expr(self, node):
930930

931931
def _translate_compare_expr(self, node):
932932
# TODO: handle multiple comparisons in one expression
933-
assert len(node.ops) == 1
934-
assert len(node.comparators) == 1
933+
if len(node.ops) != 1 or len(node.comparators) != 1:
934+
self._fail(node, "Multiple comparisons in one expression are not supported.")
935935
op = type(node.ops[0])
936936
if op not in primop_map:
937937
raise ValueError(self._message(node, f"Unsupported operator {op!r}."))
@@ -1120,7 +1120,8 @@ def ret(exp, i, suffix):
11201120
return return_var
11211121

11221122
val = stmt.value
1123-
assert val is not None, "Return statement without return-value not supported."
1123+
if val is None:
1124+
self._fail(stmt, "Return statement without a return value is not supported.")
11241125
if isinstance(val, ast.Tuple):
11251126
check_num_outputs(len(val.elts))
11261127
return [ret(exp, i, str(i)) for i, exp in enumerate(val.elts)]
@@ -1185,7 +1186,8 @@ def _translate_loop_stmt(self, loop_stmt: ast.For | ast.While) -> None:
11851186
python_loop_var_name = loop_stmt.target.id
11861187
# iter
11871188
iter = loop_stmt.iter
1188-
assert isinstance(iter, ast.Call), "Loop bound not a call."
1189+
if not isinstance(iter, ast.Call):
1190+
self._fail(loop_stmt, "Loop bound must be a function call (e.g. 'range(n)').")
11891191
if not isinstance(iter.func, ast.Name):
11901192
self._fail(loop_stmt, f"Unsupported loop bound {iter.func!r}.")
11911193
if iter.func.id != "range":
@@ -1194,7 +1196,8 @@ def _translate_loop_stmt(self, loop_stmt: ast.For | ast.While) -> None:
11941196
)
11951197
if not iter.args or len(iter.args) != 1:
11961198
self._fail(loop_stmt, "Unsupported loop bound, it should be 'range(?)'.")
1197-
assert not iter.keywords, "Unsupported loop bound."
1199+
if iter.keywords:
1200+
self._fail(loop_stmt, "Keyword arguments in loop bound are not supported.")
11981201
o_loop_bound = self._translate_expr(iter.args[0], "loop_bound")
11991202
onnx_cond_var = make_value(
12001203
self._generate_unique_name("cond_in"),

0 commit comments

Comments
 (0)