Skip to content

Commit

Permalink
Use custom exceptions rather than re-exporting them from onnx
Browse files Browse the repository at this point in the history
  • Loading branch information
cbourjau committed Oct 6, 2023
1 parent 624eaef commit c933f06
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
Change log
==========

0.9.1 (2023-xx-xx)
0.10.0 (2023-xx-xx)
------------------

**Breaking change**

- Failures during type/shape inference and model validation now use exceptions defined in ``spox`` rather than passing through exceptions from ``onnx``. This addresses an issues where the type of the exceptions raised from ``onnx`` depends on the host platform.

0.9.1 (2023-10-05)
------------------

**Bug fix**
Expand Down
8 changes: 5 additions & 3 deletions src/spox/_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import onnx.shape_inference
class InferenceError(Exception):
"""An error originating from the type or shape inference."""

InferenceError = onnx.shape_inference.InferenceError
ValidationError = onnx.checker.ValidationError

class ValidationError(Exception):
"""A failure to validate against the standard."""


class InferenceWarning(Warning):
Expand Down
12 changes: 10 additions & 2 deletions src/spox/_standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,18 @@ def infer_output_types_onnx(self) -> Dict[str, Type]:
typed_model = onnx.shape_inference.infer_shapes(
model, check_type=True, strict_mode=True, data_prop=True
)
except InferenceError as e:
raise type(e)(
except onnx.shape_inference.InferenceError as e:
raise InferenceError(
f"{str(e)} -- for {self.schema.name}: {self.signature}"
) from e
except RuntimeError as e:
# onnx to raises a less descriptive `RuntimeError`s on MacOS out of needless cruelty
if "[ShapeInferenceError]" in str(e):
raise InferenceError(
f"{str(e)} -- for {self.schema.name}: {self.signature}"
) from e
else:
raise e

# Recover the types from protobuf, ignoring empty protobuf objects for failing/unimplemented type inference.
results = {
Expand Down

0 comments on commit c933f06

Please sign in to comment.