FastStream uses the secondary library FastDepends for dependency management. This dependency system is literally borrowed from FastAPI, so if you know how to work with that framework, you'll be comfortable with dependencies in FastStream.
You can visit the FastDepends documentation for more details, but the key points and additions are covered here.
The key function in the dependency management and type conversion system in FastStream is the decorator @apply_types (also known as @inject in FastDepends).
By default, it applies to all event handlers, unless you disabled the same option when creating the broker.
Setting the apply_types=False flag not only disables type casting but also Depends and Context. If you want to disable only type casting, use validate=False instead.
This flag can be useful if you are using FastStream within another framework and you need to use its native dependency system.
The last step: Just use the result of executing your dependency!
It's easy, isn't it?
Auto @apply_types
In the code above, we didn't use this decorator for our dependencies. However, it still applies to all functions used as dependencies. Please keep this in your mind.
In the example above, the another_dependency function will be called at ONCE! FastDepends caches all dependency execution results within ONE@apply_types call stack. This means that all nested dependencies will receive the cached result of dependency execution. But, between different calls of the main function, these results will be different.
To prevent this behavior, just use Depends(...,cache=False). In this case, the dependency will be used for each function in the call stack where it is used.
In asynchronous code, you can use both synchronous and asynchronous dependencies. But in synchronous code, only synchronous dependencies are available to you.
FastDepends, used by FastStream, also gives the type return. This means that the value returned by the dependency will be be cast to the type twice: as return for dependencies and as the input argument of the main function. This does not incur additional costs if these types have the same annotation. Just keep it in mind. Or not... Anyway, I've warned you.
fromfaststreamimportDepends,apply_typesdefsimple_dependency(a:int,b:int=3)->str:returna+b# 'return' is cast to `str` for the first time@apply_typesdefmethod(a:int,d:int=Depends(simple_dependency)):# 'd' is cast to `int` for the second timereturna+dassertmethod("1")==5
Also, the result of executing the dependency is cached. If you use this dependency in N functions, this cached result will be converted to type N times (at the input to the function being used).
To avoid problems with this, use mypy or just be careful with the annotation of types in your project.