Skip to content

Commit 3deb7e2

Browse files
authored
docs: prose pass, fix string-injection validation timing, refresh social card (#261)
1 parent b30121c commit 3deb7e2

27 files changed

Lines changed: 212 additions & 221 deletions

‎README.md‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ pip install that-depends
3939
## Ecosystem
4040

4141
`that-depends` is part of the [`modern-python`](https://github.com/modern-python) family.
42-
If you're starting a new project, consider [`modern-di`](https://github.com/modern-python/modern-di) —
42+
If you're starting a new project, consider [`modern-di`](https://github.com/modern-python/modern-di),
4343
the newer DI framework from the same author, with separate framework adapters:
4444

45-
- [`modern-di`](https://github.com/modern-python/modern-di) — core DI framework with scopes
45+
- [`modern-di`](https://github.com/modern-python/modern-di): core DI framework with scopes
4646
- [`modern-di-fastapi`](https://github.com/modern-python/modern-di-fastapi),
4747
[`modern-di-litestar`](https://github.com/modern-python/modern-di-litestar),
4848
[`modern-di-faststream`](https://github.com/modern-python/modern-di-faststream),
4949
[`modern-di-typer`](https://github.com/modern-python/modern-di-typer),
5050
[`modern-di-pytest`](https://github.com/modern-python/modern-di-pytest)
5151

52-
`that-depends` remains actively maintained — see the
52+
`that-depends` remains actively maintained. See the
5353
[migration guide](https://modern-di.modern-python.org/migration/from-that-depends/) if you
5454
want to move existing projects across.
5555

@@ -61,5 +61,4 @@ want to move existing projects across.
6161

6262
## Part of `modern-python`
6363

64-
Browse the full list of templates and libraries in
65-
[`modern-python`](https://github.com/modern-python) — see the org profile for the categorized index.
64+
The [`modern-python`](https://github.com/modern-python) org profile has the full categorized list of templates and libraries.

‎docs/assets/social-card.png‎

529 Bytes
Loading

‎docs/ecosystem.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Ecosystem
22

3-
`that-depends` is part of the [`modern-python`](https://github.com/modern-python) organization —
3+
`that-depends` is part of the [`modern-python`](https://github.com/modern-python) organization,
44
a collection of open-source templates and libraries for production-ready Python applications.
55

66
## Newer DI framework: `modern-di`
77

8-
If you're starting a new project, consider [`modern-di`](https://github.com/modern-python/modern-di) —
8+
If you're starting a new project, consider [`modern-di`](https://github.com/modern-python/modern-di),
99
the newer DI framework from the same author. It ships as a small core plus a family of thin
1010
framework adapters, in contrast to `that-depends`'s batteries-included approach.
1111

@@ -28,13 +28,13 @@ walks through the API differences if you want to move an existing project across
2828

2929
End-to-end examples using `modern-di` for dependency injection:
3030

31-
- [`fastapi-sqlalchemy-template`](https://github.com/modern-python/fastapi-sqlalchemy-template) —
31+
- [`fastapi-sqlalchemy-template`](https://github.com/modern-python/fastapi-sqlalchemy-template):
3232
dockerized web application with DI on FastAPI, SQLAlchemy 2, PostgreSQL
33-
- [`litestar-sqlalchemy-template`](https://github.com/modern-python/litestar-sqlalchemy-template) —
33+
- [`litestar-sqlalchemy-template`](https://github.com/modern-python/litestar-sqlalchemy-template):
3434
dockerized web application on LiteStar, SQLAlchemy 2, PostgreSQL
3535

3636
## Full project index
3737

3838
See the [`modern-python` organization profile](https://github.com/modern-python) for the
3939
complete categorized list, including microservice utilities (`lite-bootstrap`, the
40-
`faststream-*` family) and helper packages (`db-retry`, `eof-fixer`).
40+
`faststream-*` family) and helper packages (`db-retry`, `eof-fixer`).

‎docs/experimental/lazy.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Lazy Provider
1+
# Lazy provider
22

33
The `LazyProvider` enables you to reference other providers without explicitly
44
importing them into your module.
@@ -7,7 +7,7 @@ This can be helpful if you have a circular dependency between providers in
77
multiple containers.
88

99

10-
## Creating a Lazy Provider
10+
## Creating a lazy provider
1111

1212
=== "Single import string"
1313
```python

‎docs/index.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ async def create_async_resource():
4949
logger.debug("Async resource destructed")
5050
```
5151

52-
### Setup Dependency Injection Container with Providers
52+
### Set up a dependency injection container with providers
5353
```python
5454
from that_depends import BaseContainer, providers
5555

‎docs/integrations/fastapi.md‎

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
# Usage with FastAPI
22

33
!!! info "See also"
4-
[`modern-di-fastapi`](https://github.com/modern-python/modern-di-fastapi) — the equivalent
4+
[`modern-di-fastapi`](https://github.com/modern-python/modern-di-fastapi) is the equivalent
55
FastAPI integration for [`modern-di`](https://github.com/modern-python/modern-di), the newer
66
sibling DI framework.
77

88
## Installation
99

10-
To use **`that-depends`** with FastAPI, you need to install the package with the `fastapi` extra. You can do this using pip:
10+
To use `that-depends` with FastAPI, you need to install the package with the `fastapi` extra. You can do this using pip:
1111

1212
```bash
1313
pip install that-depends[fastapi]
1414
```
1515

1616
---
1717

18-
## Creating a Container
18+
## Creating a container
1919

2020
Suppose you have a simple container in a file called `mycontainer.py`:
2121

@@ -38,9 +38,9 @@ Here, `MyContainer.current_time` is a provider that, when called, creates a new
3838

3939
---
4040

41-
## Using a custom Router class
41+
## Using a custom router class
4242

43-
You can use the `create_fastapi_route_class()` method to create custom Route class for your application:
43+
You can use the `create_fastapi_route_class()` method to create a custom route class for your application:
4444

4545
```python
4646
from that_depends.integrations.fastapi import create_fastapi_route_class
@@ -58,7 +58,7 @@ router = APIRouter(route_class=my_route_class)
5858

5959
This will enable you to use dependency injection in your `FastAPI` endpoints:
6060

61-
> **Note**: If you don't want to use the custom router class, you can make use of `fastapi.Depends` instead.
61+
> If you don't want to use the custom router class, you can use `fastapi.Depends` instead.
6262
6363

6464
=== "Router class"
@@ -90,7 +90,7 @@ This will enable you to use dependency injection in your `FastAPI` endpoints:
9090

9191
### Managing container context
9292

93-
If you wish to initialize the container context you can simply pass arguments to `create_fastapi_route_class()`:
93+
To initialize the container context, pass arguments to `create_fastapi_route_class()`:
9494

9595
```python
9696
from that_depends import ContextScopes
@@ -103,16 +103,16 @@ and the global context will be set.
103103

104104

105105

106-
## Integrating with FastAPI Using DIContextMiddleware
106+
## Integrating with FastAPI using DIContextMiddleware
107107

108108
The `DIContextMiddleware` can be used to manage context, but its features overlap with the [custom router class](#using-a-custom-router-class).
109109
The main advantage of using middleware is that you can set it up for your entire `FastAPI` application.
110110

111-
> **Note:** If you want to use both the `DIContextMiddleware` and the custom router class, you should not pass any arguments to `create_fastapi_route_class()`.
111+
> If you want to use both the `DIContextMiddleware` and the custom router class, do not pass any arguments to `create_fastapi_route_class()`.
112112
113-
### Setting Up the FastAPI App
113+
### Setting up the FastAPI app
114114

115-
You can use **`that-depends`**’s `DIContextMiddleware` so that any request automatically initializes the context for your container(s). This approach is convenient if you want to:
115+
You can use the `DIContextMiddleware` from `that-depends` so that any request automatically initializes the context for your container(s). This approach is convenient if you want to:
116116

117117
- Automatically initialize or tear down resources on each request.
118118
- Provide a global or request-level context dictionary you can read from your container.
@@ -147,7 +147,7 @@ def get_time(
147147
)
148148
```
149149

150-
- **`DIContextMiddleware`** automatically sets a “global context” for every request.
150+
- `DIContextMiddleware` automatically sets a global context for every request.
151151
- The `Depends(MyContainer.current_time)` call is how you reference the container’s provider using the standard FastAPI injection system.
152152

153153
To run this app:
@@ -158,15 +158,15 @@ uvicorn main:app --reload
158158

159159
When you make a request to `/`, you will see the current time printed, and behind the scenes the that-depends container is in a context.
160160

161-
> **Note**: If your container uses advanced context-based resources (e.g. `ContextResource`), you may also set `default_scope` in your container, or configure an explicit scope. See the advanced section below.
161+
> If your container uses context-based resources such as `ContextResource`, you can also set `default_scope` in your container or configure an explicit scope. See the advanced section below.
162162
163163
---
164164

165165

166166

167-
## Examples of different Providers in FastAPI
167+
## Examples of different providers in FastAPI
168168

169-
### Singleton Providers
169+
### Singleton providers
170170

171171
```python
172172
# Suppose in mycontainer.py
@@ -195,7 +195,7 @@ def read_settings(settings: AppSettings = Depends(MyAdvancedContainer.settings))
195195
return {"db_url": settings.database_url}
196196
```
197197

198-
### Context Resources
198+
### Context resources
199199

200200
For “request-scoped” resources (e.g. a DB connection per request), you can use `ContextResource` in your container. This typically works in conjunction with `DIContextMiddleware` or a manual `container_context(...)` call. For example:
201201

@@ -247,7 +247,7 @@ async def read_db(
247247

248248
When writing unit tests, you can use `TestClient` from `starlette.testclient` or `pytest-asyncio` with standard FastAPI patterns. The `DIContextMiddleware` approach ensures resources are created and torn down automatically each request, so no special arrangement is necessary.
249249

250-
**Example**:
250+
For example:
251251

252252
```python
253253
import pytest
@@ -268,21 +268,21 @@ def test_read_db(client: TestClient):
268268

269269
---
270270

271-
## Common Patterns and Tips
271+
## Common patterns and tips
272272

273-
1. **Global vs. Request Context**: Decide whether your container’s dependencies should be globally shared (e.g., singletons) or created anew per request (e.g., database or session).
274-
2. **Combining with FastAPI’s `Depends`**: Generally, you can pass `Depends(MyContainer.some_provider)` to route handlers. Under the hood, that-depends will be invoked.
275-
3. **Overriding**: You can override a provider in tests by calling `MyContainer.some_provider.override_sync(...)` or using the context manager `with MyContainer.some_provider.override_context_sync(...):`.
276-
4. **Performance**: If you have expensive creation logic (like a DB engine that can be reused globally), prefer using a `Singleton` or `Object` provider. If you need ephemeral resources, use `ContextResource` with the `DIContextMiddleware`.
277-
5. **Custom Context**: If you do not want to rely on the middleware, you can manually create a context in any async function by calling `async with container_context(MyContainer):`.
278-
6. **Multiple Containers**: You can define multiple containers and connect them (e.g., `ContainerA.connect_containers(ContainerB)`), or add them all to the `DIContextMiddleware`. For advanced usage, see the that-depends documentation on “container connection.”
273+
- Decide whether your container's dependencies should be globally shared (e.g., singletons) or created anew per request (e.g., database or session).
274+
- You can generally pass `Depends(MyContainer.some_provider)` to route handlers, and `that-depends` resolves the provider under the hood.
275+
- You can override a provider in tests by calling `MyContainer.some_provider.override_sync(...)` or using the context manager `with MyContainer.some_provider.override_context_sync(...):`.
276+
- If you have expensive creation logic (like a DB engine that can be reused globally), prefer a `Singleton` or `Object` provider. If you need ephemeral resources, use `ContextResource` with the `DIContextMiddleware`.
277+
- If you do not want to rely on the middleware, you can create a context manually in any async function by calling `async with container_context(MyContainer):`.
278+
- You can define multiple containers and connect them (e.g., `ContainerA.connect_containers(ContainerB)`), or add them all to the `DIContextMiddleware`. See [Usage with multiple containers](../introduction/multiple-containers.md) for details.
279279

280280

281-
### Accessing the FastAPI Request or Other Context Items
281+
### Accessing the FastAPI request or other context items
282282

283283
Sometimes you want to pass the `fastapi.Request` (or other request-scoped data) into the container context so that providers can read it. You can do that either via the `DIContextMiddleware` (by customizing the `global_context` dynamically) or by writing your own dependency that calls `container_context()`.
284284

285-
**Example**: Writing a custom dependency that sets up the context with the current `Request`:
285+
For example, a custom dependency that sets up the context with the current `Request`:
286286

287287
```python
288288
# request_deps.py

‎docs/integrations/faststream.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Usage with `FastStream`
22

33
!!! info "See also"
4-
[`modern-di-faststream`](https://github.com/modern-python/modern-di-faststream) — the
4+
[`modern-di-faststream`](https://github.com/modern-python/modern-di-faststream) is the
55
equivalent FastStream integration for [`modern-di`](https://github.com/modern-python/modern-di),
66
the newer sibling DI framework.
77

8-
`that-depends` is out of the box compatible with `faststream.Depends()`:
8+
`that-depends` works out of the box with `faststream.Depends()`:
99

1010
```python hl_lines="14"
1111
from typing import Annotated
@@ -30,10 +30,10 @@ async def process(
3030
1. This would be the same as `Provide[Container.suffix_factory]`
3131

3232

33-
## Context Middleware
33+
## Context middleware
3434

35-
If you are using [ContextResource](../providers/context-resources.md) provider, you likely will want to
36-
initialize a context before processing message with `faststream.`
35+
If you are using [ContextResource](../providers/context-resources.md) provider, you will likely want to
36+
initialize a context before processing messages with `faststream.`
3737

3838
`that-depends` provides integration for these use cases:
3939

‎docs/integrations/litestar.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Usage with `Litestar`
22

33
!!! info "See also"
4-
[`modern-di-litestar`](https://github.com/modern-python/modern-di-litestar) — the equivalent
4+
[`modern-di-litestar`](https://github.com/modern-python/modern-di-litestar) is the equivalent
55
Litestar integration for [`modern-di`](https://github.com/modern-python/modern-di), the newer
66
sibling DI framework.
77

‎docs/introduction/generator-injection.md‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Injection into Generator Functions
1+
# Injection into generator functions
22

33

44
`that-depends` supports dependency injections into generator functions. However, this comes
@@ -43,9 +43,9 @@ You can use the `@inject` decorator to inject dependencies into generator functi
4343
yield value
4444
```
4545

46-
## Supported Generators
46+
## Supported generators
4747

48-
### Synchronous Generators
48+
### Synchronous generators
4949

5050
`that-depends` supports injection into sync generator functions with the following signature:
5151

@@ -60,7 +60,7 @@ This means that wrapping a sync generator with `@inject` will always preserve al
6060
- It will raise `StopIteration` when the generator is exhausted or otherwise returns.
6161

6262

63-
### Asynchronous Generators
63+
### Asynchronous generators
6464

6565
`that-depends` supports injection into async generator functions with the following signature:
6666

@@ -73,7 +73,7 @@ This means that wrapping an async generator with `@inject` will have the followi
7373
- The generator will yield as expected
7474
- The generator will **not** accept values via `asend()`
7575

76-
If you need to send values to an async generator, you can simply resolve dependencies in the generator body:
76+
If you need to send values to an async generator, you can resolve dependencies in the generator body:
7777

7878
```python
7979

@@ -95,7 +95,7 @@ as part of dependency injection into a generator.
9595

9696
This is the case for both async and sync injection.
9797

98-
**For example:**
98+
For example:
9999
```python
100100
def sync_resource() -> typing.Iterator[float]:
101101
yield random.random()
@@ -139,12 +139,10 @@ with container_context(scope=ContextScopes.REQUEST):
139139
next(injected())
140140
```
141141

142-
Since no context initialization was needed, the generator will work as expected.
143-
144142
1. Scope provided to `@inject` no longer matches scope of the `sync_provider`
145143

146144

147-
### Container Context
145+
### Container context
148146

149147
Similarly to above, the `@container_context` also does **not** support generators:
150148

0 commit comments

Comments
 (0)