From 3606ca87d558731d2d6af8cdc4f28c37841b0e84 Mon Sep 17 00:00:00 2001 From: rbrugaro Date: Thu, 8 May 2025 13:03:23 -0700 Subject: [PATCH] [bug] add list support in setting attributes from env variables (#8295) --- openhands/core/config/utils.py | 6 ++++-- tests/unit/test_config.py | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/openhands/core/config/utils.py b/openhands/core/config/utils.py index 4882c0286c..f90525585d 100644 --- a/openhands/core/config/utils.py +++ b/openhands/core/config/utils.py @@ -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: diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py index 0f96b95c94..ef77396b60 100644 --- a/tests/unit/test_config.py +++ b/tests/unit/test_config.py @@ -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: