approx.py


Overview

This file provides extensive **tests** and utility helpers for the `pytest.approx` functionality, which is part of the `pytest` testing framework. The `approx` function is used for approximate numerical comparisons in tests, allowing floating-point numbers (and more complex data structures containing them) to be compared within specified tolerances (relative and absolute).

The file primarily contains:

This file does **not** implement the `approx` functionality itself but rigorously tests it, ensuring correctness, helpful error reporting, and compatibility with many data types and edge cases.


Classes and Functions

Fixtures and Context Managers

mocked_doctest_runner(monkeypatch) -> MyDocTestRunner


temporary_verbosity(config, verbosity=0)


assert_approx_raises_regex(pytestconfig) -> Callable


Classes

TestApprox


MyVec3


TestRecursiveSequenceMap


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Usage Examples

import pytest

def test_my_computation():
    result = 0.1 + 0.2
    expected = 0.3
    # Use approx to allow small floating-point errors
    assert result == pytest.approx(expected, rel=1e-6)

def test_list_approx():
    actual = [1.000001, 2.000002]
    expected = [1.0, 2.0]
    assert actual == pytest.approx(expected, abs=1e-5)

def test_dict_approx():
    actual = {"a": 1.0001, "b": 2.0001}
    expected = {"a": 1.0, "b": 2.0}
    assert actual == pytest.approx(expected, rel=1e-4)

Mermaid Diagram: Class Structure

This file mainly contains test classes and one utility class used in tests.

classDiagram
    class TestApprox {
        +test_error_messages_native_dtypes()
        +test_error_messages_numpy_dtypes()
        +test_operator_overloading()
        +test_exactly_equal()
        +test_custom_tolerances()
        +test_numpy_array_protocol()
        +test_doctests()
        +test_unicode_plus_minus()
        +test_nonnumeric_okay_if_equal()
        +test_nonnumeric_false_if_unequal()
        +test_comparison_operator_type_error()
        +test_generic_ordered_sequence()
        +test_decimal_approx_repr()
        +test_allow_ordered_sequences_only()
        +test_strange_sequence()
    }

    class MyVec3 {
        -_x: int
        -_y: int
        -_z: int
        +__init__(x: int, y: int, z: int)
        +__repr__() -> str
        +__len__() -> int
        +__getitem__(key: int) -> int
    }

    class TestRecursiveSequenceMap {
        +test_map_over_scalar()
        +test_map_over_empty_list()
        +test_map_over_list()
        +test_map_over_tuple()
        +test_map_over_nested_lists()
        +test_map_over_mixed_sequence()
        +test_map_over_sequence_like()
    }

Summary


End of approx.py Documentation