Skip to content

Commit

Permalink
change di to not resolve unnamed dependency in place of named ones (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
livioribeiro committed Apr 7, 2024
1 parent 842dd1a commit 29d5263
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 34 deletions.
9 changes: 0 additions & 9 deletions src/selva/di/service/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ def get(self, name: str = None) -> ServiceSpec | None:
if service := self.providers.get(name):
return service

if default := self.providers.get(None):
message = (
f"using default service instead of '{name}'"
f" for '{default.provides.__qualname__}'"
)

warnings.warn(message)
return default

return None

def __contains__(self, name: str | None) -> bool:
Expand Down
50 changes: 25 additions & 25 deletions tests/di/test_named_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,13 @@


@service(name="1")
class DependentService:
class NamedService:
pass


@service
class ServiceWithNamedDep:
dependent: Annotated[DependentService, Inject(name="1")]


@service
class ServiceWithUnamedDep:
dependent: Annotated[DependentService, Inject]
dependent: Annotated[NamedService, Inject(name="1")]


class Interface:
Expand All @@ -38,45 +33,50 @@ class Impl2(Interface):


async def test_dependency_with_name(ioc: Container):
ioc.register(DependentService)
ioc.register(NamedService)
ioc.register(ServiceWithNamedDep)

instance = await ioc.get(ServiceWithNamedDep)
dependent = instance.dependent
assert isinstance(dependent, DependentService)
assert isinstance(dependent, NamedService)


async def test_unnamed_dependency_with_named_service_should_fail(ioc: Container):
@service
class Service:
pass

@service
class DependentService:
dependent: Annotated[Service, Inject(name="1")]

async def test_dependency_without_name_should_fail(ioc: Container):
ioc.register(NamedService)
ioc.register(DependentService)
ioc.register(ServiceWithUnamedDep)

with pytest.raises(ServiceNotFoundError):
await ioc.get(ServiceWithUnamedDep)
await ioc.get(DependentService)


async def test_default_dependency(ioc: Container):
@service
async def test_named_dependency_with_unnamed_service_should_fail(ioc: Container):
@service(name="1")
class Service:
pass

@service
class WithNamedDep:
dependent: Annotated[Service, Inject(name="1")]

ioc.register(Service)
ioc.register(WithNamedDep)
class DependentService:
dependent: Annotated[Service, Inject]

with pytest.warns(UserWarning):
instance = await ioc.get(WithNamedDep)
ioc.register(NamedService)
ioc.register(DependentService)

dependent = instance.dependent
assert isinstance(dependent, Service)
with pytest.raises(ServiceNotFoundError):
await ioc.get(DependentService)


async def test_register_two_services_with_the_same_name_should_fail(ioc: Container):
ioc.register(DependentService)
ioc.register(NamedService)
with pytest.raises(ServiceAlreadyRegisteredError):
ioc.register(DependentService)
ioc.register(NamedService)


async def test_dependencies_with_same_interface(ioc: Container):
Expand Down

0 comments on commit 29d5263

Please sign in to comment.