-
-
Notifications
You must be signed in to change notification settings - Fork 485
feat: make environment flag parsing consistent with click
#4443
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
base: main
Are you sure you want to change the base?
Changes from 8 commits
441f6fa
d2c7573
72264f0
b7ef9eb
e0e7d74
afdfabe
bd3b530
f597f83
2a3a508
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| from enum import Enum | ||
| from functools import partial | ||
| from os import getenv | ||
| from typing import TYPE_CHECKING, TypeVar, cast | ||
| from urllib.parse import quote | ||
|
|
||
|
|
@@ -102,3 +103,25 @@ def get_exception_group() -> type[BaseException]: | |
| from exceptiongroup import ExceptionGroup as _ExceptionGroup # pyright: ignore | ||
|
|
||
| return cast("type[BaseException]", _ExceptionGroup) | ||
|
|
||
|
|
||
| def envflag(varname: str, default: bool = False) -> bool: | ||
|
||
| """Get the boolean value of an environment variable. | ||
|
|
||
| Determines whether an environment variable is set to a truthy value. | ||
| Returns True if the variable exists and its value matches one of: | ||
| "1", "true", "t", "yes", "on", "y" (case-insensitive). | ||
| Otherwise, including when the variable is not set, returns False. | ||
|
|
||
| Args: | ||
| varname (str): The name of the environment variable to check. | ||
| default (bool): Value to return if the variable is not set OR empty. | ||
|
|
||
| Returns: | ||
| bool: True if the environment variable is set to a truthy value, | ||
| otherwise False. | ||
| """ | ||
| envvar = getenv(varname) | ||
| if not envvar: | ||
| return default | ||
|
||
| return envvar.lower() in ("1", "true", "t", "yes", "on", "y") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,10 @@ | ||
| import os | ||
| from functools import partial | ||
| from typing import Any, Generic, TypeVar | ||
|
|
||
| import pytest | ||
|
|
||
| from litestar.utils.helpers import get_name, unique_name_for_scope, unwrap_partial | ||
| from litestar.utils.helpers import envflag, get_name, unique_name_for_scope, unwrap_partial | ||
|
|
||
| T = TypeVar("T") | ||
|
|
||
|
|
@@ -45,3 +46,40 @@ def test_unique_name_for_scope() -> None: | |
| assert unique_name_for_scope("a", ["a", "a_0", "b"]) == "a_1" | ||
|
|
||
| assert unique_name_for_scope("b", ["a", "a_0", "b"]) == "b_0" | ||
|
|
||
|
|
||
| def test_envflag_truthy_values() -> None: | ||
| for value in ("1", "true", "TRUE", "t", "T", "yes", "YES", "on", "ON", "y", "Y"): | ||
| os.environ["TEST_FLAG"] = value | ||
| assert envflag("TEST_FLAG") is True | ||
| del os.environ["TEST_FLAG"] | ||
|
||
|
|
||
|
|
||
| def test_envflag_falsy_values() -> None: | ||
| for value in ("0", "false", "no", "off", ""): | ||
| os.environ["TEST_FLAG"] = value | ||
| assert envflag("TEST_FLAG") is False | ||
| del os.environ["TEST_FLAG"] | ||
|
|
||
|
|
||
| def test_envflag_missing() -> None: | ||
| assert envflag("NONEXISTENT_VAR") is False | ||
| assert envflag("NONEXISTENT_VAR_123", default=True) is True | ||
| assert envflag("NONEXISTENT_VAR_456", default=False) is False | ||
|
|
||
|
|
||
| def test_envflag_overrides_default() -> None: | ||
| os.environ["TEST_FLAG"] = "true" | ||
| assert envflag("TEST_FLAG", default=False) is True | ||
| del os.environ["TEST_FLAG"] | ||
|
|
||
| os.environ["TEST_FLAG"] = "0" | ||
| assert envflag("TEST_FLAG", default=True) is False | ||
| del os.environ["TEST_FLAG"] | ||
|
|
||
|
|
||
| def test_envflag_empty_string_uses_default() -> None: | ||
| os.environ["TEST_FLAG"] = "" | ||
| assert envflag("TEST_FLAG", default=True) is True | ||
| assert envflag("TEST_FLAG", default=False) is False | ||
| del os.environ["TEST_FLAG"] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.