Pydantic Config not worked
Closed this issue · 8 comments
class Config(BaseSettings):
server_id: int
model_config = SettingsConfigDict(env_nested_delimiter="__")
@lru_cache
def get_config()->Config:
return Config()
class Services(containers.DeclarativeContainer):
config: Config = providers.Configuration()
print(config.server_id) # <dependency_injector.providers.ConfigurationOption('config.server_id') at 0x757d0f761f50>
print(config.server_id()) # None
cache = providers.Factory(CacheService, memcache_repository=repositories.memcache)
class MainApplication(containers.DeclarativeContainer):
args = providers.Callable(parse_arguments)
config = providers.Configuration()
core = providers.Container(Core)
services = providers.Container(Services, config=config, gateways=gateways, repositories=repositories)
if __name__ == "__main__":
config = get_config()
application = MainApplication()
application.override_providers(config=config)
application.core.init_resources()
print(config.server_id) # <dependency_injector.providers.ConfigurationOption('config.server_id') at 0x757d0f761f50>
print(config.server_id()) # None This is expected behavior. Your config is not bound to the container at this stage yet, neither it is loaded from your pydantic settings. Use application.config.from_pydantic(config) instead of application.override_providers(config=config).
print(config.server_id) # <dependency_injector.providers.ConfigurationOption('config.server_id') at 0x757d0f761f50>
print(config.server_id()) # None
This is expected behavior. Yourconfigis not bound to the container at this stage yet, neither it is loaded from your pydantic settings. Useapplication.config.from_pydantic(config)instead ofapplication.override_providers(config=config).
problem remains
class Config(BaseSettings):
server_id: int
model_config = SettingsConfigDict(env_nested_delimiter="__")
@lru_cache
def get_config()->Config:
return Config()
class Services(containers.DeclarativeContainer):
config: Config = providers.Configuration()
print(config.server_id) # <dependency_injector.providers.ConfigurationOption('config.server_id') at 0x757d0f761f50>
print(config.server_id()) # None
cache = providers.Factory(CacheService, memcache_repository=repositories.memcache)
class MainApplication(containers.DeclarativeContainer):
args = providers.Callable(parse_arguments)
config = providers.Configuration()
core = providers.Container(Core)
services = providers.Container(Services, config=config, gateways=gateways, repositories=repositories)
if __name__ == "__main__":
config = get_config()
application = MainApplication()
application.config.from_pydantic(config)
application.core.init_resources()
One of:
- Keep
Configurationprovider only at the top-level container (MainApplication) and use Dependency provider to pass config values down. - Explicitly load configs for each sub-container:
application.services.config.from_pydantic(config)
One of:
- Keep
Configurationprovider only at the top-level container (MainApplication) and use Dependency provider to pass config values down.- Explicitly load configs for each sub-container:
application.services.config.from_pydantic(config)
I don't understand you, what's the problem with making a normal configuration, why is it so inconvenient
what's the problem with making a normal configuration
If you have ideas how to do it better, please send PR.
why is it so inconvenient
Configuration provider was made to be used in declarative fashion. If you do not need it, just replace config = providers.Configuration() with config = providers.Factory(get_config).
what's the problem with making a normal configuration
If you have ideas how to do it better, please send PR.
why is it so inconvenient
Configurationprovider was made to be used in declarative fashion. If you do not need it, just replace with .config = providers.Configuration()``config = providers.Factory(get_config)
None of your ideas worked for me. You don't seem to have checked it out.
I need fully reproducible example to assist you further. Note: by fully I mean something I can copypaste and run with python without any modifications. Code you provided in the issue description is not sufficient.
File "src/dependency_injector/providers.pyx", line 878, in dependency_injector.providers.Dependency.getattr
AttributeError: Provider "Dependency" has no attribute "server_id"
class Config(BaseSettings):
server_id: int
model_config = SettingsConfigDict(env_nested_delimiter="__")
@lru_cache
def get_config()->Config:
return Config()
class Services(containers.DeclarativeContainer):
config: Config = providers.Dependency(instance_of=Config)
print(config.server_id)
cache = providers.Factory(CacheService, memcache_repository=repositories.memcache)
class MainApplication(containers.DeclarativeContainer):
args = providers.Callable(parse_arguments)
config = providers.Configuration()
core = providers.Container(Core)
services = providers.Container(Services, config=config, gateways=gateways, repositories=repositories)
if __name__ == "__main__":
config = get_config()
application = MainApplication()
application.config.from_pydantic(config)
application.core.init_resources()