You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit b30121c
Browse filesBrowse the repository at this point in the historyBrowse files
docs: make code examples run and fix factual errors (#260)
- context-resources: add default_scope to the quick-start container, pass a
context item where container_context/DIContextMiddleware had no arguments,
use the real sync-context error message, list only global_context and scope
as middleware arguments
- scopes: ContextScopes.APP, return val, nested-scope example no longer
re-enters p in REQUEST, default_scope caveat
- fastapi: scoped middleware example uses MyScopedContainer with
scope=REQUEST, sync override helpers, missing imports
- factories: call provider/provider_sync, import ContextScopes from
that_depends, context example uses a Factory, drop closing sentence
- injection, generator-injection, ioc-container, object, index, lazy:
syntax and name errors
- multiple-containers, faststream, litestar: inline definitions instead of
importing the repo's tests package; Litestar lifespan type
- README, resources, provider-overriding, contributing: wording fixes
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).
271
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.
272
-
3.**Overriding**: You can override a provider in tests by calling `MyContainer.some_provider.override(...)` or using the context manager `with MyContainer.some_provider.override_context(...):`.
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(...):`.
273
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`.
274
-
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():`.
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):`.
275
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.”
276
279
277
280
@@ -285,7 +288,7 @@ Sometimes you want to pass the `fastapi.Request` (or other request-scoped data)
285
288
# request_deps.py
286
289
from fastapi import Request
287
290
from typing import AsyncIterator
288
-
from that_depends import container_context, fetch_context_item
Copy file name to clipboardExpand all lines: docs/introduction/injection.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -84,7 +84,7 @@ def greet_user_direct(
84
84
returnf"Greeting: {greeting}"
85
85
```
86
86
87
-
1. Notice that although `greeting` is a `str`, `mypy` and you IDE will not complain.
87
+
1. Notice that although `greeting` is a `str`, `mypy` and your IDE will not complain.
88
88
89
89
---
90
90
@@ -123,7 +123,7 @@ For more details regarding scopes and context management, see the [Context Resou
123
123
124
124
## Overriding Providers
125
125
126
-
In tests or specialized scenarios, you may want to override a provider’s value temporarily. You can do so with the container’s `override_providers()` method or the provider’s own `override_context()`:
126
+
In tests or specialized scenarios, you may want to override a provider’s value temporarily. You can do so with the container’s `override_providers_sync()` method or the provider’s own `override_context_sync()`:
127
127
128
128
```python
129
129
deftest_greet_override():
@@ -135,7 +135,7 @@ def test_greet_override():
135
135
136
136
This is especially helpful for unit tests where you want to substitute real dependencies (e.g., database connections) with mocks or stubs.
137
137
138
-
For more details on overring providers, see the [Overriding Providers](../testing/provider-overriding.md) documentation.
138
+
For more details on overriding providers, see the [Overriding Providers](../testing/provider-overriding.md) documentation.
0 commit comments