Skip to content

Commit

Permalink
fix: correct test to -> test_nested_unresolved_generic
Browse files Browse the repository at this point in the history
  • Loading branch information
nrbnlulu committed Dec 18, 2024
1 parent b18fb9e commit 1fe2baf
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 37 deletions.
19 changes: 12 additions & 7 deletions aioinject/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,15 @@ def register(self, provider: Provider[Any]) -> None:


def is_generic_alias(type_: Any) -> TypeGuard[GenericAlias]:
return isinstance(type_, types.GenericAlias | t._GenericAlias) and type_ not in (int, tuple, list, dict, set)


# we currently don't support tuple, list, dict, set, type
return isinstance(type_, types.GenericAlias | t._GenericAlias) and t.get_origin(type_) not in (tuple, list, dict, set, type) # type: ignore[reportAttributeAccessIssue] # noqa: SLF001

def get_typevars(type_: Any) -> tuple[t.TypeVar, ...] | None:
if is_generic_alias(type_):
args = t.get_args(type_)
if all(isinstance(arg, t.TypeVar) for arg in args):
return args
return None
class InjectionContext(_BaseInjectionContext[ContextExtension]):
async def resolve(
self,
Expand All @@ -101,16 +107,15 @@ async def resolve(
for dependency in provider.resolve_dependencies(
self._container.type_context,
):
if type_is_generic and is_generic_alias(dependency.type_):
if type_is_generic and (args:= get_typevars(dependency.type_)):
# This is a generic type, we need to resolve the type arguments
# and pass them to the provider.
resolved_args = [
args_map[arg.__name__]
for arg in
t.get_args(dependency.type_)

args
]
resolved_type = dependency.type_.__class_getitem__(*resolved_args)
resolved_type = dependency.type_[*resolved_args]
dependencies[dependency.name] = await self.resolve(
type_=resolved_type,
)
Expand Down
45 changes: 37 additions & 8 deletions tests/features/test_generics.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from typing import Generic, TypeVar

import pytest
Expand Down Expand Up @@ -67,21 +68,49 @@ async def test_resolve_generics(
assert isinstance(instance, instanceof)



# TODO: I'm pretty sure this redundant.
class NestedGenericService(Generic[T]):
def __init__(self, service: GenericService[T]) -> None:
def __init__(self, service: T) -> None:
self.service = service

MEANING_OF_LIFE = 42
class Something:
def __init__(self) -> None:
self.a = MEANING_OF_LIFE

async def test_nested_generics() -> None:
container = Container()
container.register(Scoped(NestedGenericService[int]),
Scoped(GenericService[int]),
Object(42),
container.register(
Scoped(NestedGenericService[WithGenericDependency[Something]]),
Scoped(WithGenericDependency[Something]),
Scoped(Something),
Object(MEANING_OF_LIFE),
Object("42"))

async with container.context() as ctx:
instance = await ctx.resolve(NestedGenericService[int])
instance = await ctx.resolve(NestedGenericService[WithGenericDependency[Something]])
assert isinstance(instance, NestedGenericService)
assert isinstance(instance.service, GenericService)
assert instance.service.dependency == "42"
assert isinstance(instance.service, WithGenericDependency)
assert isinstance(instance.service.dependency, Something)
assert instance.service.dependency.a == MEANING_OF_LIFE

IS_PY_312 = sys.version_info >= (3, 12)
skip_ifnot_312 = pytest.mark.skipif(not IS_PY_312, reason="Python 3.12+ required")

class TestNestedUnresolvedGeneric(Generic[T]):
def __init__(self, service: WithGenericDependency[T]) -> None:
self.service = service


async def test_nested_unresolved_generic() -> None:
container = Container()
container.register(Scoped(TestNestedUnresolvedGeneric[int]),
Scoped(WithGenericDependency[int]),
Object(42),
Object("42"))

async with container.context() as ctx:
instance = await ctx.resolve(TestNestedUnresolvedGeneric[int])
assert isinstance(instance, TestNestedUnresolvedGeneric)
assert isinstance(instance.service, WithGenericDependency)
assert instance.service.dependency == 42
22 changes: 0 additions & 22 deletions uv.lock

This file was deleted.

0 comments on commit 1fe2baf

Please sign in to comment.