diff --git a/injection/_core/module.py b/injection/_core/module.py index 36a9832..f326fdc 100644 --- a/injection/_core/module.py +++ b/injection/_core/module.py @@ -604,11 +604,12 @@ def use( return self def stop_using(self, module: Module) -> Self: - event = ModuleRemoved(self, module) + if module in self.__modules: + event = ModuleRemoved(self, module) - with suppress(KeyError), self.dispatch(event): - self.__modules.pop(module) - module.remove_listener(self) + with self.dispatch(event): + self.__modules.pop(module) + module.remove_listener(self) return self diff --git a/tests/_core/test_module.py b/tests/_core/test_module.py index 777ce5f..9a92420 100644 --- a/tests/_core/test_module.py +++ b/tests/_core/test_module.py @@ -992,12 +992,27 @@ def test_stop_using_with_used_module( def test_stop_using_with_unused_module( self, + caplog: pytest.LogCaptureFixture, event_history: EventHistory, module: Module, ) -> None: + caplog.set_level(logging.DEBUG, logger="python-injection") + module.stop_using(Module()) assert event_history.events == () + assert caplog.messages == [] + + def test_stop_using_with_unused_module_and_locked_module( + self, + module: Module, + ) -> None: + module.singleton(A) + module.find_instance(A) + + module.stop_using(Module()) + + assert module.is_locked is True def test_use_temporarily_with_module(self, module: Module) -> None: other = Module()