Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/dependency_injector/providers.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from contextlib import AbstractContextManager, AbstractAsyncContextManager
from pathlib import Path
from typing import (
Awaitable,
Expand Down Expand Up @@ -465,6 +466,20 @@ class Resource(Provider[T]):
**kwargs: Injection,
) -> None: ...
@overload
def __init__(
self,
provides: Optional[_Callable[..., AbstractContextManager[T]]] = None,
*args: Injection,
**kwargs: Injection,
) -> None: ...
@overload
def __init__(
self,
provides: Optional[_Callable[..., AbstractAsyncContextManager[T]]] = None,
*args: Injection,
**kwargs: Injection,
) -> None: ...
@overload
def __init__(
self,
provides: Optional[_Callable[..., _Iterator[T]]] = None,
Expand Down
61 changes: 61 additions & 0 deletions tests/typing/resource.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import contextmanager, asynccontextmanager
from typing import (
Any,
AsyncGenerator,
Expand All @@ -7,6 +8,7 @@
Iterator,
List,
Optional,
Self,
)

from dependency_injector import providers, resources
Expand Down Expand Up @@ -109,3 +111,62 @@ async def _provide8() -> None:
# Test 9: to check string imports
provider9: providers.Resource[Dict[Any, Any]] = providers.Resource("builtins.dict")
provider9.set_provides("builtins.dict")


# Test 10: to check the return type with classes implementing AbstractContextManager protocol
Copy link
Author

@leonarduschen leonarduschen Oct 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test 10 and 12 (classes implementing __(a)enter__/__(a)exit__) were actually already passing because of the first overload Callable[..., T], but I added these tests anyway because they were not covered yet.

class MyResource10:
def __init__(self) -> None:
pass

def __enter__(self) -> Self:
return self

def __exit__(self, *args: Any, **kwargs: Any) -> None:
return None


provider10 = providers.Resource(MyResource10)
var10: MyResource10 = provider10()


# Test 11: to check the return type with functions decorated with contextlib.contextmanager
@contextmanager
def init11() -> Iterator[int]:
yield 1


provider11 = providers.Resource(init11)
var11: int = provider11()


# Test 12: to check the return type with classes implementing AbstractAsyncContextManager protocol
class MyResource12:
def __init__(self) -> None:
pass

async def __aenter__(self) -> Self:
return self

async def __aexit__(self, *args: Any, **kwargs: Any) -> None:
return None


provider12 = providers.Resource(MyResource12)


async def _provide12() -> None:
var1: MyResource12 = await provider12() # type: ignore
var2: MyResource12 = await provider12.async_()


# Test 13: to check the return type with functions decorated with contextlib.asynccontextmanager
@asynccontextmanager
async def init13() -> AsyncIterator[int]:
yield 1


provider13 = providers.Resource(init13)

async def _provide13() -> None:
var1: int = await provider13() # type: ignore
var2: int = await provider13.async_()