Home / Function/ test_using_secret_from_env_as_default_factory() — langchain Function Reference

test_using_secret_from_env_as_default_factory() — langchain Function Reference

Architecture documentation for the test_using_secret_from_env_as_default_factory() function in test_utils.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  0cface2d_4392_63a9_bdb5_972c13ce9d01["test_using_secret_from_env_as_default_factory()"]
  76328b48_c466_d255_3e8f_a315858c4c02["test_utils.py"]
  0cface2d_4392_63a9_bdb5_972c13ce9d01 -->|defined in| 76328b48_c466_d255_3e8f_a315858c4c02
  style 0cface2d_4392_63a9_bdb5_972c13ce9d01 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/tests/unit_tests/utils/test_utils.py lines 346–381

def test_using_secret_from_env_as_default_factory(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    class Foo(BaseModel):
        secret: SecretStr = Field(default_factory=secret_from_env("TEST_KEY"))

    # Pass the secret as a parameter
    foo = Foo(secret="super_secret")
    assert foo.secret.get_secret_value() == "super_secret"

    # Set the environment variable
    monkeypatch.setenv("TEST_KEY", "secret_value")
    assert Foo().secret.get_secret_value() == "secret_value"

    class Bar(BaseModel):
        secret: SecretStr | None = Field(
            default_factory=secret_from_env("TEST_KEY_2", default=None)
        )

    assert Bar().secret is None

    class Buzz(BaseModel):
        secret: SecretStr | None = Field(
            default_factory=secret_from_env("TEST_KEY_2", default="hello")
        )

    # We know it will be SecretStr rather than SecretStr | None
    assert Buzz().secret.get_secret_value() == "hello"  # type: ignore[union-attr]

    class OhMy(BaseModel):
        secret: SecretStr | None = Field(
            default_factory=secret_from_env("FOOFOOFOOBAR")
        )

    with pytest.raises(ValueError, match="Did not find FOOFOOFOOBAR"):
        OhMy()

Domain

Subdomains

Frequently Asked Questions

What does test_using_secret_from_env_as_default_factory() do?
test_using_secret_from_env_as_default_factory() is a function in the langchain codebase, defined in libs/core/tests/unit_tests/utils/test_utils.py.
Where is test_using_secret_from_env_as_default_factory() defined?
test_using_secret_from_env_as_default_factory() is defined in libs/core/tests/unit_tests/utils/test_utils.py at line 346.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free