kodemore / kink

Dependency injection container made for Python
MIT License
397 stars 25 forks source link

Pydantic and Kink #56

Closed YanSte closed 3 months ago

YanSte commented 4 months ago

Hi,

Thank you very much for your work on this library. It's simple and easy to use. I considered using another library, but it hasn't been updated.

I have a question regarding the integration with Pydantic. Specifically, I want to know how to use dependency injection with Pydantic models. Here's what I mean:

@inject
class A(BaseModel):
    ...
    b: B

Is it possible to inject B without implementing the __init__ method manually?

Thank you!

YanSte commented 4 months ago

I found this solution: (Pydantic v1)

# No @inject
class A(BaseModel):
    ...
    b: B = Field(default_factory=lambda: di[B])

Could you tell me what you think?

YanSte commented 4 months ago

After a try that works great ✌️ Thanks a lot you save my time.