tmpdir.py


Overview

The [tmpdir.py](/projects/286/67435) file provides robust support for creating and managing temporary directories for test functions within the pytest testing framework. It primarily defines the `TempPathFactory` class that centralizes temporary directory creation under a common base directory and enforces retention policies for cleanup or preservation of these directories after test execution.

This module integrates deeply with pytest’s fixture and hook systems to:

The file exposes session-scoped and function-scoped fixtures (`tmp_path_factory` and `tmp_path`) to deliver temporary directory paths to test code, while managing lifecycle and cleanup transparently.


Detailed Explanation of Classes, Functions, and Methods

Class: TempPathFactory

A [@dataclass](/projects/286/67337) and [@final](/projects/286/67223) class responsible for creating and managing temporary directories under a shared base temporary directory. It encapsulates directory creation, base directory management, and retention policy enforcement.

Attributes

Methods

__init__
def __init__(
    self,
    given_basetemp: Path | None,
    retention_count: int,
    retention_policy: RetentionType,
    trace,
    basetemp: Path | None = None,
    *,
    _ispytest: bool = False,
) -> None:
from_config
@classmethod
def from_config(cls, config: Config, *, _ispytest: bool = False) -> TempPathFactory:
factory = TempPathFactory.from_config(config)
_ensure_relative_to_basetemp
def _ensure_relative_to_basetemp(self, basename: str) -> str:
mktemp
def mktemp(self, basename: str, numbered: bool = True) -> Path:
temp_dir = factory.mktemp("testdata-", numbered=True)
getbasetemp
def getbasetemp(self) -> Path:

Function: get_user

def get_user() -> str | None:

Pytest Hooks and Fixtures

pytest_configure

def pytest_configure(config: Config) -> None:

pytest_addoption

def pytest_addoption(parser: Parser) -> None:

Fixture: tmp_path_factory

@fixture(scope="session")
def tmp_path_factory(request: FixtureRequest) -> TempPathFactory:

Internal Helper: _mk_tmp

def _mk_tmp(request: FixtureRequest, factory: TempPathFactory) -> Path:

Fixture: tmp_path

@fixture
def tmp_path(request: FixtureRequest, tmp_path_factory: TempPathFactory) -> Generator[Path]:
def test_something(tmp_path):
    file = tmp_path / "data.txt"
    file.write_text("hello")
    assert file.exists()

Hook: pytest_sessionfinish

def pytest_sessionfinish(session, exitstatus: int | ExitCode):

Hook: pytest_runtest_makereport

@hookimpl(wrapper=True, tryfirst=True)
def pytest_runtest_makereport(item: Item, call) -> Generator[None, TestReport, TestReport]:

Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram: Class Structure and Main Methods

classDiagram
    class TempPathFactory {
        -_given_basetemp: Path | None
        -_trace: Any
        -_basetemp: Path | None
        -_retention_count: int
        -_retention_policy: RetentionType
        +__init__(given_basetemp, retention_count, retention_policy, trace, basetemp=None, _ispytest=False)
        +from_config(config, _ispytest=False) TempPathFactory
        -_ensure_relative_to_basetemp(basename) str
        +mktemp(basename, numbered=True) Path
        +getbasetemp() Path
    }

    TempPathFactory ..> "uses" make_numbered_dir
    TempPathFactory ..> "uses" make_numbered_dir_with_cleanup
    TempPathFactory ..> "uses" rm_rf

Summary

The [tmpdir.py](/projects/286/67435) file implements a comprehensive temporary directory management system for pytest tests, providing:

This design ensures reproducible, isolated test environments with minimal manual cleanup, improving test reliability and developer experience.


End of Documentation for tmpdir.py