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

Config confuse

Open xuexiaolei1997 opened this issue 1 year ago • 3 comments

Under normal circumstances, the following code can be run:

class Container(containers.DeclarativeContainer):
    
    config: CommonConfig = providers.Configuration()

    # redis pool
    redis_pool = providers.Resource(
        init_redis_pool,
        config=config
    )

    async def init_resources(self):
        super().init_resources()

    async def shutdown_resources(self):
        redis_client = await self.redis_pool()
        await redis_client.close()

But I want to write the code in the following way:

class Container(containers.DeclarativeContainer):
    
    config: CommonConfig = providers.Configuration()

    # redis pool
    redis_pool = providers.Resource(
        init_redis_pool,
        redis_config=config.redis
    )

    async def init_resources(self):
        super().init_resources()

    async def shutdown_resources(self):
        redis_client = await self.redis_pool()
        await redis_client.close()

In this case, an error occurred: ***(Pydantic.BaseModel) object has no attribute 'get' Here's how I declare config:

import yaml
from pydantic import BaseModel, Field

class RedisConfig(BaseModel):
    host: str
    port: int
    password: str
    db: str
    max_connections: int = Field(..., ge=2, le=20)

class CommonConfig(BaseModel):
    redis: RedisConfig
    log_level: str


def load_config(config_path: str) -> CommonConfig:
    with open(config_path, "r") as f:
        config = yaml.safe_load(f)
    return CommonConfig(**config)

_config = load_config(config_path)
_container = Container(config=_config)
await _container.init_resources()
_container.wire(modules=[__name__])

xuexiaolei1997 avatar Apr 15 '25 03:04 xuexiaolei1997

What I understand is that when I declare a class, after passing in the config variable, it will be overridded, so it can be called directly. But from the results, config.redis doesn't dynamically modify providers.Resource based on config changes, which I'm puzzled about.

xuexiaolei1997 avatar Apr 15 '25 03:04 xuexiaolei1997

What I understand is that when I declare a class, after passing in the config variable, it will be overridded, so it can be called directly. But from the results, config.redis doesn't dynamically modify providers.Resource based on config changes, which I'm puzzled about.

that's how I declare nested config

....

class Container(containers.DeclarativeContainer):
    __self__ = providers.Self()

    wiring_config = containers.WiringConfiguration(
        modules=["..."]
    )
    # settings
    settings = providers.Singleton(
        ApplicationSettings,
        _env_file=os.getenv("ENV_FILE", DEFAULT_ENV_FILE_NAME),
    )

    # logger
    logger = providers.Singleton(
        init_logger,
        level=settings.provided.logger.level,
        ...
    )

mainden7 avatar May 12 '25 14:05 mainden7

What are you doing is roughly equivalent to the following:

_config = load_config(config_path)
_container = Container()
_container.override_providers(config=_config)
await _container.init_resources()
_container.wire(modules=[__name__])

But you want:

_config = load_config(config_path)
_container = Container()
_container.config.from_pydantic(_config)
await _container.init_resources()
_container.wire(modules=[__name__])

For details, refer to the documentation:

ZipFile avatar May 28 '25 18:05 ZipFile