Skip to content

Conversation

leonarduschen
Copy link

@leonarduschen leonarduschen commented Oct 10, 2025

Addresses #926


Demo

# main.py

import asyncio
from collections.abc import AsyncIterator, Iterator
from contextlib import asynccontextmanager, contextmanager
from typing import reveal_type

from dependency_injector import containers, providers


@contextmanager
def _get_my_int() -> Iterator[int]:
    yield 1


@asynccontextmanager
async def _a_get_my_int() -> AsyncIterator[int]:
    yield 1


class Container(containers.DeclarativeContainer):
    get_my_int = providers.Resource(_get_my_int)
    a_get_my_int = providers.Resource(_a_get_my_int)


async def main() -> None:
    container = Container()
    my_int = container.get_my_int()
    reveal_type(my_int)

    a_my_int = await container.a_get_my_int.async_()
    reveal_type(a_my_int)


if __name__ == "__main__":
    asyncio.run(main())

Runtime:

python main.py
Runtime type is 'int'
Runtime type is 'int'

Mypy output before changing stubs

mypy main.py
main.py:29: note: Revealed type is "contextlib._GeneratorContextManager[builtins.int, None, None]"
main.py:32: note: Revealed type is "contextlib._AsyncGeneratorContextManager[builtins.int, None]"

Mypy output after changing stubs

mypy main.py
main.py:29: note: Revealed type is "builtins.int"
main.py:32: note: Revealed type is "builtins.int"

@leonarduschen leonarduschen changed the base branch from master to develop October 10, 2025 01:45
@leonarduschen leonarduschen marked this pull request as draft October 10, 2025 01:48
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.

@leonarduschen leonarduschen marked this pull request as ready for review October 10, 2025 02:03
@coveralls
Copy link

Coverage Status

coverage: 95.057%. remained the same
when pulling 4bc7e5d on leonarduschen:fix-resource-stubs
into 5acddac on ets-labs:develop.

@ZipFile ZipFile merged commit 9d3a432 into ets-labs:develop Oct 10, 2025
14 checks passed
@ZipFile
Copy link
Contributor

ZipFile commented Oct 10, 2025

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants