From 7e6922b62599f85dba6c5fa784a712657e51bd53 Mon Sep 17 00:00:00 2001 From: Aldo Mateli Date: Sun, 25 Aug 2024 10:43:01 +0100 Subject: [PATCH] Improve errors raised by container.get --- test/unit/test_container.py | 16 +++++++++++++--- wireup/ioc/dependency_container.py | 2 -- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/test/unit/test_container.py b/test/unit/test_container.py index 8b5568b..c9c82a5 100644 --- a/test/unit/test_container.py +++ b/test/unit/test_container.py @@ -204,12 +204,22 @@ def test_raises_when_not_supplying_qualifier_registered_multiple(self): self.container.register(Counter, qualifier="foo_qualified") self.container.register(Counter, qualifier="foo_qualified2") - with self.assertRaises(UnknownServiceRequestedError) as context: + with self.assertRaises(UnknownQualifiedServiceRequestedError) as context: self.container.get(Counter) self.assertEqual( - "Cannot wire unknown class . " - "Use @Container.{register,abstract} to enable autowiring", + f"Cannot instantiate concrete class for {Counter} as qualifier 'None' is unknown. " + "Available qualifiers: ['foo_qualified', 'foo_qualified2'].", + str(context.exception), + ) + + def test_raises_on_unknown_service(self): + container = DependencyContainer(ParameterBag()) + with self.assertRaises(UnknownServiceRequestedError) as context: + container.get(Counter) + + self.assertEqual( + f"Cannot wire unknown class {Counter}. Use @Container.{{register,abstract}} to enable autowiring", str(context.exception), ) diff --git a/wireup/ioc/dependency_container.py b/wireup/ioc/dependency_container.py index c76cac0..73cffae 100644 --- a/wireup/ioc/dependency_container.py +++ b/wireup/ioc/dependency_container.py @@ -71,8 +71,6 @@ def get(self, klass: type[T], qualifier: Qualifier | None = None) -> T: if res := self._overrides.get((klass, qualifier)): return res # type: ignore[no-any-return] - self._registry.assert_dependency_exists(klass, qualifier) - if self._registry.is_interface_known(klass): klass = self._registry.interface_resolve_impl(klass, qualifier)