core[minor]: Add support for multiple env keys for secrets_from_env (#25971)

- Add support to look up secret using more than one env variable
- Add overload to help mypy

Needed for https://github.com/langchain-ai/langchain/pull/25491
This commit is contained in:
Eugene Yurtsev 2024-09-03 11:39:54 -04:00 committed by GitHub
parent fdeaff4149
commit fa8402ea09
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -302,7 +302,9 @@ def from_env(
@overload
def from_env(key: str, /, *, default: None) -> Callable[[], Optional[str]]: ...
def from_env(
key: Union[str, Sequence[str]], /, *, default: None
) -> Callable[[], Optional[str]]: ...
def from_env(
@ -360,7 +362,7 @@ def secret_from_env(key: str, /, *, default: str) -> Callable[[], SecretStr]: ..
@overload
def secret_from_env(
key: str, /, *, default: None
key: Union[str, Sequence[str]], /, *, default: None
) -> Callable[[], Optional[SecretStr]]: ...
@ -369,7 +371,7 @@ def secret_from_env(key: str, /, *, error_message: str) -> Callable[[], SecretSt
def secret_from_env(
key: str,
key: Union[str, Sequence[str]],
/,
*,
default: Union[str, _NoDefaultType, None] = _NoDefault,
@ -390,9 +392,14 @@ def secret_from_env(
def get_secret_from_env() -> Optional[SecretStr]:
"""Get a value from an environment variable."""
if key in os.environ:
return SecretStr(os.environ[key])
elif isinstance(default, str):
if isinstance(key, (list, tuple)):
for k in key:
if k in os.environ:
return SecretStr(os.environ[k])
if isinstance(key, str):
if key in os.environ:
return SecretStr(os.environ[key])
if isinstance(default, str):
return SecretStr(default)
elif isinstance(default, type(None)):
return None