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

#98 Increase granularity of context. #135

Merged
merged 28 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
cdde09c
Fixed some context-dependent tests
Oct 25, 2024
7ad6754
Merge with upstream.
Nov 9, 2024
c66aa9b
Fixed existing pytests.
alexanderlazarev0 Nov 9, 2024
fcdedbf
Merge remote-tracking branch 'upstream/main' into main
alexanderlazarev0 Jan 3, 2025
32477fe
Updated ContextResource.context to only be usable as a wrapper.
alexanderlazarev0 Jan 6, 2025
9720f0f
Made BaseContainerMeta thread safe.
alexanderlazarev0 Jan 6, 2025
0913b96
Added tests and enabled context for containers.
alexanderlazarev0 Jan 8, 2025
a7d0dcb
Added additional tests for container context wrappers.
alexanderlazarev0 Jan 8, 2025
511c7dc
Added tests for dependent containers.
alexanderlazarev0 Jan 8, 2025
57a642a
Added SupportsContext interface.
alexanderlazarev0 Jan 8, 2025
df5ed5f
Changed container context api to support SupportContext items.
alexanderlazarev0 Jan 8, 2025
11d24bd
Added tests for coverage.
alexanderlazarev0 Jan 8, 2025
579d04f
Fixed a method signature & removed so redundant mypy ignores.
alexanderlazarev0 Jan 9, 2025
7b3d24e
Refactored container_context api.
alexanderlazarev0 Jan 9, 2025
0f6c1a7
Reworked attr_getter tests.
alexanderlazarev0 Jan 9, 2025
1a6ff65
Merge with main.
alexanderlazarev0 Jan 9, 2025
0118071
Extended DIContextMiddleware to support new Context features.
alexanderlazarev0 Jan 9, 2025
c78878d
Added migrations guide.
alexanderlazarev0 Jan 9, 2025
08c42bf
Improved migration guide.
alexanderlazarev0 Jan 9, 2025
3db8345
Fixed argument order in middleware.
alexanderlazarev0 Jan 9, 2025
8701417
Made middleware tests actually test context resources.
alexanderlazarev0 Jan 9, 2025
935ca91
Rewrote introductions to context-resources.md
alexanderlazarev0 Jan 9, 2025
26c66b5
Wrote the global context section.
alexanderlazarev0 Jan 10, 2025
0de0362
Finished the context-resources documentation.
alexanderlazarev0 Jan 10, 2025
a195079
Minor documentation improvements.
alexanderlazarev0 Jan 10, 2025
59c77cb
Merge remote-tracking branch 'upstream/main' into main
alexanderlazarev0 Jan 10, 2025
d542116
Removed redundant comment.
alexanderlazarev0 Jan 10, 2025
bb96e23
Removed white-space at the top of files.
alexanderlazarev0 Jan 10, 2025
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
10 changes: 10 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,20 @@
testing/fixture
testing/provider-overriding

.. toctree::
:maxdepth: 1
:caption: Migration

migration/v2


.. toctree::
:maxdepth: 1
:caption: For developers

dev/main-decisions
dev/contributing



```
150 changes: 150 additions & 0 deletions docs/migration/v2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# Migrating from 1.* to 2.*

## How to Read This Guide

This guide is intended to help you migrate existing functionality from `that-depends` version `1.*` to `2.*`.
The goal is to enable you to migrate as quickly as possible while making only the minimal necessary changes to your codebase.

If you want to learn more about the new features introduced in `2.*`, please refer to the [documentation](https://that-depends.readthedocs.io/) and the [release notes](https://github.com/modern-python/that-depends/releases).

---

## Deprecated Features

1. **`BaseContainer.init_async_resources()` removed**
The method `BaseContainer.init_async_resources()` has been removed. Use `BaseContainer.init_resources()` instead.

**Example:**
If you are using containers, your setup might look like this:

```python
from that_depends import BaseContainer

