Skip to content

Injecting with typing.Annotated #693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
maintain0404 opened this issue Apr 7, 2023 · 2 comments · Fixed by #721
Closed

Injecting with typing.Annotated #693

maintain0404 opened this issue Apr 7, 2023 · 2 comments · Fixed by #721

Comments

@maintain0404
Copy link
Contributor

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 maintain0404 changed the title Support typing.Annotated Injecting with typing.Annotated Apr 7, 2023
@fadedDexofan
Copy link

Please make PR or share how you implemented this

@ZipFile
Copy link
Contributor

ZipFile commented Feb 25, 2025

Done in v4.46.0 (#721).

@ZipFile ZipFile closed this as completed Feb 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants