modern-python / that-depends

DI-framework, inspired by python-dependency-injector, but without wiring. Python 3.12 is supported
https://that-depends.readthedocs.io/
MIT License
156 stars 12 forks source link

30 configuration provider #43

Closed lesnik512 closed 4 months ago

lesnik512 commented 4 months ago
import dataclasses

import pydantic

from that_depends import BaseContainer, providers

@dataclasses.dataclass(kw_only=True, slots=True)
class SingletonFactory:
    dep1: str

class Settings(pydantic.BaseModel):
    some_setting: str = "some_value"
    other_setting: str = "other_value"

class DIContainer(BaseContainer):
    settings: Settings = providers.Singleton(Settings).cast
    singleton = providers.Singleton(SingletonFactory, dep1=settings.some_setting)

async def test_singleton_attr_getter() -> None:
    singleton1 = await DIContainer.singleton()

    assert singleton1.dep1 == Settings().some_setting

    await DIContainer.tear_down()
lesnik512 commented 4 months ago

@jasonprado @vrslev hello, guys! What do you think about this way of lazy attributes getting?

vrslev commented 4 months ago

Hey there!

I guess, if mypy is happy then it works!

vrslev commented 4 months ago

Yes, indeed: I like it 👍