Skip to content

Commit b108254

Browse files
justinchubyCopilotCopilot
authored
Ensure model package type correctness (#477)
Ensure model package type --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: justinchuby <[email protected]>
1 parent 71f223e commit b108254

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

‎src/mobius/_model_package.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ def __repr__(self) -> str:
5555
names = ", ".join(repr(k) for k in self.data)
5656
return f"ModelPackage({{{names}}})"
5757

58+
def __setitem__(self, name: str, model: ir.Model) -> None:
59+
"""Store a component whose ONNX graph is available for downstream tooling."""
60+
if not isinstance(model, ir.Model) or model.graph is None:
61+
raise TypeError(f"ModelPackage component {name!r} must be an ir.Model with a graph")
62+
super().__setitem__(name, model)
63+
5864
# -- Persistence -------------------------------------------------------
5965

6066
def save(

‎src/mobius/_model_package_test.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import logging
99

1010
import onnx_ir as ir
11+
import pytest
1112
import torch
1213

1314
from mobius._builder import build_from_module
@@ -37,6 +38,14 @@ def test_setitem(self):
3738
pkg["new"] = m
3839
assert pkg["new"] is m
3940

41+
def test_rejects_component_without_graph(self):
42+
with pytest.raises(TypeError, match="must be an ir.Model with a graph"):
43+
ModelPackage({"invalid": object()}) # type: ignore[arg-type]
44+
45+
pkg = ModelPackage()
46+
with pytest.raises(TypeError, match="must be an ir.Model with a graph"):
47+
pkg["invalid"] = object() # type: ignore[assignment]
48+
4049
def test_delitem(self):
4150
pkg = ModelPackage({"a": _make_simple_model()})
4251
del pkg["a"]
@@ -99,6 +108,7 @@ def test_roundtrip(self, tmp_path):
99108

100109
assert len(loaded) == 1
101110
assert "model" in loaded
111+
assert pkg["model"].graph is not None
102112
assert loaded["model"].graph.num_nodes() == pkg["model"].graph.num_nodes()
103113

104114
def test_load_multiple(self, tmp_path):

0 commit comments

Comments
 (0)