question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hi,

I just wonder if there any possible transform configuration as pydantic.BaseSettings type?

There are some frameworks are dependency on pydantic settings.

This is weird to import pydantic to DI’s config then to provide it as pydantic again

Or can I use like

from dependency_injector import containers
from pydantic import BaseSettings

class Settings(BaseSettings):
    ...


class Contrainer(containers.DeclarativeContainer):
    settings = providers.Singleton(Settings)

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jacquelinegarrahancommented, Feb 24, 2022

I’d like to second this concern- my current use case would benefit from a passed pydantic object rather than the dict:

from dependency_injector import containers, providers
from dependency_injector.wiring import Provide, inject

from pydantic import BaseSettings
import yaml

from slac_services.modeling import ModelDBConfig, ModelDB, ModelingService, RemoteModelingService, LocalModelingService


class Settings(BaseSettings):
    model_db: ModelDBConfig


class ServiceContainer(containers.DeclarativeContainer):

    config = providers.Configuration()

    model_db = providers.Singleton(
        ModelDB,
        config.model_db # would like to pass as Pydantic settings
    )

    remote_modeling_service = providers.Singleton(
        RemoteModelingService,
        model_db=model_db,
    )

    local_modeling_service = providers.Singleton(
        LocalModelingService
    )

0reactions
radyzcommented, Apr 25, 2022

I have the same issue but managed to work around it by

class Application(containers.DeclarativeContainer):
    settings = providers.ThreadSafeSingleton(Settings)

This works, although I would prefer to get the pydantic object from the start when using the pydantic_settings option 😅

Read more comments on GitHub >

github_iconTop Results From Across the Web

Settings management - pydantic
Create a clearly-defined, type-hinted application configuration class; Automatically read modifications to the configuration from environment variables ...
Read more >
Pedantic Configuration Management with Pydantic
The class GlobalConfig inherits from Pydantic's BaseSettings which helps to load and read the variables from the .env file. The .env file itself ......
Read more >
Pydantic Model Config with Examples | by ukyen | Dec, 2022
Pydantic is a powerful parsing library that validates input data during runtime. It brings a series configuration options in the Config ...
Read more >
Configuration management in Python like a Boss(Pydantic ...
Let's modify our configuration to have a different environment and some more configurations. 1. Class-based config. config.py. import os from typing import ...
Read more >
Model Config - pydantic
Behaviour of pydantic can be controlled via the Config class on a model or a pydantic dataclass. Python 3.7 and above. from pydantic...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found