findpaths.py


Overview

The [findpaths.py](/projects/286/67504) module provides utility functions for locating and parsing pytest configuration files within a filesystem hierarchy. It primarily deals with discovering configuration files such as `pytest.ini`, `setup.cfg`, `tox.ini`, and `pyproject.toml`, parsing their contents into a standardized dictionary format, and determining the root directory for pytest runs based on command-line arguments and filesystem layout.

Key functionalities include:

This module is a foundational piece to support pytest's ability to configure itself flexibly depending on project layout and user invocation.


Detailed Explanation of Functions and Methods

_parse_ini_config(path: Path) -> iniconfig.IniConfig

Parses a generic `.ini` configuration file using the legacy [IniConfig](/projects/286/67332) parser from the `iniconfig` library.


load_config_dict_from_file(filepath: Path) -> ConfigDict | None

Loads pytest configuration from a given file path if it contains valid pytest settings.


locate_config(invocation_dir: Path, args: Iterable[Path]) -> tuple[Path | None, Path | None, ConfigDict]

Searches for a valid pytest configuration file based on a list of argument paths and returns its root directory, config file path, and configuration dictionary.


get_common_ancestor(invocation_dir: Path, paths: Iterable[Path]) -> Path

Determines the common ancestor directory of a set of filesystem paths.


get_dirs_from_args(args: Iterable[str]) -> list[Path]

Extracts directories from command-line arguments, handling both file paths and node IDs (pytest test specifiers).


determine_setup(*, inifile: str | None, args: Sequence[str], rootdir_cmd_arg: str | None, invocation_dir: Path) -> tuple[Path, Path | None, ConfigDict]

Determines the pytest root directory, configuration file path, and configuration dictionary based on command-line arguments and invocation context.


is_fs_root(p: Path) -> bool

Checks if the given path points to the root of the filesystem.


Important Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram of Main Functions and Relationships

flowchart TD
    A[determine_setup] --> B[get_dirs_from_args]
    A --> C[get_common_ancestor]
    A --> D[locate_config]
    D --> E[load_config_dict_from_file]
    E --> F[_parse_ini_config]
    E --> G[toml parsing logic]
    C --> H[commonpath utility]
    B --> I[safe_exists]
    A -.-> J[is_fs_root]
    E -.-> K[fail on deprecated config]
    style A fill:#f9f,stroke:#333,stroke-width:2px
    style D fill:#bbf,stroke:#333,stroke-width:1px
    style E fill:#bbf,stroke:#333,stroke-width:1px
    style C fill:#bbf,stroke:#333,stroke-width:1px
    style B fill:#bbf,stroke:#333,stroke-width:1px

Summary

[findpaths.py](/projects/286/67504) is a utility module crucial for pytest's configuration discovery and setup. It encapsulates the logic for searching, parsing, and validating pytest configuration files across multiple formats and locations. By integrating with pytest's CLI parsing and filesystem utilities, it determines the project root and configuration context, enabling pytest to run tests consistently in diverse project layouts.

This module ensures backward compatibility and enforces configuration correctness, facilitating a smooth user experience when configuring pytest runs.