class MyContainer(BaseContainer):
# Define your providers here
...
```
Replace all instances of:
```python
await MyContainer.init_async_resources()
```
With:
```python
await MyContainer.init_resources()
lesnik512 marked this conversation as resolved.
Show resolved Hide resolved
```

2. **`that_depends.providers.AsyncResource` removed**
The `AsyncResource` class has been removed. Use `providers.Resource` instead.

**Example:**
Replace all instances of:
```python
from that_depends.providers import AsyncResource
my_provider = providers.AsyncResource(some_async_function)
```
With:
```python
from that_depends.providers import Resource
my_provider = providers.Resource(some_async_function)
```

---

## Changes in the API

1. **`container_context()` now requires a keyword argument for initial Context**
Previously, a global context could be initialized by passing a dictionary to the `container_context()` context manager:

```python
my_global_context = {"some_key": "some_value"}
async with container_context(my_global_context):
assert fetch_context_item("some_key") == "some_value"
```

In `2.*`, use the `global_context` keyword argument instead:

```python
my_global_context = {"some_key": "some_value"}
async with container_context(global_context=my_global_context):
assert fetch_context_item("some_key") == "some_value"
```

2. **Context reset behavior changed in `container_context()`**
Previously, calling `container_context(my_global_context)` would:
- Set the global context to `my_global_context`, allowing values to be resolved using `fetch_context_item()`. This behavior remains the same.
- Reset the context for all `providers.ContextResource` instances globally. This behavior has changed.

In `2.*`, if you want to reset the context for all resources in addition to setting a global context, you need to use the `reset_all_containers=True` argument:

```python
async with container_context(global_context=my_global_context, reset_all_containers=True):
assert fetch_context_item("some_key") == "some_value"
```

> **Note:** `reset_all_containers=True` only reinitializes the context for `ContextResource` instances defined within containers (i.e., classes inheriting from `BaseContainer`). If you also need to reset contexts for resources defined outside containers, you must handle these explicitly. See the [ContextResource documentation](../providers/context-resources.md) for more details.

---

## Potential Issues with `container_context()`

If you have migrated the functionality as described above but still experience issues managing context resources, it might be due to improperly initializing resources when entering `container_context()`.

Here’s an example of an incompatibility with `1.*`:

```python
from that_depends import container_context

async def some_async_function():
# Enter a new context but import `MyContainer` later
async with container_context():
from some_other_module import MyContainer
# Attempt to resolve a `ContextResource` resource
my_resource = await MyContainer.my_context_resource.async_resolve() # ❌ Error!
```

To resolve such issues in `2.*`, consider the following suggestions:

1. **Pass explicit arguments to `DIContextMiddleware`**
If you are using `DIContextMiddleware` with your ASGI application, you can now pass additional arguments.

**Example with `FastAPI`:**

```python
import fastapi
from that_depends.providers import DIContextMiddleware, ContextResource
from that_depends import BaseContainer

MyContainer: BaseContainer
my_context_resource_provider: ContextResource
my_app: fastapi.FastAPI

my_app.add_middleware(DIContextMiddleware, MyContainer, my_context_resource_provider)
```

This middleware will automatically initialize the context for the provided resources when an endpoint is called.

2. **Avoid entering `container_context()` without arguments**
Pass all resources supporting context initialization (e.g., `providers.ContextResource` instances and `BaseContainer` subclasses) explicitly.

**Example:**

```python
from that_depends import container_context

MyContainer: BaseContainer
my_context_resource_provider: ContextResource

async with container_context(MyContainer, my_context_resource_provider):
# Resolve resources
my_container_instance = MyContainer.my_context_resource.sync_resolve()
my_provider_instance = my_context_resource_provider.sync_resolve()
```

Explicit initialization of container context is recommended to prevent unexpected behavior and improve performance.

---

## Further Help

If you continue to experience issues during migration, consider creating a [discussion](https://github.com/modern-python/that-depends/discussions) or opening an [issue](https://github.com/modern-python/that-depends/issues).
```
Loading
Loading