Skip to content

Commit 6497a4f

Browse files
docs(event-handler): add import path gotcha for dependency_overrides testing (#8269)
* docs(event-handler): add import path gotcha for dependency_overrides testing docs(event-handler): add import path gotcha for dependency_overrides testing Signed-off-by: hirenkumar-n-dholariya <hirenkumarnd@gmail.com> * docs(event-handler): add import path gotcha for dependency_overrides testing docs(event-handler): add import path gotcha for dependency_overrides testing Signed-off-by: hirenkumar-n-dholariya <hirenkumarnd@gmail.com> --------- Signed-off-by: hirenkumar-n-dholariya <hirenkumarnd@gmail.com> Co-authored-by: Leandro Damascena <lcdama@amazon.pt>
1 parent c0804d4 commit 6497a4f

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

docs/core/event_handler/api_gateway.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,33 @@ Use `dependency_overrides` to replace any dependency with a mock or stub during
14591459
--8<-- "examples/event_handler_rest/src/dependency_injection_testing.py"
14601460
```
14611461

1462+
???+ warning "Import path must match exactly when using dependency_overrides"
1463+
When using `dependency_overrides` in tests, the imported dependency reference must use the **exact same import path** as the handler file. Python treats differently-imported modules as different objects in `sys.modules`, so the override will be silently ignored otherwise.
1464+
1465+
=== "✅ Correct"
1466+
1467+
```python
1468+
# handler.py
1469+
from depends import get_config
1470+
1471+
# test_handler.py - matches handler's import path exactly
1472+
from depends import get_config
1473+
1474+
app.dependency_overrides[get_config] = lambda: "test-value"
1475+
```
1476+
1477+
=== "❌ Wrong"
1478+
1479+
```python
1480+
# handler.py
1481+
from depends import get_config
1482+
1483+
# test_handler.py - different import path, override won't apply
1484+
from my_app.api_handler.depends import get_config
1485+
1486+
app.dependency_overrides[get_config] = lambda: "test-value"
1487+
```
1488+
14621489
???+ tip "Caching behavior"
14631490
By default, dependencies are cached within the same invocation (`use_cache=True`). If the same dependency is used by multiple handlers or sub-dependencies, it is resolved once and the result is reused. Use `Depends(fn, use_cache=False)` to resolve every time.
14641491

0 commit comments

Comments
 (0)