Skip to content

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.
@Swannbm
Copy link
Author

Swannbm commented Mar 17, 2025

@joke2k hello ! Have you some time to review the proposed PR ? Would be much appreciate.

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