Skip to content
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

Q: Can Configuration values be made required? #341

Closed
ms-lolo opened this issue Dec 21, 2020 · 7 comments
Closed

Q: Can Configuration values be made required? #341

ms-lolo opened this issue Dec 21, 2020 · 7 comments
Assignees
Labels

Comments

@ms-lolo
Copy link

ms-lolo commented Dec 21, 2020

Couldn't find an example of this in the docs but I am trying to ensure that the system fails fast if the container hasn't been properly configured when the necessary provider is called. For example:

class MyContainer(DeclarativeContainer):

    config = Configuration()

    some_client = Singleton(SomeClient, config.something_important)

Right now, if I forget to set mycontainer.config.something_important before calling mycontainer.some_client(), then SomeClient will be given None as its parameter which is actually worse than forgetting to pass the argument at all because Python will allow it even if it's not an optional parameter. I don't want to sprinkle my classes with something_important is None checks.

What I really want is to be able to specify the exact keys I would expect in a Configuration provider and have the Container initialization fail if those values are not provided. Do you have any suggestions?

@ms-lolo
Copy link
Author

ms-lolo commented Dec 21, 2020

I wonder if maybe what I want is a custom provider that behaves as a stricter version of Configuration.

@rmk135
Copy link
Member

rmk135 commented Dec 21, 2020

Hey @ms-lolo , that doesn't work out of the box, but that's a good idea. I'll think how it could be implemented. The wild guess I have now is to make something like this:

class MyContainer(DeclarativeContainer):

    config = Configuration()
    config.something_important.required()

    some_client = Singleton(SomeClient, config.something_important)

There also could be a strict mode for Configuration provider that will prohibit using any undefined values.

@rmk135
Copy link
Member

rmk135 commented Jan 16, 2021

I have released version 4.10.0 with strict mode and required modifier features for the Configuration provider. Here is a doc: https://python-dependency-injector.ets-labs.org/providers/configuration.html#strict-mode-and-required-options

@rmk135
Copy link
Member

rmk135 commented Jan 16, 2021

Closing the issue. Reopen if needed.

@rmk135 rmk135 closed this as completed Jan 16, 2021
@rmk135
Copy link
Member

rmk135 commented Jan 16, 2021

@ms-lolo , thanks for suggesting the feature!

@ms-lolo
Copy link
Author

ms-lolo commented Jan 16, 2021

this seems to solve my problem, thank a lot!

@rmk135
Copy link
Member

rmk135 commented Jan 16, 2021

Glad to hear that! Thank you again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants