Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ccflow/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,10 @@ def __getitem__(self, item) -> ModelType:
else:
raise KeyError(f"No registered model found by the name '{item}' in registry '{self._debug_name}'")

def __setitem__(self, name: str, model: BaseModel) -> None:
"""Reject item assignment in favor of the registry's controlled API."""
raise TypeError("ModelRegistry does not support item assignment; use the 'add' method instead.")

def __iter__(self):
for key, model in self._models.items():
yield key
Expand Down
9 changes: 9 additions & 0 deletions ccflow/tests/test_base_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ def test_add_twice(self):
self.assertListEqual(m.get_registrations(), [(r, "foo"), (r, "bar"), (r2, "foo2"), (r2, "bar2")])
self.assertListEqual(m.get_registered_names(), ["/foo", "/bar"])

def test_setitem_directs_users_to_add(self):
registry = ModelRegistry(name="test")
model = MyTestModel(a="test", b=0.0)

with self.assertRaisesRegex(TypeError, "use the 'add' method"):
registry["foo"] = model

self.assertEqual(registry.models, {})

def test_add_two_places(self):
m = MyTestModel(a="test", b=0.0)
r1 = ModelRegistry(name="test")
Expand Down