Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: explain DI container teardown in pytest fixtures #130

Merged
merged 6 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 3 additions & 2 deletions docs/introduction/application-settings.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Application settings

For example, you have application settings in `pydantic_settings`

```python
import pydantic_settings

Expand All @@ -18,8 +20,7 @@ from that_depends import BaseContainer, providers

class DIContainer(BaseContainer):
settings = providers.Singleton(Settings)
settings_casted: Settings = settings.cast
some_factory = providers.Factory(SomeFactory, service_name=settings_casted.service_name)
some_factory = providers.Factory(SomeFactory, service_name=settings.cast.service_name)
```

And when `some_factory` is resolved it will receive `service_name` attribute from `Settings`
6 changes: 5 additions & 1 deletion docs/providers/resources.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Resource

lesnik512 marked this conversation as resolved.
Show resolved Hide resolved
- resolve the dependency only once and cache the resolved instance for future injections;
- unlike `Singleton` has finalization logic;
- generator or async generator can be used;
- context manager derived from `typing.ContextManager` or `typing.AsyncContextManager` can be used;

# How it works
## How it works

```python
import typing

Expand Down Expand Up @@ -33,7 +35,9 @@ class MyContainer(BaseContainer):
```

## Concurrency safety

`Resource` is safe to use in threading and asyncio concurrency:

```python
# calling async_resolve concurrently in different coroutines will create only one instance
await MyContainer.async_resource.async_resolve()
Expand Down
21 changes: 21 additions & 0 deletions docs/testing/fixture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Fixture

## Dependencies teardown

When using dependency injection in tests, it's important to properly tear down resources after tests complete.

Without proper teardown, the Python event loop might close before resources have a chance to shut down properly, leading to errors like `RuntimeError: Event loop is closed`.

You can set up automatic teardown using a pytest fixture:

```python
import pytest_asyncio
from typing import AsyncGenerator

from my_project import DIContainer

@pytest_asyncio.fixture(autouse=True)
async def di_container_teardown() -> AsyncGenerator[None]:
yield
await DIContainer.tear_down()
lesnik512 marked this conversation as resolved.
Show resolved Hide resolved
```