python-dependency-injector icon indicating copy to clipboard operation
python-dependency-injector copied to clipboard

Injecting with typing.Annotated

Open maintain0404 opened this issue 2 years ago • 1 comments
trafficstars

Hello. Thank you for building and maintaining awesome project.

FastAPI recently announced a new feature called Annotated. link Currently, dependency-injector is not compatible with this feature. How about modify it to be compatible? Like fastapi, wiring with Annotated can make several benefits like re-using, more compatibility with type checkers and so on.

See sample code following.

from typing import Annotated
from dependency_injector.containers import DeclarativeContainer
from dependency_injector.providers import Factory
from dependency_injector.wiring import inject


class Service:
    ...


class Container(DeclarativeContainer):
    service = Factory(Service)


WiredService = Annotated[Service, Provide[Container.service]]


@inject
def to_be_injected(service: WiredService):
    ...

I already made it for using at my personal work. If PR is welcome, I'll made PR with the code I've wrote and other work.

maintain0404 avatar Apr 07 '23 10:04 maintain0404

Please make PR or share how you implemented this

fadedDexofan avatar Jun 19 '23 17:06 fadedDexofan