Home / File/ test_utils.py — langchain Source File

test_utils.py — langchain Source File

Architecture documentation for test_utils.py, a python file in the langchain codebase. 5 imports, 0 dependents.

File python CoreAbstractions Serialization 5 imports 3 functions

Entity Profile

Dependency Diagram

graph LR
  9e8325c0_492c_c4ce_b2ab_6badc4ca9e70["test_utils.py"]
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  9e8325c0_492c_c4ce_b2ab_6badc4ca9e70 --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  9e8325c0_492c_c4ce_b2ab_6badc4ca9e70 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  120e2591_3e15_b895_72b6_cb26195e40a6["pytest"]
  9e8325c0_492c_c4ce_b2ab_6badc4ca9e70 --> 120e2591_3e15_b895_72b6_cb26195e40a6
  c764ccae_0d75_abec_7c23_6d5d1949a7ba["langchain_core.runnables.base"]
  9e8325c0_492c_c4ce_b2ab_6badc4ca9e70 --> c764ccae_0d75_abec_7c23_6d5d1949a7ba
  81c04601_d095_a27d_4af1_55e771bb2b6b["langchain_core.runnables.utils"]
  9e8325c0_492c_c4ce_b2ab_6badc4ca9e70 --> 81c04601_d095_a27d_4af1_55e771bb2b6b
  style 9e8325c0_492c_c4ce_b2ab_6badc4ca9e70 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from collections.abc import Callable
from typing import Any

import pytest

from langchain_core.runnables.base import RunnableLambda
from langchain_core.runnables.utils import (
    get_function_nonlocals,
    get_lambda_source,
    indent_lines_after_first,
)


@pytest.mark.parametrize(
    ("func", "expected_source"),
    [
        (lambda x: x * 2, "lambda x: x * 2"),
        (lambda a, b: a + b, "lambda a, b: a + b"),
        (lambda x: x if x > 0 else 0, "lambda x: x if x > 0 else 0"),  # noqa: FURB136
    ],
)
def test_get_lambda_source(func: Callable[..., Any], expected_source: str) -> None:
    """Test get_lambda_source function."""
    source = get_lambda_source(func)
    assert source == expected_source


@pytest.mark.parametrize(
    ("text", "prefix", "expected_output"),
    [
        ("line 1\nline 2\nline 3", "1", "line 1\n line 2\n line 3"),
        ("line 1\nline 2\nline 3", "ax", "line 1\n  line 2\n  line 3"),
    ],
)
def test_indent_lines_after_first(text: str, prefix: str, expected_output: str) -> None:
    """Test indent_lines_after_first function."""
    indented_text = indent_lines_after_first(text, prefix)
    assert indented_text == expected_output


global_agent = RunnableLambda[str, str](lambda x: x * 3)


def test_nonlocals() -> None:
    agent = RunnableLambda[str, str](lambda x: x * 2)

    def my_func(value: str, agent: dict[str, str]) -> str:
        return agent.get("agent_name", value)

    def my_func2(value: str) -> str:
        return str(agent.get("agent_name", value))  # type: ignore[attr-defined]

    def my_func3(value: str) -> str:
        return agent.invoke(value)

    def my_func4(value: str) -> str:
        return global_agent.invoke(value)

    def my_func5() -> tuple[Callable[[str], str], RunnableLambda]:
        global_agent = RunnableLambda[str, str](lambda x: x * 3)

        def my_func6(value: str) -> str:
            return global_agent.invoke(value)

        return my_func6, global_agent

    assert get_function_nonlocals(my_func) == []
    assert get_function_nonlocals(my_func2) == []
    assert get_function_nonlocals(my_func3) == [agent.invoke]
    assert get_function_nonlocals(my_func4) == [global_agent.invoke]
    func, nl = my_func5()
    assert get_function_nonlocals(func) == [nl.invoke]
    assert RunnableLambda(my_func3).deps == [agent]
    assert RunnableLambda(my_func4).deps == [global_agent]
    assert RunnableLambda(func).deps == [nl]

Subdomains

Dependencies

  • collections.abc
  • langchain_core.runnables.base
  • langchain_core.runnables.utils
  • pytest
  • typing

Frequently Asked Questions

What does test_utils.py do?
test_utils.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, Serialization subdomain.
What functions are defined in test_utils.py?
test_utils.py defines 3 function(s): test_get_lambda_source, test_indent_lines_after_first, test_nonlocals.
What does test_utils.py depend on?
test_utils.py imports 5 module(s): collections.abc, langchain_core.runnables.base, langchain_core.runnables.utils, pytest, typing.
Where is test_utils.py in the architecture?
test_utils.py is located at libs/core/tests/unit_tests/runnables/test_utils.py (domain: CoreAbstractions, subdomain: Serialization, directory: libs/core/tests/unit_tests/runnables).

Analyze Your Own Codebase

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

Try Supermodel Free