test_compat.py


Overview

`test_compat.py` is a test suite designed to validate utility functions and compatibility helpers primarily related to function unwrapping, safe attribute access, class type checks, cached properties, and exhaustive type checking assertions. The file contains multiple test cases that ensure the correctness and robustness of these utilities, many of which are imported from the `_pytest.compat` module. The tests cover scenarios such as handling decorator wrappers, partial functions, exception safety in attribute access, verifying `cached_property` behavior, and asserting unreachable code paths with `assert_never`.

This file plays a critical role in verifying foundational helper functions that are leveraged across the pytest codebase to maintain compatibility and correctness in complex runtime scenarios, such as decorator chains and safe introspection.


Detailed Explanation of Components

Functions


test_real_func_loop_limit() -> None


test_get_real_func() -> None


test_get_real_func_partial() -> None


test_helper_failures() -> None


test_safe_getattr() -> None


test_safe_isclass() -> None


test_cached_property() -> None


test_assert_never_union() -> None


test_assert_never_enum() -> None


test_assert_never_literal() -> None


Class


ErrorsHelper


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram: Function Relationships Flowchart

flowchart TD
    A[get_real_func] -->|unwraps| B[decorator chain]
    A -->|unwraps| C[functools.partial]
    D[test_real_func_loop_limit] --> A
    E[test_get_real_func] --> A
    F[test_get_real_func_partial] --> A

    G[safe_getattr] --> H[ErrorsHelper properties]
    I[test_safe_getattr] --> G
    I --> H

    J[safe_isclass] --> K[Class or Instance]
    L[test_safe_isclass] --> J
    L --> K

    M[assert_never] --> N[Union, Enum, Literal]
    O[test_assert_never_union] --> M
    P[test_assert_never_enum] --> M
    Q[test_assert_never_literal] --> M

    R[cached_property] --> S[Class property caching]
    T[test_cached_property] --> R

    U[ErrorsHelper] --> H
    V[test_helper_failures] --> H

Summary

`test_compat.py` is a comprehensive test module validating critical compatibility utilities in the pytest framework. It covers safe function unwrapping, handling of partial functions, safe attribute access amidst exceptions, proper class detection, cached property behavior, and exhaustive type checking assertions. The file ensures these utilities perform reliably under edge cases such as infinite decorator loops, unusual class implementations, and various exception types. This testing is vital for the robustness of pytest's internal compatibility layer, influencing many areas of the testing framework.