Repository navigation
4.3.0
4.3.0 adds provider.derive(fn) and fixes the scope of providers written inline in a Factory's kwargs. The scope fix changes behaviour in ways that raise no error, so read "Changed" before upgrading.
Added
provider.derive(fn)returns a provider offn(value), wherevalueis whatproviderresolves to. Use it to pass one field of a settings object without writing a creator function for the component. Type checkers inferfn's argument from the provider. A derived provider is never cached, takes its scope from its source and is not registered under any type, so an override of the source reaches it. See Derived values. (#733)
class Dependencies(Group):
settings = providers.Factory(Settings, cache=True)
database = providers.Factory(Database, kwargs={"dsn": settings.derive(lambda s: s.db_dsn)}, cache=True)Changed
A provider written inline in a Factory's kwargs without its own scope= used to resolve at APP scope whatever the scope of the provider using it. It now takes the shallowest scope among the providers that use it, fixed when the container registers them. (#734)
- An inline
Factory(..., cache=True)under a REQUEST-scoped parent used to be one instance shared by every request. It is now one instance per request. To keep a single app-wide instance, passscope=Scope.APPto the inline provider. - An inline provider over a request-scoped source used to fail
validate()withInvalidScopeDependencyError. It now validates and resolves. - An inline
ContextProvidernow reads the context at its parent's scope instead of raisingContextValueNotSetErrorfrom the APP container. - A module-level provider without
scope=, used inline, now takes its parents' scope. If it must be APP-scoped, passscope=Scope.APP. - A group declared after registration that would change an inline provider's scope raises
ProviderScopeFrozenError, as it already did for class attributes.
See Inline providers.
Docs
- The integration guide and the starlette, fastapi, fastmcp, litestar and faststream pages say a child container's close is shielded from cancellation and abandoned with a warning after 10 seconds. (#728, #729, #731, #735)
What's Changed
- docs: say how an integration guarantees release after a cancelled child close by @lesnik512 in #728
- docs: say the starlette and fastapi child close is shielded and bounded by @lesnik512 in #729
- docs: say the fastmcp, litestar and faststream child close is shielded and bounded by @lesnik512 in #731
- feat: add provider.derive(fn) to inject a value computed from another provider by @lesnik512 in #733
- docs: describe the shielded, bounded child close in the integration guide by @lesnik512 in #735
- fix: give inline providers in Factory kwargs the scope of the providers using them by @lesnik512 in #734
Full Changelog: 4.2.2...4.3.0