Dependency Injection API¶
Module: wepositive_di.di
Depends = _DependsType()
module-attribute
¶
registry = containers.DynamicContainer()
module-attribute
¶
register_provider(name=None, singleton=False, context_manager=False)
¶
Source code in src/wepositive_di/di.py
setup(overrides=None)
¶
Wire the dependency injection system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
overrides
|
dict[Callable[..., Any] | str, Callable[..., Any]] | None
|
Optional dictionary of provider overrides to apply before wiring. Maps original providers to their override implementations. |
None
|
Usage:
setup()
def redis_storage() -> ContextStorage:
return RedisContextStorage()
setup(overrides={context_storage_singleton: redis_storage})
Source code in src/wepositive_di/di.py
override_provider(original, override=None)
¶
Override a provider with a new implementation.
Can be used as a function or a decorator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
original
|
Callable[..., Any] | str
|
The original provider function or its name |
required |
override
|
Callable[..., Any] | None
|
The new provider function (when used as a function call) |
None
|
Returns:
| Type | Description |
|---|---|
None | Callable[[Callable[..., Any]], Callable[..., Any]]
|
None when used as a function, decorator when used as @override_provider(original) |
Usage:
@register_provider()
async def config() -> Config:
return Config()
async def prod_config() -> Config:
return Config(db_url="production")
override_provider(config, prod_config)
Source code in src/wepositive_di/di.py
clear_overrides()
¶
provider_overrides(overrides)
¶
Context manager to temporarily override providers for testing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
overrides
|
dict[Callable[..., Any] | str, Callable[..., Any]]
|
Dictionary mapping original providers to their overrides |
required |
Usage:
async def test_config() -> Config:
return Config(sqlalchemy_db_uri=SecretStr("sqlite:///:memory:"))
with provider_overrides({config: test_config}):
result = await my_function()
Source code in src/wepositive_di/di.py
inject(func)
¶
Decorator that resolves Depends markers in function arguments.
Works with both sync and async functions.
The decorator:
- Inspects the function signature for
_DependsMarkerdefaults. - At call time, resolves each marker by calling the registry provider.
- Handles context manager providers transparently: enters the context manager, passes the yielded value to the function, and exits on completion.
- Returns the appropriate wrapper based on the function type.
Provider types and how they are resolved:
- AsyncCMFactory: await coroutine, get context manager, await
__aenter__. - CMFactory: get context manager, call
__enter__. - providers.Coroutine: await result.
- providers.Factory / providers.Singleton: use result directly.
Usage:
@inject
def my_func(config: Config = Depends[config]):
return config.value
@inject
async def my_async_func(session: AsyncSession = Depends[async_session]):
return session.query(...)
Source code in src/wepositive_di/di.py
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 | |
The custom provider wrapper classes used internally by register_provider() are
documented in Provider Classes.