test_numpy.py


Overview

`test_numpy.py` is a comprehensive test suite designed to validate the integration and correct serialization of NumPy data types using the `orjson` JSON library. The file primarily ensures that various NumPy arrays and scalar types—across multiple dtypes and dimensions—are serialized correctly to JSON format when using the `orjson.OPT_SERIALIZE_NUMPY` option.

The tests cover:

This ensures that `orjson` correctly supports NumPy serialization in all expected edge cases, providing robust JSON interoperability for applications using NumPy data structures.


File Contents and Detailed Explanation

Imports

Function: numpy_default(obj)

def numpy_default(obj):
    if isinstance(obj, numpy.ndarray):
        return obj.tolist()
    raise TypeError

Class: TestNumpy

Decorated with `@pytest.mark.skipif(numpy is None, reason="numpy is not installed")` to skip all tests if NumPy is unavailable.

Contains numerous test methods that validate serialization of different NumPy types and structures using `orjson.dumps` with `option=orjson.OPT_SERIALIZE_NUMPY`.

Highlights of test methods:

**Usage Example of a test method:**

def test_numpy_array_d1_i8(self):
    arr = numpy.array([-128, 127], numpy.int8)
    serialized = orjson.dumps(arr, option=orjson.OPT_SERIALIZE_NUMPY)
    assert serialized == b"[-128,127]"

Class: TestNumpyEquivalence

Also skipped if NumPy is not installed.


Class: NumpyEndianness


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram

The file mainly consists of three test classes and one utility function. The diagram below shows their structure and relationships.

classDiagram
    class numpy_default {
        +obj: object
        +return: list or TypeError
    }

    class TestNumpy {
        +test_numpy_array_d1_uintp()
        +test_numpy_array_d1_intp()
        +test_numpy_array_d1_i64()
        +test_numpy_array_d1_u64()
        +test_numpy_array_d1_i8()
        +test_numpy_array_d1_u8()
        +test_numpy_array_d1_i32()
        +test_numpy_array_d1_i16()
        +test_numpy_array_d1_u16()
        +test_numpy_array_d1_u32()
        +test_numpy_array_d1_f32()
        +test_numpy_array_d1_f16()
        +test_numpy_array_f16_roundtrip()
        +test_numpy_array_f16_edge()
        +test_numpy_array_f32_edge()
        +test_numpy_array_f64_edge()
        +test_numpy_array_d1_bool()
        +test_numpy_array_d1_datetime64_years()
        +test_numpy_array_d1_datetime64_months()
        +test_numpy_array_d1_datetime64_days()
        +test_numpy_array_d1_datetime64_hours()
        +test_numpy_array_d1_datetime64_minutes()
        +test_numpy_array_d1_datetime64_seconds()
        +test_numpy_array_d1_datetime64_milliseconds()
        +test_numpy_array_d1_datetime64_microseconds()
        +test_numpy_array_d1_datetime64_nanoseconds()
        +test_numpy_array_d1_datetime64_picoseconds()
        +test_numpy_array_d2_i64()
        +test_numpy_array_d2_f64()
        +test_numpy_array_d3_i8()
        +test_numpy_array_d3_u8()
        +test_numpy_array_d3_i32()
        +test_numpy_array_d3_i64()
        +test_numpy_array_d3_f64()
        +test_numpy_array_fortran()
        +test_numpy_array_non_contiguous_message()
        +test_numpy_array_unsupported_dtype()
        +test_numpy_array_d1()
        +test_numpy_array_d2()
        +test_numpy_array_d3()
        +test_numpy_array_d4()
        +test_numpy_array_4_stride()
        +test_numpy_array_dimension_zero()
        +test_numpy_array_dimension_max()
        +test_numpy_scalar_int8()
        +test_numpy_scalar_int16()
        +test_numpy_scalar_int32()
        +test_numpy_scalar_int64()
        +test_numpy_scalar_uint8()
        +test_numpy_scalar_uint16()
        +test_numpy_scalar_uint32()
        +test_numpy_scalar_uint64()
        +test_numpy_scalar_float16()
        +test_numpy_scalar_float32()
        +test_numpy_scalar_float64()
        +test_numpy_bool()
        +test_numpy_datetime_year()
        +test_numpy_datetime_month()
        +test_numpy_datetime_day()
        +test_numpy_datetime_hour()
        +test_numpy_datetime_minute()
        +test_numpy_datetime_second()
        +test_numpy_datetime_milli()
        +test_numpy_datetime_micro()
        +test_numpy_datetime_nano()
        +test_numpy_datetime_naive_utc_year()
        +test_numpy_datetime_naive_utc_month()
        +test_numpy_datetime_naive_utc_day()
        +test_numpy_datetime_naive_utc_hour()
        +test_numpy_datetime_naive_utc_minute()
        +test_numpy_datetime_naive_utc_second()
        +test_numpy_datetime_naive_utc_milli()
        +test_numpy_datetime_naive_utc_micro()
        +test_numpy_datetime_naive_utc_nano()
        +test_numpy_datetime_naive_utc_utc_z_year()
        +test_numpy_datetime_naive_utc_utc_z_month()
        +test_numpy_datetime_naive_utc_utc_z_day()
        +test_numpy_datetime_naive_utc_utc_z_hour()
        +test_numpy_datetime_naive_utc_utc_z_minute()
        +test_numpy_datetime_naive_utc_utc_z_second()
        +test_numpy_datetime_naive_utc_utc_z_milli()
        +test_numpy_datetime_naive_utc_utc_z_micro()
        +test_numpy_datetime_naive_utc_utc_z_nano()
        +test_numpy_datetime_omit_microseconds_year()
        +test_numpy_datetime_omit_microseconds_month()
        +test_numpy_datetime_omit_microseconds_day()
        +test_numpy_datetime_omit_microseconds_hour()
        +test_numpy_datetime_omit_microseconds_minute()
        +test_numpy_datetime_omit_microseconds_second()
        +test_numpy_datetime_omit_microseconds_milli()
        +test_numpy_datetime_omit_microseconds_micro()
        +test_numpy_datetime_omit_microseconds_nano()
        +test_numpy_datetime_nat()
        +test_numpy_repeated()
    }

    class TestNumpyEquivalence {
        -_test(obj)
        +test_numpy_uint8()
        +test_numpy_uint16()
        +test_numpy_uint32()
        +test_numpy_uint64()
        +test_numpy_int8()
        +test_numpy_int16()
        +test_numpy_int32()
        +test_numpy_int64()
        +test_numpy_float32()
        +test_numpy_float64()
    }

    class NumpyEndianness {
        +test_numpy_array_dimension_zero()
    }

    TestNumpy --> numpy_default : uses (for fallback serialization)

Summary

`test_numpy.py` is a rigorous test suite ensuring that `orjson` can serialize NumPy arrays and scalars correctly across a wide range of scenarios, including edge cases with special values, datetime units, array memory layouts, and scalar types. This file is essential for maintaining the reliability and correctness of NumPy serialization support in the `orjson` library and helps prevent regressions or unsupported cases.


If you need further information or examples on specific tests or usage scenarios, please let me know.