Skip to content

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

Merged
joke2k merged 4 commits intojoke2k:developfrom
Swannbm:feature/add-choice-parameter-and-raise-an-exception-if-fetched-value-is-not-within
Feb 17, 2026
Merged

Feature/add choice parameter and raise an exception if fetched value is not within#555
joke2k merged 4 commits intojoke2k:developfrom
Swannbm:feature/add-choice-parameter-and-raise-an-exception-if-fetched-value-is-not-within

Conversation

@Swannbm
Copy link
Contributor

@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.

…ces. Raise improperlyConfigured if the value is not within choices.
@Swannbm
Copy link
Contributor Author

Swannbm commented Mar 17, 2025

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

joke2k and others added 2 commits February 17, 2026 22:40
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@coveralls
Copy link

Coverage Status

coverage: 96.759% (+0.02%) from 96.743%
when pulling fa790df on Swannbm:feature/add-choice-parameter-and-raise-an-exception-if-fetched-value-is-not-within
into c441413 on joke2k:develop.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@joke2k joke2k merged commit 24b299e into joke2k:develop Feb 17, 2026
@joke2k
Copy link
Owner

joke2k commented Feb 17, 2026

Thank you @Swannbm for the contribution and collaboration on this feature. Really appreciated by the maintainers and community. 🙌

ulgens pushed a commit to ulgens/django-environ that referenced this pull request Feb 22, 2026
…is not within (joke2k#555)

* set new parameter in str to coerce value into a specific list of choices. Raise improperlyConfigured if the value is not within choices.

* Fix indentation after rebase in Env.str

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Document Env.str choices option

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Daniele Faraglia <joke2k@users.noreply.github.com>
Co-authored-by: joke2k <daniele.faraglia@gmail.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

3 participants