legacypath.py

Overview

The [legacypath.py](/projects/286/67505) module provides backward compatibility support for the legacy `py.path.local` path type within the `pytest` testing framework ecosystem. It wraps modern `pathlib.Path` based implementations and exposes legacy-style path objects (`LEGACY_PATH`) where needed, ensuring older plugins and test code that rely on the legacy path API continue to work without modification.

This compatibility layer is essential during the transition from legacy `py.path` to `pathlib.Path` in `pytest`. It offers legacy wrappers for core test directory management, temporary directory factories, and common pytest components (e.g., `Config`, `Session`, `Node`) by monkeypatching properties and methods to return `LEGACY_PATH` types.

The file mainly defines:


Detailed API Documentation

Constants & Types


Class: Testdir

A backward compatibility wrapper around the modern `Pytester` class, adapting all method calls and path-related properties to return legacy `LEGACY_PATH` objects.

Purpose

To provide a legacy-compatible test directory helper for tests or plugins that expect the old `py.path.local` style API.

Attributes

Properties

Methods

All methods forward to the internal `Pytester` instance, converting paths to legacy paths as needed.

Usage Example

def test_example(testdir):
    # testdir is an instance of Testdir, returns legacy paths
    test_file = testdir.makepyfile("def test_foo(): assert True")
    result = testdir.runpytest(test_file)
    assert result.ret == 0

Class: LegacyTestdirPlugin

A pytest plugin that provides a `testdir` fixture, which returns a `Testdir` instance wrapping a `Pytester`. This fixture is intended for backward compatibility.

**Note:** New tests should prefer the `pytester` fixture instead.


Class: TempdirFactory

A backward compatibility dataclass wrapping `TempPathFactory` to provide legacy `py.path.local` style temporary directories.

Attributes

Methods

Usage Example

def test_tempdir_factory(tmpdir_factory):
    tempdir = tmpdir_factory.mktemp("data")
    assert tempdir.check(dir=1)  # legacy py.path method

Class: LegacyTmpdirPlugin

A pytest plugin providing legacy fixtures:


Monkeypatched Methods / Properties

The module monkeypatches several pytest core classes to add legacy path properties or methods:


Pytest Hooks


Implementation Details & Algorithms


Interaction with Other Parts of the System


Mermaid Diagram

classDiagram
    class Testdir {
        -_pytester: Pytester
        +tmpdir: LEGACY_PATH
        +test_tmproot: LEGACY_PATH
        +request
        +plugins
        +monkeypatch: MonkeyPatch
        +make_hook_recorder(pluginmanager) HookRecorder
        +chdir() void
        +finalize() void
        +makefile(ext, *args, **kwargs) LEGACY_PATH
        +makeconftest(source) LEGACY_PATH
        +makeini(source) LEGACY_PATH
        +getinicfg(source) SectionWrapper
        +makepyprojecttoml(source) LEGACY_PATH
        +makepyfile(*args, **kwargs) LEGACY_PATH
        +maketxtfile(*args, **kwargs) LEGACY_PATH
        +syspathinsert(path=None) void
        +mkdir(name) LEGACY_PATH
        +mkpydir(name) LEGACY_PATH
        +copy_example(name=None) LEGACY_PATH
        +getnode(config, arg) Item|Collector|None
        +getpathnode(path)
        +genitems(colitems) list~Item~
        +runitem(source)
        +inline_runsource(source, *cmdlineargs)
        +inline_genitems(*args)
        +inline_run(*args, plugins=(), no_reraise_ctrlc=False)
        +runpytest_inprocess(*args, **kwargs) RunResult
        +runpytest(*args, **kwargs) RunResult
        +parseconfig(*args) Config
        +parseconfigure(*args) Config
        +getitem(source, funcname="test_func")
        +getitems(source)
        +getmodulecol(source, configargs=(), withinit=False)
        +collect_by_name(modcol, name) Item|Collector|None
        +popen(cmdargs, stdout, stderr, stdin, **kw)
        +run(*cmdargs, timeout=None, stdin)
        +runpython(script) RunResult
        +runpython_c(command)
        +runpytest_subprocess(*args, timeout=None) RunResult
        +spawn_pytest(string, expect_timeout=10.0)
        +spawn(cmd, expect_timeout=10.0)
    }

    class TempdirFactory {
        -_tmppath_factory: TempPathFactory
        +mktemp(basename, numbered=True) LEGACY_PATH
        +getbasetemp() LEGACY_PATH
    }

    class LegacyTestdirPlugin {
        +testdir(pytester) Testdir
    }

    class LegacyTmpdirPlugin {
        +tmpdir_factory(request) TempdirFactory
        +tmpdir(tmp_path) LEGACY_PATH
    }

    Testdir --> Pytester : wraps
    TempdirFactory --> TempPathFactory : wraps
    LegacyTestdirPlugin ..> Testdir : provides fixture
    LegacyTmpdirPlugin ..> TempdirFactory : provides fixture
    LegacyTmpdirPlugin ..> LEGACY_PATH : provides fixture

Summary

[legacypath.py](/projects/286/67505) is a critical backward compatibility module in `pytest` that ensures legacy `py.path.local` based plugins and tests continue to function seamlessly while the internal implementations move towards `pathlib.Path`. It achieves this via wrapper classes, monkeypatching core classes, and providing legacy fixtures. It is designed as a temporary bridge to ease the migration towards modern Python path handling within the pytest ecosystem.