test_non_str_keys.py

Overview

`test_non_str_keys.py` is a comprehensive test suite designed to verify the behavior of the `orjson` JSON serialization library when serializing dictionary keys that are **not strings**. The file specifically tests the `OPT_NON_STR_KEYS` option, which enables serialization of non-string dictionary keys by converting them into string representations or handling them according to defined rules.

The tests cover a wide range of key types including integers, floats, booleans, datetime objects, UUIDs, custom subclasses of `str`, and invalid or unsupported types. The suite also verifies the interaction of `OPT_NON_STR_KEYS` with other `orjson` serialization options and error conditions.

The goal is to ensure consistent, predictable, and correct serialization behavior when dictionaries with non-string keys are serialized using `orjson`.


Classes and Functions

Class: SubStr(str)


Class: TestNonStrKeyTests

This class contains multiple test methods, each verifying a particular aspect of `orjson`’s handling of non-string dictionary keys with the `OPT_NON_STR_KEYS` option.

All tests use `pytest` framework conventions.


Test Methods

Each method tests serialization behavior or error handling for specific key types or option combinations.


test_dict_keys_duplicate(self)

test_dict_keys_int(self)

test_dict_keys_substr(self)

test_dict_keys_substr_passthrough(self)

test_dict_keys_substr_invalid(self)

test_dict_keys_strict(self)

test_dict_keys_int_range_valid_i64(self)

test_dict_keys_int_range_valid_u64(self)

test_dict_keys_int_range_invalid(self)

test_dict_keys_float(self)

test_dict_keys_inf(self)

test_dict_keys_nan(self)

test_dict_keys_bool(self)

test_dict_keys_datetime(self)

test_dict_keys_datetime_opt(self)

test_dict_keys_datetime_passthrough(self)

test_dict_keys_uuid(self)

test_dict_keys_date(self)

test_dict_keys_time(self)

test_dict_non_str_and_sort_keys(self)

test_dict_keys_time_err(self)

test_dict_keys_str(self)

test_dict_keys_type(self)

test_dict_keys_array(self)

test_dict_keys_dataclass(self)

test_dict_keys_dataclass_hash(self)

test_dict_keys_list(self)

test_dict_keys_dict(self)

test_dict_keys_tuple(self)

test_dict_keys_unknown(self)

test_dict_keys_no_str_call(self)

Important Implementation Details


Interaction with Other Parts of the System


Usage Examples

Below are representative examples derived from tests showing how `orjson` serializes dictionaries with non-string keys when using `OPT_NON_STR_KEYS`:

import orjson
import datetime
import uuid

# Integer keys serialize as strings
print(orjson.dumps({1: True, 2: False}, option=orjson.OPT_NON_STR_KEYS))
# Output: b'{"1":true,"2":false}'

# Datetime keys serialize as ISO 8601 strings
dt = datetime.datetime(2023, 6, 15, 12, 30, 0)
print(orjson.dumps({dt: "value"}, option=orjson.OPT_NON_STR_KEYS))
# Output: b'{"2023-06-15T12:30:00": "value"}'

# UUID keys serialize as their canonical string
uid = uuid.UUID("7202d115-7ff3-4c81-a7c1-2a1f067b1ece")
print(orjson.dumps({uid: True}, option=orjson.OPT_NON_STR_KEYS))
# Output: b'{"7202d115-7ff3-4c81-a7c1-2a1f067b1ece":true}'

# Boolean keys serialize as "true" and "false" strings
print(orjson.dumps({True: 1, False: 0}, option=orjson.OPT_NON_STR_KEYS))
# Output: b'{"true":1,"false":0}'

Mermaid Diagram: Test Class Structure

classDiagram
    class SubStr {
        <<str subclass>>
    }

    class TestNonStrKeyTests {
        +test_dict_keys_duplicate()
        +test_dict_keys_int()
        +test_dict_keys_substr()
        +test_dict_keys_substr_passthrough()
        +test_dict_keys_substr_invalid()
        +test_dict_keys_strict()
        +test_dict_keys_int_range_valid_i64()
        +test_dict_keys_int_range_valid_u64()
        +test_dict_keys_int_range_invalid()
        +test_dict_keys_float()
        +test_dict_keys_inf()
        +test_dict_keys_nan()
        +test_dict_keys_bool()
        +test_dict_keys_datetime()
        +test_dict_keys_datetime_opt()
        +test_dict_keys_datetime_passthrough()
        +test_dict_keys_uuid()
        +test_dict_keys_date()
        +test_dict_keys_time()
        +test_dict_non_str_and_sort_keys()
        +test_dict_keys_time_err()
        +test_dict_keys_str()
        +test_dict_keys_type()
        +test_dict_keys_array()
        +test_dict_keys_dataclass()
        +test_dict_keys_dataclass_hash()
        +test_dict_keys_list()
        +test_dict_keys_dict()
        +test_dict_keys_tuple()
        +test_dict_keys_unknown()
        +test_dict_keys_no_str_call()
    }

    TestNonStrKeyTests ..> orjson : uses
    TestNonStrKeyTests ..> pytest : uses
    TestNonStrKeyTests ..> numpy : conditional
    TestNonStrKeyTests ..> pytz : conditional

Summary

`test_non_str_keys.py` is a focused test suite that rigorously ensures the `orjson` library correctly handles dictionary keys that are not strings, under the `OPT_NON_STR_KEYS` serialization option. It covers valid and invalid key types, interactions with other serialization options, and error handling scenarios. This file is critical for maintaining the correctness and robustness of `orjson`’s flexible key serialization capabilities.