util.py


Overview

The [util.py](/projects/287/67676) file serves as a utility module primarily focused on handling test fixture data for the project. It provides functions to read fixture files from disk in various formats (raw bytes, decoded strings, and deserialized JSON objects), caching the results to optimize repeated access during testing. Additionally, it includes conditional imports to support optional dependencies (`numpy` and `pandas`) depending on Python interpreter configuration, and a Pytest marker for tests that require fixture data.

This file plays a supportive role in the testing infrastructure by simplifying fixture management, improving test performance through caching, and ensuring compatibility with different Python runtime settings.


Detailed Description of Functions and Variables

Global Variables


Functions

read_fixture_bytes(filename: str, subdir: str | None = None) -> bytes

Reads the raw bytes from a fixture file.


read_fixture_str(filename: str, subdir: str | None = None) -> str

Reads the contents of a fixture file as a UTF-8 decoded string, with caching.


read_fixture_obj(filename: str) -> Any

Loads and returns a parsed JSON object from a fixture file, with caching.


Pytest Marker


Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram: Flowchart of Utility Functions and Their Relationships

flowchart TD
    A[read_fixture_bytes(filename, subdir)] -->|returns bytes| B[read_fixture_str(filename, subdir)]
    B -->|caches decoded str| C[STR_CACHE]
    B -->|returns str| D[read_fixture_obj(filename)]
    D -->|parses JSON with orjson.loads| E[OBJ_CACHE]
    D -->|returns object| F[Tests]

    subgraph "Cache Layers"
        C
        E
    end

    style C fill:#f9f,stroke:#333,stroke-width:1px
    style E fill:#f9f,stroke:#333,stroke-width:1px

Summary

[util.py](/projects/287/67676) is a focused utility module that simplifies loading test fixture data in raw bytes, string, or JSON object form with efficient caching and support for compressed files. It conditionally manages optional dependencies and provides a Pytest marker to handle test skipping when fixtures are unavailable. This module enhances the testing framework's robustness and performance by centralizing fixture handling logic.