Skip to content

Providers - pass config dict as kwargs to provided class #881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
frenzyk opened this issue May 10, 2025 · 0 comments
Open

Providers - pass config dict as kwargs to provided class #881

frenzyk opened this issue May 10, 2025 · 0 comments

Comments

@frenzyk
Copy link

frenzyk commented May 10, 2025

Is there any way to pass config dict as **kwargs to my class?
There is 2 problem I try to solve:

  1. Too many args will lead to an additional manual labor of mapping config
  2. in example below session_db has a default, which become useless as if not provided in config it will be None.
from dependency_injector import containers, providers

class Client:
    def __init__(
            self,
            api_id: int,
            session_db: str | Path = "some default value",
    ):
        self.api_id = api_id
        self.session_db = session_db


class ApplicationContainer(containers.DeclarativeContainer):
    config = providers.Configuration()

    my_client = providers.Singleton(
        Client,
        config.client.api_id,
        config.client.session_db,
    )


container = ApplicationContainer()
container.config.from_dict(
    {
        "client": {
             "api_id": "id",
         },
    },
)
my_client = container.my_client()
assert my_client.session_db == "some default value"

What I want is something like

class ApplicationContainer(containers.DeclarativeContainer):
    config = providers.Configuration()

    my_client = providers.Singleton(
        Client,
        **config.client
    )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant