Skip to content

Commit

Permalink
Merge pull request #11 from nrbnlulu/support-batch-register
Browse files Browse the repository at this point in the history
Support batch register
  • Loading branch information
ThirVondukr authored Apr 7, 2024
2 parents 4ca1b4e + 765c0aa commit 076e3ef
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 14 deletions.
17 changes: 10 additions & 7 deletions aioinject/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,18 @@ def _init_extensions(self, extensions: Sequence[Extension]) -> None:

def register(
self,
provider: Provider[Any],
*providers: Provider[Any],
) -> None:
if provider.type_ in self.providers:
msg = f"Provider for type {provider.type_} is already registered"
raise ValueError(msg)
for provider in providers:
if provider.type_ in self.providers:
msg = (
f"Provider for type {provider.type_} is already registered"
)
raise ValueError(msg)

self.providers[provider.type_] = provider
if class_name := getattr(provider.type_, "__name__", None):
self.type_context[class_name] = provider.type_
self.providers[provider.type_] = provider
if class_name := getattr(provider.type_, "__name__", None):
self.type_context[class_name] = provider.type_

def get_provider(self, type_: type[T]) -> Provider[T]:
try:
Expand Down
4 changes: 1 addition & 3 deletions benchmark/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,5 @@

def create_container() -> aioinject.Container:
container = aioinject.Container()
for provider in providers:
container.register(provider=provider)

container.register(*providers)
return container
3 changes: 2 additions & 1 deletion docs/code/contextmanagers/03_implicity.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from types import TracebackType
from typing import Self

from typing_extensions import Self

import aioinject
from aioinject import Scoped
Expand Down
6 changes: 4 additions & 2 deletions docs/code/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ def get(self, id: int) -> str:


container = aioinject.Container()
container.register(aioinject.Singleton(Database))
container.register(aioinject.Singleton(UserService))
container.register(
aioinject.Singleton(Database),
aioinject.Singleton(UserService),
)

with container.sync_context() as ctx:
service = ctx.resolve(UserService)
Expand Down
8 changes: 8 additions & 0 deletions tests/container/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ def test_can_register_single(container: Container) -> None:
assert container.providers == expected


def test_can_register_batch(container: Container) -> None:
provider1 = providers.Scoped(_ServiceA)
provider2 = providers.Scoped(_ServiceB)
container.register(provider1, provider2)
excepted = {_ServiceA: provider1, _ServiceB: provider2}
assert container.providers == excepted


def test_cant_register_multiple_providers_for_same_type(
container: Container,
) -> None:
Expand Down
1 change: 0 additions & 1 deletion tests/extensions/mypy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


class _TestExtension(SyncOnResolveExtension, OnResolveExtension):

async def on_resolve(
self,
context: InjectionContext,
Expand Down

0 comments on commit 076e3ef

Please sign in to comment.