Home / File/ test_parser.py — langchain Source File

test_parser.py — langchain Source File

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

File python LangChainCore MessageInterface 5 imports 15 functions

Entity Profile

Dependency Diagram

graph LR
  064e0ca3_1663_b885_96b9_052c91c4e115["test_parser.py"]
  feec1ec4_6917_867b_d228_b134d0ff8099["typing"]
  064e0ca3_1663_b885_96b9_052c91c4e115 --> feec1ec4_6917_867b_d228_b134d0ff8099
  5619f221_a580_f72e_3e32_071432cc276c["lark"]
  064e0ca3_1663_b885_96b9_052c91c4e115 --> 5619f221_a580_f72e_3e32_071432cc276c
  f69d6389_263d_68a4_7fbf_f14c0602a9ba["pytest"]
  064e0ca3_1663_b885_96b9_052c91c4e115 --> f69d6389_263d_68a4_7fbf_f14c0602a9ba
  c39a1380_2ee7_581e_4839_31617170612b["langchain_core.structured_query"]
  064e0ca3_1663_b885_96b9_052c91c4e115 --> c39a1380_2ee7_581e_4839_31617170612b
  2ba272aa_42ba_77d4_e469_8f11b62ae859["langchain_classic.chains.query_constructor.parser"]
  064e0ca3_1663_b885_96b9_052c91c4e115 --> 2ba272aa_42ba_77d4_e469_8f11b62ae859
  style 064e0ca3_1663_b885_96b9_052c91c4e115 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Test LLM-generated structured query parsing."""

from typing import Any, cast

import lark
import pytest
from langchain_core.structured_query import (
    Comparator,
    Comparison,
    Operation,
    Operator,
)

from langchain_classic.chains.query_constructor.parser import get_parser

DEFAULT_PARSER = get_parser()


@pytest.mark.parametrize("x", ["", "foo", 'foo("bar", "baz")'])
def test_parse_invalid_grammar(x: str) -> None:
    with pytest.raises((ValueError, lark.exceptions.UnexpectedToken)):
        DEFAULT_PARSER.parse(x)


def test_parse_comparison() -> None:
    comp = 'gte("foo", 2)'
    expected = Comparison(comparator=Comparator.GTE, attribute="foo", value=2)
    for text in (
        comp,
        comp.replace('"', "'"),
        comp.replace(" ", ""),
        comp.replace(" ", "  "),
        comp.replace("(", " ("),
        comp.replace(",", ", "),
        comp.replace("2", "2.0"),
    ):
        actual = DEFAULT_PARSER.parse(text)
        assert expected == actual


def test_parse_operation() -> None:
    op = 'and(eq("foo", "bar"), lt("baz", 1995.25))'
    eq = Comparison(comparator=Comparator.EQ, attribute="foo", value="bar")
    lt = Comparison(comparator=Comparator.LT, attribute="baz", value=1995.25)
    expected = Operation(operator=Operator.AND, arguments=[eq, lt])
    for text in (
        op,
        op.replace('"', "'"),
        op.replace(" ", ""),
        op.replace(" ", "  "),
        op.replace("(", " ("),
        op.replace(",", ", "),
        op.replace("25", "250"),
    ):
        actual = DEFAULT_PARSER.parse(text)
        assert expected == actual


def test_parse_nested_operation() -> None:
    op = 'and(or(eq("a", "b"), eq("a", "c"), eq("a", "d")), not(eq("z", "foo")))'
// ... (93 more lines)

Domain

Subdomains

Dependencies

  • langchain_classic.chains.query_constructor.parser
  • langchain_core.structured_query
  • lark
  • pytest
  • typing

Frequently Asked Questions

What does test_parser.py do?
test_parser.py is a source file in the langchain codebase, written in python. It belongs to the LangChainCore domain, MessageInterface subdomain.
What functions are defined in test_parser.py?
test_parser.py defines 15 function(s): _test_parse_value, test_parse_bool_value, test_parse_comparison, test_parse_date_value, test_parse_datetime_value, test_parse_disallowed_comparator, test_parse_disallowed_operator, test_parse_float_value, test_parse_int_value, test_parse_invalid_grammar, and 5 more.
What does test_parser.py depend on?
test_parser.py imports 5 module(s): langchain_classic.chains.query_constructor.parser, langchain_core.structured_query, lark, pytest, typing.
Where is test_parser.py in the architecture?
test_parser.py is located at libs/langchain/tests/unit_tests/chains/query_constructor/test_parser.py (domain: LangChainCore, subdomain: MessageInterface, directory: libs/langchain/tests/unit_tests/chains/query_constructor).

Analyze Your Own Codebase

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

Try Supermodel Free