core[minor]: Support multiple keys in get_from_dict_or_env (#23086)

Support passing multiple keys for ge_from_dict_or_env
This commit is contained in:
Eugene Yurtsev
2024-06-18 14:13:28 -04:00
committed by GitHub
parent 226802f0c4
commit aa6415aa7d
2 changed files with 94 additions and 6 deletions

View File

@@ -0,0 +1,64 @@
import pytest
from langchain_core.utils.env import get_from_dict_or_env
def test_get_from_dict_or_env() -> None:
assert (
get_from_dict_or_env(
{
"a": "foo",
},
["a"],
"__SOME_KEY_IN_ENV",
)
== "foo"
)
assert (
get_from_dict_or_env(
{
"a": "foo",
},
["b", "a"],
"__SOME_KEY_IN_ENV",
)
== "foo"
)
assert (
get_from_dict_or_env(
{
"a": "foo",
},
"a",
"__SOME_KEY_IN_ENV",
)
== "foo"
)
assert (
get_from_dict_or_env(
{
"a": "foo",
},
"not exists",
"__SOME_KEY_IN_ENV",
default="default",
)
== "default"
)
# Not the most obvious behavior, but
# this is how it works right now
with pytest.raises(ValueError):
assert (
get_from_dict_or_env(
{
"a": "foo",
},
"not exists",
"__SOME_KEY_IN_ENV",
)
is None
)