test_typeddict.py
Overview
The [test_typeddict.py](/projects/287/68206) file contains a simple test case focused on verifying the JSON serialization of Python `TypedDict` objects using the `orjson` library. The primary functionality demonstrated here is that an instance of a `TypedDict` can be serialized correctly to a JSON byte string by [orjson.dumps()](/projects/287/67747).
This file serves as a minimal validation or example for compatibility between `TypedDict` (a type hinting feature introduced in Python 3.8 and backported via `typing_extensions`) and the `orjson` serialization library.
Classes and Methods
Class: TestTypedDict
A test class designed to group related test(s) for `TypedDict` serialization.
Method: test_typeddict(self)
Purpose:
To verify that an instance of aTypedDictsubclass can be serialized to JSON using orjson.dumps() producing the expected byte string output.Parameters:
self: The instance of theTestTypedDictclass.
Returns:
None.
The method uses an assertion to confirm the serialization result.
Description:
Inside this method, a localTypedDictsubclass TypedDict1 is defined with two fields:aof typestrbof typeint
An object `obj` of type [TypedDict1](/projects/287/67762) is created with values `a="a"` and `b=1`. The method asserts that serializing `obj` using [orjson.dumps(obj)](/projects/287/67747) matches the expected JSON byte string `b'{"a":"a","b":1}'`.
Usage Example:
test = TestTypedDict() test.test_typeddict()Running this method will raise an
AssertionErroriforjsondoes not serialize theTypedDictinstance as expected.
Implementation Details
Conditional Import for
TypedDict:
The file attempts to importTypedDictfrom the standardtypingmodule (available in Python 3.8+). If unavailable (e.g., on older Python versions), it falls back to importingTypedDictfromtyping_extensions. This ensures compatibility across Python versions.orjsonSerialization:orjsonis a fast JSON library for Python that serializes Python objects to JSON bytes. Here it is used to serialize aTypedDictinstance, which is essentially a dictionary with static type checking.Test Scope:
This is a minimal test focused only on serialization correctness. It does not test deserialization or other features ofTypedDict.
Interaction with Other System Components
orjsonlibrary:
This file tests compatibility betweenTypedDictandorjson. It depends onorjson's capability to serialize dictionary-like objects.Typing infrastructure:
It leverages Python's typing system to defineTypedDictfor structured dictionary types.Testing integration:
Although not explicitly stated, the structure suggests this is intended for use with a test runner (e.g., pytest or unittest) to automate this test as part of a test suite validating serialization behavior.
Mermaid Class Diagram
classDiagram
class TestTypedDict {
+test_typeddict()
}
Summary
This file provides a single test validating that
TypedDictinstances serialize correctly usingorjson.It ensures backward compatibility with Python versions lacking built-in
TypedDict.It is useful as a regression or compatibility test within a larger test suite for JSON serialization functionality.
The implementation is straightforward and focuses exclusively on serialization correctness for a defined typed dictionary.