Duplicate Type Error¶
This error occurs when two or more providers are registered with the same bound_type. Modern-DI uses the bound_type to resolve dependencies by type, so each type must be unique in the providers registry.
Understanding the Error¶
When you see this error:
It means you have multiple providers that can provide the same type. This typically happens when:
- You have multiple factories that return the same type
- You're using the same class in different contexts with different configurations
How to Resolve¶
To fix this error, you need to:
- Set
bound_type=Noneon one of the duplicate providers to make it unresolvable by type - Explicitly pass dependencies via the
kwargsparameter to avoid automatic resolution
Here's a complete example showing both steps:
from modern_di import Group, Scope, providers
class DatabaseConfig:
def __init__(self, connection_string: str) -> None:
self.connection_string = connection_string
class Repository:
def __init__(self, db_config: DatabaseConfig) -> None:
self.db_config = db_config
class MyGroup(Group):
# Step 1: Set bound_type=None on the secondary provider or for both providers
# This provider can be resolved by type: container.resolve(DatabaseConfig)
primary_db_config = providers.Factory(
scope=Scope.APP,
creator=DatabaseConfig,
kwargs={"connection_string": "postgresql://primary"}
)
# This provider cannot be resolved by type
# Must use: container.resolve_provider(MyGroup.secondary_db_config)
secondary_db_config = providers.Factory(
scope=Scope.APP,
creator=DatabaseConfig,
bound_type=None, # <-- Step 1: Makes it unresolvable by type
kwargs={"connection_string": "postgresql://secondary"}
)
# Step 2: Explicitly pass dependencies via kwargs for second repository or for both
primary_repository = providers.Factory(
scope=Scope.APP,
creator=Repository, # <-- Implicit dependency, no kwargs
)
secondary_repository = providers.Factory(
scope=Scope.APP,
creator=Repository,
kwargs={"db_config": secondary_db_config} # <-- Step 2: Explicit dependency
)