Skip to content

Commit

Permalink
Add more test cases for resolving generic types, fix getting `__param…
Browse files Browse the repository at this point in the history
…eters__` from class
  • Loading branch information
ThirVondukr committed Mar 20, 2024
1 parent 7dc01ec commit 446982b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion aioinject/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def _typevar_map(
if not origin:
continue

params = origin.__parameters__
params = getattr(origin, "__parameters__", ())
args = typing.get_args(base)
typevar_map |= dict(zip(params, args, strict=False))

Expand Down
22 changes: 18 additions & 4 deletions tests/features/test_generics.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Generic, TypeVar

import pytest

from aioinject import Container, Object, Scoped
from aioinject.providers import Dependency

Expand Down Expand Up @@ -43,11 +45,23 @@ async def test_generic_dependency() -> None:
)


async def test_resolve_generics() -> None:
@pytest.mark.parametrize(
("type_", "instanceof"),
[
(GenericService, GenericService),
(WithGenericDependency[int], WithGenericDependency),
(ConstrainedGenericDependency, ConstrainedGenericDependency),
],
)
async def test_resolve_generics(
type_: type[object],
instanceof: type[object],
) -> None:
container = Container()
container.register(Scoped(WithGenericDependency[int]))
container.register(Scoped(type_))
container.register(Object(42))
container.register(Object("42"))

async with container.context() as ctx:
instance = await ctx.resolve(WithGenericDependency[int])
assert isinstance(instance, WithGenericDependency)
instance = await ctx.resolve(type_)
assert isinstance(instance, instanceof)

0 comments on commit 446982b

Please sign in to comment.