[bug] add list support in setting attributes from env variables (#8295)

This commit is contained in:
rbrugaro 2025-05-08 13:03:23 -07:00 committed by GitHub
parent 05f3840ca5
commit 3606ca87d5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 2 deletions

View File

@ -89,8 +89,10 @@ def load_from_env(
# Attempt to cast the env var to type hinted in the dataclass
if field_type is bool:
cast_value = str(value).lower() in ['true', '1']
# parse dicts like SANDBOX_RUNTIME_STARTUP_ENV_VARS
elif get_origin(field_type) is dict:
# parse dicts and lists like SANDBOX_RUNTIME_STARTUP_ENV_VARS and SANDBOX_RUNTIME_EXTRA_BUILD_ARGS │
elif (
get_origin(field_type) is dict or get_origin(field_type) is list
):
cast_value = literal_eval(value)
else:
if field_type is not None:

View File

@ -344,6 +344,33 @@ user_id = 1001
assert default_config.sandbox.user_id == 1001
def test_load_from_env_with_list(monkeypatch, default_config):
"""Test loading list values from environment variables, particularly SANDBOX_RUNTIME_EXTRA_BUILD_ARGS."""
# Set the environment variable with a list-formatted string
monkeypatch.setenv(
'SANDBOX_RUNTIME_EXTRA_BUILD_ARGS',
'['
+ ' "--add-host=host.docker.internal:host-gateway",'
+ ' "--build-arg=https_proxy=https://my-proxy:912",'
+ ']',
)
# Load configuration from environment
load_from_env(default_config, os.environ)
# Verify that the list was correctly parsed
assert isinstance(default_config.sandbox.runtime_extra_build_args, list)
assert len(default_config.sandbox.runtime_extra_build_args) == 2
assert (
'--add-host=host.docker.internal:host-gateway'
in default_config.sandbox.runtime_extra_build_args
)
assert (
'--build-arg=https_proxy=https://my-proxy:912'
in default_config.sandbox.runtime_extra_build_args
)
def test_security_config_from_toml(default_config, temp_toml_file):
"""Test loading security specific configurations."""
with open(temp_toml_file, 'w', encoding='utf-8') as toml_file: