Skip to content

Commit

Permalink
fix varioius type hinting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
veriff-saulvargas committed May 19, 2024
1 parent 628869b commit 8c94a79
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 11 deletions.
9 changes: 9 additions & 0 deletions tests/test_injection.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import datetime

import pytest
Expand Down Expand Up @@ -34,3 +35,11 @@ async def inner(

with pytest.raises(RuntimeError, match="Injected arguments must not be redefined"):
await inner(_=container.SimpleFactory(dep1="1", dep2=2))


def test_type_check() -> None:
@inject
async def main() -> None:
pass

asyncio.run(main())
1 change: 1 addition & 0 deletions that_depends/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from that_depends import providers
from that_depends.container import BaseContainer
from that_depends.injection import Provide, inject

Expand Down
2 changes: 1 addition & 1 deletion that_depends/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async def _inner() -> T:
return _inner

@classmethod
async def resolve(cls, object_to_resolve: type[T] | typing.Callable[P, T]) -> T:
async def resolve(cls, object_to_resolve: type[T] | typing.Callable[..., T]) -> T:
signature = inspect.signature(object_to_resolve)
kwargs = {}
providers = cls.get_providers()
Expand Down
4 changes: 3 additions & 1 deletion that_depends/injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
T = typing.TypeVar("T")


def inject(func: typing.Callable[P, typing.Awaitable[T]]) -> typing.Callable[P, typing.Awaitable[T]]:
def inject(
func: typing.Callable[P, typing.Coroutine[typing.Any, typing.Any, T]],
) -> typing.Callable[P, typing.Coroutine[typing.Any, typing.Any, T]]:
signature = inspect.signature(func)

@functools.wraps(func)
Expand Down
10 changes: 6 additions & 4 deletions that_depends/providers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@


T = typing.TypeVar("T")
R = typing.TypeVar("R")
T_co = typing.TypeVar("T_co", covariant=True)


class AbstractProvider(typing.Generic[T], abc.ABC):
class AbstractProvider(typing.Generic[T_co], abc.ABC):
"""Abstract Provider Class."""

@abc.abstractmethod
async def async_resolve(self) -> T:
async def async_resolve(self) -> T_co:
"""Resolve dependency asynchronously."""

@abc.abstractmethod
def sync_resolve(self) -> T:
def sync_resolve(self) -> T_co:
"""Resolve dependency synchronously."""

async def __call__(self) -> T:
async def __call__(self) -> T_co:
return await self.async_resolve()

def override(self, mock: object) -> None:
Expand Down
8 changes: 4 additions & 4 deletions that_depends/providers/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
T = typing.TypeVar("T")


class List(AbstractProvider[T]):
class List(AbstractProvider[list[T]]):
def __init__(self, *providers: AbstractProvider[T]) -> None:
self._providers = providers

async def async_resolve(self) -> list[T]: # type: ignore[override]
async def async_resolve(self) -> list[T]:
return [await x.async_resolve() for x in self._providers]

def sync_resolve(self) -> list[T]: # type: ignore[override]
def sync_resolve(self) -> list[T]:
return [x.sync_resolve() for x in self._providers]

async def __call__(self) -> list[T]: # type: ignore[override]
async def __call__(self) -> list[T]:
return await self.async_resolve()
2 changes: 1 addition & 1 deletion that_depends/providers/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class Resource(AbstractResource[T]):
def __init__(
self,
creator: typing.Callable[..., typing.Iterator[T]],
creator: typing.Callable[P, typing.Iterator[T]],
*args: P.args,
**kwargs: P.kwargs,
) -> None:
Expand Down

0 comments on commit 8c94a79

Please sign in to comment.