Skip to content

Commit

Permalink
Improve errors raised by container.get
Browse files Browse the repository at this point in the history
  • Loading branch information
maldoinc committed Aug 25, 2024
1 parent 219c021 commit 7e6922b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
16 changes: 13 additions & 3 deletions test/unit/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <class 'test.fixtures.Counter'>. "
"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),
)

Expand Down
2 changes: 0 additions & 2 deletions wireup/ioc/dependency_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 7e6922b

Please sign in to comment.