init.py


Overview

This `__init__.py` file is part of the **pytest** assertion rewriting plugin. Its primary purpose is to enable and manage enhanced assertion introspection and debugging during test execution by rewriting Python `assert` statements in test modules. This rewriting allows pytest to provide detailed, user-friendly explanations when assertions fail, significantly improving debugging efficiency.

Key functionalities include:

This file acts as the central coordination point for assertion rewriting and presentation, interfacing closely with pytest’s import system, configuration, and test execution lifecycle.


Classes

RewriteHook (Protocol)

A protocol defining the interface for import hooks that support marking modules for assertion rewriting.

Methods


DummyRewriteHook

A no-operation import hook implementation used when assertion rewriting is disabled or unavailable.

Methods


AssertionState

Represents and tracks the current state of the assertion plugin within a pytest run.

Attributes

Constructor

def __init__(self, config: Config, mode) -> None:
state = AssertionState(config, "rewrite")
print(state.mode)  # Output: "rewrite"

Functions

pytest_addoption(parser: Parser) -> None

Registers pytest CLI options and ini configuration entries related to assertion debugging.


register_assert_rewrite(*names: str) -> None

Registers one or more module names to have their `assert` statements rewritten on import.

register_assert_rewrite("myplugin.assert_helpers", "myplugin.submodule")

Typically called in a plugin’s `__init__.py` to ensure rewriting before import.


install_importhook(config: Config) -> rewrite.AssertionRewritingHook

Installs the assertion rewriting import hook into Python’s import machinery.


pytest_collection(session: Session) -> None

Pytest hook called during test collection.


pytest_runtest_protocol(item: Item) -> Generator[None, object, object]

Pytest hook wrapping the test run protocol.


pytest_sessionfinish(session: Session) -> None

Pytest hook called at the end of a test session.


pytest_assertrepr_compare(config: Config, op: str, left: Any, right: Any) -> list[str] | None

Pytest hook to provide detailed assertion failure explanations for comparison expressions.


Implementation Details & Algorithms


Interaction with Other System Components


Usage Summary

A typical plugin or package wanting to use assertion rewriting would:

  1. Call register_assert_rewrite("your_module") in its __init__.py.

  2. Rely on pytest to install the import hook automatically.

  3. Optionally customize assertion reporting using pytest hooks.


Visual Diagram

This diagram illustrates the major classes and functions and their relationships within this file, focusing on assertion rewriting and hook integration.

classDiagram
    class AssertionState {
        -mode: str
        -trace: Any
        -hook: AssertionRewritingHook | None
        +__init__(config: Config, mode)
    }
    class DummyRewriteHook {
        +mark_rewrite(*names: str)
    }
    class RewriteHook {
        <<Protocol>>
        +mark_rewrite(*names: str)
    }
    class AssertionRewritingHook {
        <<from _pytest.assertion.rewrite>>
    }
    class Config {
        <<from _pytest.config>>
    }
    class Parser {
        <<from _pytest.config.argparsing>>
    }
    class Item {
        <<from _pytest.nodes>>
    }
    class Session {
        <<from _pytest.main>>
    }

    %% Functions
    class pytest_addoption {
        +pytest_addoption(parser: Parser)
    }
    class register_assert_rewrite {
        +register_assert_rewrite(*names: str)
    }
    class install_importhook {
        +install_importhook(config: Config)
    }
    class pytest_collection {
        +pytest_collection(session: Session)
    }
    class pytest_runtest_protocol {
        +pytest_runtest_protocol(item: Item)
    }
    class pytest_sessionfinish {
        +pytest_sessionfinish(session: Session)
    }
    class pytest_assertrepr_compare {
        +pytest_assertrepr_compare(config: Config, op: str, left: Any, right: Any)
    }

    %% Relationships
    AssertionState --> Config
    AssertionState --> AssertionRewritingHook
    register_assert_rewrite ..|> RewriteHook
    install_importhook --> AssertionState
    install_importhook --> AssertionRewritingHook
    pytest_collection --> AssertionState
    pytest_runtest_protocol --> Item
    pytest_sessionfinish --> AssertionState
    pytest_assertrepr_compare --> Config

Summary

This `__init__.py` file is central to pytest's assertion rewriting mechanism, enabling detailed assertion failure reporting by rewriting the source code of test modules during import. It manages command-line options, installs import hooks, maintains plugin state, and integrates deeply with pytest’s lifecycle hooks to provide rich assertion debugging capabilities. Understanding this file is critical for extending or customizing pytest’s assertion introspection behavior.