From fff0d736bff494527558d451bf2d87913e03857f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Mandart?= Date: Wed, 21 Oct 2026 13:47:24 +0200 Subject: [PATCH] fix: Make stop_using ignore modules that aren't used Stopping the use of an unused module logged a removal that never happened, and raised ModuleLockError when the module was locked. Co-Authored-By: Claude Opus 5.5 (1M context) --- injection/_core/module.py | 9 +++++---- tests/_core/test_module.py | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) 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()