Home / Function/ pytest_collection_modifyitems() — langchain Function Reference

pytest_collection_modifyitems() — langchain Function Reference

Architecture documentation for the pytest_collection_modifyitems() function in conftest.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  b816a0d1_507f_3098_36c2_7a4cc9d402fd["pytest_collection_modifyitems()"]
  e7f28879_d9b0_59eb_c106_fe5bbec0e649["conftest.py"]
  b816a0d1_507f_3098_36c2_7a4cc9d402fd -->|defined in| e7f28879_d9b0_59eb_c106_fe5bbec0e649
  style b816a0d1_507f_3098_36c2_7a4cc9d402fd fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/tests/unit_tests/conftest.py lines 52–115

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") or False
    only_core = config.getoption("--only-core") or False

    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:
                    try:
                        installed = util.find_spec(pkg) is not None
                    except Exception:
                        installed = False
                    required_pkgs_info[pkg] = installed

                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

Frequently Asked Questions

What does pytest_collection_modifyitems() do?
pytest_collection_modifyitems() is a function in the langchain codebase, defined in libs/core/tests/unit_tests/conftest.py.
Where is pytest_collection_modifyitems() defined?
pytest_collection_modifyitems() is defined in libs/core/tests/unit_tests/conftest.py at line 52.

Analyze Your Own Codebase

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

Try Supermodel Free