mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-06-09 16:13:31 +03:00
* Add example for Annotated attribute injection for module/class attributes * Fix attribute injection with Annotated types * Add unit tests for Annotated attribute and argument injection in wiring * Add .cursor to .gitignore * Style: add blank lines between class definitions and attributes in annotated attribute example * Docs: clarify and format module/class attribute injection for classic and Annotated forms * Changelog: add note and discussion link for Annotated attribute injection support * Fix nls * Fix CI checks and Python 3.8 tests * Fix PR issues * Fix Python 3.8 tests * Fix flake8 issues * Fix: robust Annotated detection for wiring across Python versions * Refactor: extract annotation retrieval and improve typing for Python 3.9 compatibility * Update src/dependency_injector/wiring.py Co-authored-by: ZipFile <zipfile.d@protonmail.com> --------- Co-authored-by: ZipFile <zipfile.d@protonmail.com>
32 lines
613 B
Python
32 lines
613 B
Python
"""Wiring attribute example with Annotated."""
|
|
|
|
from typing import Annotated
|
|
|
|
from dependency_injector import containers, providers
|
|
from dependency_injector.wiring import Provide
|
|
|
|
|
|
class Service:
|
|
...
|
|
|
|
|
|
class Container(containers.DeclarativeContainer):
|
|
|
|
service = providers.Factory(Service)
|
|
|
|
|
|
service: Annotated[Service, Provide[Container.service]]
|
|
|
|
|
|
class Main:
|
|
|
|
service: Annotated[Service, Provide[Container.service]]
|
|
|
|
|
|
if __name__ == "__main__":
|
|
container = Container()
|
|
container.wire(modules=[__name__])
|
|
|
|
assert isinstance(service, Service)
|
|
assert isinstance(Main.service, Service)
|