typing_checks.py


Overview

This file, `typing_checks.py`, serves as a static type-checking utility suite specifically designed to verify the correct typing behavior of various pytest-related constructs and Python typing features using **mypy**. It is **not executed at runtime** but is instead analyzed by **mypy** to ensure no typing errors arise from the usage of pytest fixtures, marks, parameterization, and related utilities.

The primary focus is on confirming the type correctness of pytest fixtures, parameterized tests, monkeypatch operations, context managers, and attributes of pytest objects such as `TestReport`. This aids in validating improvements and fixes related to typing issues documented in various pytest issues (e.g., #7488, #7494, #10999, #12941).


Contents

Imports


Functions

1. check_mark_xfail_raises() -> None

@pytest.mark.xfail(raises=RuntimeError)
def check_mark_xfail_raises() -> None:
    pass

2. check_fixture_ids_callable() -> None

@pytest.fixture(params=[(0, 0), (1, 1)], ids=lambda x: str(x[0]))
def check_fixture_ids_callable() -> None:
    pass

3. check_parametrize_ids_callable(func) -> None

@pytest.mark.parametrize("func", [str, int], ids=lambda x: str(x.__name__))
def check_parametrize_ids_callable(func) -> None:
    pass

4. check_monkeypatch_typeddict(monkeypatch: MonkeyPatch) -> None

def check_monkeypatch_typeddict(monkeypatch: MonkeyPatch) -> None:
    from typing import TypedDict

    class Foo(TypedDict):
        x: int
        y: float

    a: Foo = {"x": 1, "y": 3.14}
    monkeypatch.setitem(a, "x", 2)
    monkeypatch.delitem(a, "y")

5. check_raises_is_a_context_manager(val: bool) -> None

def check_raises_is_a_context_manager(val: bool) -> None:
    with pytest.raises(RuntimeError) if val else contextlib.nullcontext() as excinfo:
        pass
    assert_type(excinfo, Optional[pytest.ExceptionInfo[RuntimeError]])

6. check_testreport_attributes(report: TestReport) -> None

def check_testreport_attributes(report: TestReport) -> None:
    assert_type(report.when, Literal["setup", "call", "teardown"])
    assert_type(report.location, tuple[str, Optional[int], str])

Important Implementation Details


Interaction with Other Parts of the System


Usage Example

Because this file is for static checking only, no runtime usage is expected. However, developers working on test suites or plugin code can refer to this file as an example of how to properly annotate and type pytest constructs with mypy.


Mermaid Diagram

flowchart TD
    A[check_mark_xfail_raises()] -->|pytest.mark.xfail| B[pytest]
    C[check_fixture_ids_callable()] -->|pytest.fixture with params and ids| B
    D[check_parametrize_ids_callable(func)] -->|pytest.mark.parametrize with ids callable| B
    E[check_monkeypatch_typeddict(monkeypatch)] -->|MonkeyPatch operations on TypedDict| B
    F[check_raises_is_a_context_manager(val)] -->|Conditional pytest.raises context manager| B
    G[check_testreport_attributes(report)] -->|Type assertions on TestReport| B

Summary

`typing_checks.py` is a specialized static type check file tailored for pytest-related type validation. It ensures that pytest's advanced features like marks, fixtures, parameterization, monkeypatching, and test report handling conform to expected typing standards, improving reliability and developer experience in the project's test ecosystem.