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

Feature/add choice parameter and raise an exception if fetched value is not within #555

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

Swannbm
Copy link

@Swannbm Swannbm commented Feb 21, 2025

This PR addresses issue #193 (specifying allowed options)

Use case 1:

We don't want to hide warnings because a log-reading probe catches them and triggers an alert.

env = Env(LOGGING_LEVEL="EXCEPTION")
LOGGING_LEVEL = env.str("LOGGING_LEVEL")  # Ok
LOGGING_LEVEL = env.str("LOGGING_LEVEL", choices=["DEBUG", "INFO", "WARNING"])  # raise ImproperlyConfigured

Use case 2:

Define a set of execution environments with dedicated settings, and ensure the app is running in one of them.

env = Env(ENVIRONMENT="prod")
ENVIRONMENT = env.str("ENVIRONMENT")  # Ok
ENVIRONMENT = env.str("ENVIRONMENT", choices=["local", "qa", "staging", "production"])  # raise ImproperlyConfigured

Solution proposed:

  • The use cases considered only environment variables of type str.
  • Add a choices parameter to Env.str() to allow the user to define allowed options.
  • Ensure that the fetched value is included in the provided choices list when the choices parameter is set.

I have limited the scope of work, but I can extend the mechanism to some other fetching methods if you see use cases...

Let me know if some corrections are needed.

joke2k and others added 2 commits January 13, 2025 15:27
…ces. Raise improperlyConfigured if the value is not within choices.
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

Successfully merging this pull request may close these issues.

2 participants