Migration guide: upgrading to modern-di 1.x¶
Historical guide
This guide covers migrating from 0.x to 1.x. The APIs shown here (AsyncContainer, SyncContainer, providers.Singleton, .cast) were removed in 2.x.
If you are on 1.x today, also follow the 2.x migration guide to reach the current API.
modern-di 1.x inverts where resolution methods live and replaces a handful of provider types. Breaking changes, once:
-
BaseGraphis replaced byGroup, and the singleContainerbyAsyncContainerorSyncContainer(async supports both sync and async resolution; sync is sync-only). -
Resolution moved from the provider to the container, and can now target a type directly (requires passing
groups=at construction):# Before (0.x) instance = provider.sync_resolve(container) instance = await provider.async_resolve(container) # After (1.x) instance = container.sync_resolve_provider(provider) instance = await container.resolve_provider(provider) instance = container.sync_resolve(SomeType) # new: resolve by type instance = await container.resolve(SomeType)Manual provider overrides and the way dependencies are declared in web-framework applications changed accordingly. Both now go through the container and integration APIs.
-
SelectorandContextAdapterare removed. Replace both withFactory+ContextProvider: -
AttrGetteris removed. Reference the provider directly, or write a small factory function that extracts the attribute. - Factory attribute access (
.async_provider/.sync_provider) is removed. Inject the container itself and resolve dependencies manually instead of injecting a factory function. async_enter()is renamed toenter().
More¶
See the 2.x migration guide to move from here to the current API.