diff --git a/ccflow/base.py b/ccflow/base.py index 7a831eb..e59a01a 100644 --- a/ccflow/base.py +++ b/ccflow/base.py @@ -642,6 +642,10 @@ def __contains__(self, item: object) -> bool: return isinstance(registry, ModelRegistry) and name in registry return item in self._models + 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 diff --git a/ccflow/tests/test_base_registry.py b/ccflow/tests/test_base_registry.py index c92059d..0d20b39 100644 --- a/ccflow/tests/test_base_registry.py +++ b/ccflow/tests/test_base_registry.py @@ -226,6 +226,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")