Skip to content

Commit 5855301

Browse files
committed
Updated README.
Signed-off-by: Pavel Kirilin <win10@list.ru>
1 parent c6e14af commit 5855301

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,52 @@ This code will is going to print:
260260
strstr
261261
100
262262
```
263+
264+
## Dependencies replacement
265+
266+
You can replace dependencies in runtime, it will recalculate graph
267+
and will execute your function with updated dependencies.
268+
269+
**!!! This functionality tremendously slows down dependency resolution.**
270+
271+
Use this functionality only for tests. Otherwise, you will end up building dependency graphs on every resolution request. Which is very slow.
272+
273+
But for tests it may be a game changer, since you don't want to change your code, but some dependencies instead.
274+
275+
Here's an example. Imagine you have a built graph for a specific function, like this:
276+
277+
```python
278+
from taskiq_dependencies import DependencyGraph, Depends
279+
280+
281+
def dependency() -> int:
282+
return 1
283+
284+
285+
def target(dep_value: int = Depends(dependency)) -> None:
286+
assert dep_value == 1
287+
288+
graph = DependencyGraph(target)
289+
```
290+
291+
Normally, you would call the target, by writing something like this:
292+
293+
```python
294+
with graph.sync_ctx() as ctx:
295+
target(**ctx.resolve_kwargs())
296+
```
297+
298+
But what if you want to replace dependency in runtime, just
299+
before resolving kwargs? The solution is to add `replaced_deps`
300+
parameter to the context method. For example:
301+
302+
```python
303+
def replaced() -> int:
304+
return 2
305+
306+
307+
with graph.sync_ctx(replaced_deps={dependency: replaced}) as ctx:
308+
target(**ctx.resolve_kwargs())
309+
```
310+
311+
Furthermore, the new dependency can depend on other dependencies. Or you can change type of your dependency, like generator instead of plain return. Everything should work as you would expect it.

0 commit comments

Comments
 (0)