linkd.compose¶
- class Compose[source]¶
Class allowing for “composing” of multiple dependencies into a single object, to help declutter the signatures of functions that require a large number of dependencies.
If you have ever used msgspec or pydantic this will feel familiar. To use this feature, simply create a subclass of this class and define fields for the dependencies you wish to use.
class ComposedDependencies(linkd.Compose): foo: FooDep bar: BarDep baz: BazDep
Then, in place of specifying
foo,barandbazwithin the function signature, you can request a single object of typeComposedDependencies.async def function(deps: ComposedDependencies): ...
Linkd will automatically try to create an instance of your composed class with all the dependencies that it requires. As with defining dependencies within the function signature, you can use fallback and If or Try syntax within the composed class field annotations.
Warning
None of the fields may contain a dependency on a composed class.
Warning
Composed classes cannot have any defined methods, they will be erased at runtime.