|
| 1 | +# Duplicate Type Error |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +## Understanding the Error |
| 6 | + |
| 7 | +When you see this error: |
| 8 | + |
| 9 | +``` |
| 10 | +RuntimeError: Provider is duplicated by type <class 'SomeType'>. |
| 11 | +``` |
| 12 | + |
| 13 | +It means you have multiple providers that can provide the same type. This typically happens when: |
| 14 | + |
| 15 | +1. You have multiple factories that return the same type |
| 16 | +2. You're using the same class in different contexts with different configurations |
| 17 | + |
| 18 | +## How to Resolve |
| 19 | + |
| 20 | +To fix this error, you need to: |
| 21 | + |
| 22 | +1. Set `bound_type=None` on one of the duplicate providers to make it unresolvable by type |
| 23 | +2. Explicitly pass dependencies via the `kwargs` parameter to avoid automatic resolution |
| 24 | + |
| 25 | +Here's a complete example showing both steps: |
| 26 | + |
| 27 | +```python |
| 28 | +from modern_di import Group, Scope, providers |
| 29 | + |
| 30 | + |
| 31 | +class DatabaseConfig: |
| 32 | + def __init__(self, connection_string: str) -> None: |
| 33 | + self.connection_string = connection_string |
| 34 | + |
| 35 | + |
| 36 | +class Repository: |
| 37 | + def __init__(self, db_config: DatabaseConfig) -> None: |
| 38 | + self.db_config = db_config |
| 39 | + |
| 40 | + |
| 41 | +class MyGroup(Group): |
| 42 | + # Step 1: Set bound_type=None on the secondary provider or for both providers |
| 43 | + # This provider can be resolved by type: container.resolve(DatabaseConfig) |
| 44 | + primary_db_config = providers.Factory( |
| 45 | + scope=Scope.APP, |
| 46 | + creator=DatabaseConfig, |
| 47 | + kwargs={"connection_string": "postgresql://primary"} |
| 48 | + ) |
| 49 | + |
| 50 | + # This provider cannot be resolved by type |
| 51 | + # Must use: container.resolve_provider(MyGroup.secondary_db_config) |
| 52 | + secondary_db_config = providers.Factory( |
| 53 | + scope=Scope.APP, |
| 54 | + creator=DatabaseConfig, |
| 55 | + bound_type=None, # <-- Step 1: Makes it unresolvable by type |
| 56 | + kwargs={"connection_string": "postgresql://secondary"} |
| 57 | + ) |
| 58 | + |
| 59 | + # Step 2: Explicitly pass dependencies via kwargs for second repository or for both |
| 60 | + primary_repository = providers.Factory( |
| 61 | + scope=Scope.APP, |
| 62 | + creator=Repository, # <-- Implicit dependency, no kwargs |
| 63 | + ) |
| 64 | + |
| 65 | + secondary_repository = providers.Factory( |
| 66 | + scope=Scope.APP, |
| 67 | + creator=Repository, |
| 68 | + kwargs={"db_config": secondary_db_config} # <-- Step 2: Explicit dependency |
| 69 | + ) |
| 70 | +``` |
0 commit comments