Home / File/ conftest.py — langchain Source File

conftest.py — langchain Source File

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

File python CoreAbstractions MessageSchema 3 imports 2 functions

Entity Profile

Dependency Diagram

graph LR
  1928451c_662c_79e8_380c_935b94c5f926["conftest.py"]
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  1928451c_662c_79e8_380c_935b94c5f926 --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  3888b2bf_bffe_7c16_770f_a406d400119c["importlib"]
  1928451c_662c_79e8_380c_935b94c5f926 --> 3888b2bf_bffe_7c16_770f_a406d400119c
  120e2591_3e15_b895_72b6_cb26195e40a6["pytest"]
  1928451c_662c_79e8_380c_935b94c5f926 --> 120e2591_3e15_b895_72b6_cb26195e40a6
  style 1928451c_662c_79e8_380c_935b94c5f926 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Configuration for unit tests."""

from collections.abc import Sequence
from importlib import util

import pytest


def pytest_addoption(parser: pytest.Parser) -> None:
    """Add custom command line options to pytest."""
    parser.addoption(
        "--only-extended",
        action="store_true",
        help="Only run extended tests. Does not allow skipping any extended tests.",
    )
    parser.addoption(
        "--only-core",
        action="store_true",
        help="Only run core tests. Never runs any extended tests.",
    )

    parser.addoption(
        "--community",
        action="store_true",
        dest="community",
        default=False,
        help="enable running unite tests that require community",
    )


def pytest_collection_modifyitems(
    config: pytest.Config, items: Sequence[pytest.Function]
) -> None:
    """Add implementations for handling custom markers.

    At the moment, this adds support for a custom `requires` marker.

    The `requires` marker is used to denote tests that require one or more packages
    to be installed to run. If the package is not installed, the test is skipped.

    The `requires` marker syntax is:

    ```python
    @pytest.mark.requires("package1", "package2")
    def test_something(): ...
    ```
    """
    # Mapping from the name of a package to whether it is installed or not.
    # Used to avoid repeated calls to `util.find_spec`
    required_pkgs_info: dict[str, bool] = {}

    only_extended = config.getoption("--only-extended", default=False)
    only_core = config.getoption("--only-core", default=False)

    if not config.getoption("--community", default=False):
        skip_community = pytest.mark.skip(reason="need --community option to run")
        for item in items:
            if "community" in item.keywords:
                item.add_marker(skip_community)

    if only_extended and only_core:
        msg = "Cannot specify both `--only-extended` and `--only-core`."
        raise ValueError(msg)

    for item in items:
        requires_marker = item.get_closest_marker("requires")
        if requires_marker is not None:
            if only_core:
                item.add_marker(pytest.mark.skip(reason="Skipping not a core test."))
                continue

            # Iterate through the list of required packages
            required_pkgs = requires_marker.args
            for pkg in required_pkgs:
                # If we haven't yet checked whether the pkg is installed
                # let's check it and store the result.
                if pkg not in required_pkgs_info:
                    required_pkgs_info[pkg] = util.find_spec(pkg) is not None

                if not required_pkgs_info[pkg]:
                    if only_extended:
                        pytest.fail(
                            f"Package `{pkg}` is not installed but is required for "
                            f"extended tests. Please install the given package and "
                            f"try again.",
                        )

                    else:
                        # If the package is not installed, we immediately break
                        # and mark the test as skipped.
                        item.add_marker(
                            pytest.mark.skip(reason=f"Requires pkg: `{pkg}`"),
                        )
                        break
        elif only_extended:
            item.add_marker(
                pytest.mark.skip(reason="Skipping not an extended test."),
            )

Subdomains

Dependencies

  • collections.abc
  • importlib
  • pytest

Frequently Asked Questions

What does conftest.py do?
conftest.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, MessageSchema subdomain.
What functions are defined in conftest.py?
conftest.py defines 2 function(s): pytest_addoption, pytest_collection_modifyitems.
What does conftest.py depend on?
conftest.py imports 3 module(s): collections.abc, importlib, pytest.
Where is conftest.py in the architecture?
conftest.py is located at libs/langchain/tests/unit_tests/conftest.py (domain: CoreAbstractions, subdomain: MessageSchema, directory: libs/langchain/tests/unit_tests).

Analyze Your Own Codebase

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

Try Supermodel Free