Skip to content

Commit

Permalink
fix(container): handle registering providers with unhashable implemen…
Browse files Browse the repository at this point in the history
…tations
  • Loading branch information
ThirVondukr committed Jan 30, 2025
1 parent 0cdb26c commit e74eb24
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
7 changes: 3 additions & 4 deletions aioinject/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@ def try_register(self, *providers: Provider[Any]) -> None:
self._register(provider)

def _register(self, provider: Provider[Any]) -> None:
existing_impls = {
existing_provider.impl
if any(
provider.impl == existing_provider.impl
for existing_provider in self.providers.get(provider.type_, [])
}
if provider.impl in existing_impls:
):
msg = (
f"Provider for type {provider.type_} with same "
f"implementation already registered"
Expand Down
13 changes: 12 additions & 1 deletion tests/container/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from collections.abc import AsyncIterator, Iterator

import pytest
from pydantic_settings import BaseSettings

from aioinject import Scoped, Singleton, providers
from aioinject import Object, Scoped, Singleton, providers
from aioinject.containers import Container
from aioinject.context import InjectionContext

Expand Down Expand Up @@ -50,6 +51,16 @@ def test_can_register_batch(container: Container) -> None:
assert container.providers == excepted


async def test_register_unhashable_implementation(
container: Container,
) -> None:
class ExampleSettings(BaseSettings):
value: list[str] = []

container.register(Object([], type_=list[int]))
container.register(Object(ExampleSettings(), type_=ExampleSettings))


def test_cant_register_multiple_providers_for_same_type(
container: Container,
) -> None:
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e74eb24

Please sign in to comment.