Skip to content

Commit de1fd5a

Browse files
committed
frozendict
1 parent f110dbe commit de1fd5a

9 files changed

Lines changed: 166 additions & 16 deletions

File tree

‎README.md‎

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,16 @@ def dumps(
151151
`dumps()` serializes Python objects to JSON.
152152

153153
It natively serializes
154-
`str`, `dict`, `list`, `tuple`, `int`, `float`, `bool`, `None`,
154+
`str`, `dict`, `frozendict`, `list`, `tuple`, `int`, `float`, `bool`, `None`,
155155
`dataclasses.dataclass`, `typing.TypedDict`, `datetime.datetime`,
156156
`datetime.date`, `datetime.time`, `uuid.UUID`, `numpy.ndarray`, and
157157
`orjson.Fragment` instances. It supports arbitrary types through `default`. It
158158
serializes subclasses of `str`, `int`, `dict`, `list`,
159159
`dataclasses.dataclass`, and `enum.Enum`. It does not serialize subclasses
160-
of `tuple` to avoid serializing `namedtuple` objects as arrays. To avoid
161-
serializing subclasses, specify the option `orjson.OPT_PASSTHROUGH_SUBCLASS`.
160+
of `tuple` to avoid serializing `namedtuple` objects as arrays. It does not
161+
serialize subclasses of `float` or `frozendict` due to implementation details
162+
in CPython. To avoid serializing subclasses, specify the
163+
option `orjson.OPT_PASSTHROUGH_SUBCLASS`.
162164

163165
The output is a `bytes` object containing UTF-8.
164166

@@ -174,8 +176,8 @@ It raises `JSONEncodeError` on a `str` that contains invalid UTF-8.
174176
It raises `JSONEncodeError` on an integer that exceeds 64 bits by default or,
175177
with `OPT_STRICT_INTEGER`, 53 bits.
176178

177-
It raises `JSONEncodeError` if a `dict` has a key of a type other than `str`,
178-
unless `OPT_NON_STR_KEYS` is specified.
179+
It raises `JSONEncodeError` if a `dict` or `frozendict` has a key of a type
180+
other than `str`, unless `OPT_NON_STR_KEYS` is specified.
179181

180182
It raises `JSONEncodeError` if the output of `default` recurses to handling by
181183
`default` more than 254 levels deep.
@@ -326,9 +328,10 @@ b'"1970-01-01T00:00:00+00:00"'
326328

327329
##### OPT_NON_STR_KEYS
328330

329-
Serialize `dict` keys of type other than `str`. This allows `dict` keys
330-
to be one of `str`, `int`, `float`, `bool`, `None`, `datetime.datetime`,
331-
`datetime.date`, `datetime.time`, `enum.Enum`, and `uuid.UUID`. For comparison,
331+
Serialize `dict` and `frozendict` keys of type other than `str`. This allows
332+
`dict` and `frozendict` keys to be one of `str`, `int`, `float`,
333+
`bool`, `None`, `datetime.datetime`, `datetime.date`,
334+
`datetime.time`, `enum.Enum`, and `uuid.UUID`. For comparison,
332335
the standard library serializes `str`, `int`, `float`, `bool` or `None` by
333336
default. orjson benchmarks as being faster at serializing non-`str` keys
334337
than other libraries. This option is slower for `str` keys than the default.
@@ -462,7 +465,7 @@ TypeError: Type is not JSON serializable: datetime.datetime
462465
b'{"created_at":"Thu, 01 Jan 1970 00:00:00 GMT"}'
463466
```
464467

465-
This does not affect datetimes in `dict` keys if using OPT_NON_STR_KEYS.
468+
This does not affect datetimes in `dict` or `frozendict` keys if using `OPT_NON_STR_KEYS`.
466469

467470
##### OPT_PASSTHROUGH_SUBCLASS
468471

@@ -487,8 +490,8 @@ TypeError: Type is not JSON serializable: Secret
487490
b'"******"'
488491
```
489492

490-
This does not affect serializing subclasses as `dict` keys if using
491-
OPT_NON_STR_KEYS.
493+
This does not affect serializing subclasses as `dict` or `frozendict` keys
494+
if using `OPT_NON_STR_KEYS`.
492495

493496
##### OPT_SERIALIZE_DATACLASS
494497

@@ -509,9 +512,9 @@ required to serialize `uuid.UUID` instances. For more, see
509512

510513
##### OPT_SORT_KEYS
511514

512-
Serialize `dict` keys in sorted order. The default is to serialize in an
513-
unspecified order. This is equivalent to `sort_keys=True` in the standard
514-
library.
515+
Serialize `dict` and `frozendict` keys in sorted order. The default is
516+
to serialize in an unspecified order. This is equivalent
517+
to `sort_keys=True` in the standard library.
515518

516519
This can be used to ensure the order is deterministic for hashing or tests.
517520
It has a substantial performance penalty and is not recommended in general.

‎pyproject.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ ignore = [
8383
"E402", # Module level import not at top of file
8484
"E501", # line too long
8585
"F601", # Dictionary key literal ... repeated
86+
"F821", # Undefined name `...`. Consider specifying `requires-python = ">= ..."`
8687
"PIE810", # Call `startswith` once with a `tuple`
8788
"PLR2004", # Magic value used in comparison
8889
"RUF061", # Use context-manager form of `pytest.raises()`

‎src/ffi/mod.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,7 @@ pub(crate) use pyo3_ffi::{PyDict_Contains, PyDictObject};
136136
#[cfg(Py_3_13)]
137137
#[allow(unused_imports)]
138138
pub(crate) use pyo3_ffi::{Py_MOD_GIL_NOT_USED, Py_MOD_GIL_USED, Py_mod_gil};
139+
140+
#[cfg(Py_3_15)]
141+
#[allow(unused_imports)]
142+
pub(crate) use pyo3_ffi::PyFrozenDict_New;

‎src/ffi/pydictref.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ impl PyDictRef {
7979
pub(crate) unsafe fn from_ptr_unchecked(ptr: *mut pyo3_ffi::PyObject) -> Self {
8080
unsafe {
8181
debug_assert!(!ptr.is_null());
82+
#[cfg(Py_3_15)]
83+
debug_assert!(
84+
crate::ffi::PyObject_Type(ptr) == crate::typeref::DICT_TYPE
85+
|| crate::ffi::PyObject_Type(ptr) == crate::typeref::FROZENDICT_TYPE
86+
|| is_subclass_by_flag!(
87+
PyType_GetFlags(crate::ffi::PyObject_Type(ptr)),
88+
Py_TPFLAGS_DICT_SUBCLASS
89+
)
90+
);
91+
#[cfg(not(Py_3_15))]
8292
debug_assert!(
8393
crate::ffi::PyObject_Type(ptr) == crate::typeref::DICT_TYPE
8494
|| is_subclass_by_flag!(

‎src/serialize/obtype.rs‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
use crate::opt::{PASSTHROUGH_DATACLASS, PASSTHROUGH_SUBCLASS, SERIALIZE_NUMPY};
55
use crate::serialize::numpy::{is_numpy_array, is_numpy_scalar};
6+
#[cfg(Py_3_15)]
7+
use crate::typeref::FROZENDICT_TYPE;
68
use crate::typeref::{
79
BOOL_TYPE, DATACLASS_FIELDS_STR, DATE_TYPE, DATETIME_TYPE, DICT_TYPE, ENUM_TYPE, FLOAT_TYPE,
810
FRAGMENT_TYPE, INT_TYPE, LIST_TYPE, NONE_TYPE, STR_TYPE, TIME_TYPE, TUPLE_TYPE, UUID_TYPE,
@@ -62,7 +64,18 @@ pub(crate) fn pyobject_to_obtype_likely(ob: PyTypeRef) -> Option<ObType> {
6264
} else if ob_type == DATETIME_TYPE {
6365
Some(ObType::Datetime)
6466
} else {
65-
None
67+
#[cfg(Py_3_15)]
68+
{
69+
if ob_type == FROZENDICT_TYPE {
70+
Some(ObType::Dict)
71+
} else {
72+
None
73+
}
74+
}
75+
#[cfg(not(Py_3_15))]
76+
{
77+
None
78+
}
6679
}
6780
}
6881
}

‎src/typeref.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ use crate::ffi::{
1414
PyUnicode_InternFromString, PyUnicode_New, PyUnicode_Type, orjson_fragmenttype_new,
1515
};
1616

17+
#[cfg(Py_3_15)]
18+
use crate::ffi::{PyDict_New, PyFrozenDict_New};
19+
1720
pub(crate) static mut DEFAULT: *mut PyObject = null_mut();
1821
pub(crate) static mut OPTION: *mut PyObject = null_mut();
1922

@@ -31,6 +34,8 @@ pub(crate) static mut FLOAT_TYPE: *mut PyTypeObject = null_mut();
3134
pub(crate) static mut LIST_TYPE: *mut PyTypeObject = null_mut();
3235
pub(crate) static mut DICT_TYPE: *mut PyTypeObject = null_mut();
3336
pub(crate) static mut DATETIME_TYPE: *mut PyTypeObject = null_mut();
37+
#[cfg(Py_3_15)]
38+
pub(crate) static mut FROZENDICT_TYPE: *mut PyTypeObject = null_mut();
3439
pub(crate) static mut UUID_TYPE: *mut PyTypeObject = null_mut();
3540
pub(crate) static mut TUPLE_TYPE: *mut PyTypeObject = null_mut();
3641
pub(crate) static mut DATE_TYPE: *mut PyTypeObject = null_mut();
@@ -138,6 +143,12 @@ fn _init_typerefs_impl() -> bool {
138143
INT_TYPE = &raw mut PyLong_Type;
139144
FLOAT_TYPE = &raw mut PyFloat_Type;
140145

146+
#[cfg(Py_3_15)]
147+
{
148+
// PyFrozenDict_Type link error on Windows as of v3.15.0rc1 and pyo3 0.29
149+
FROZENDICT_TYPE = PyObject_Type(PyFrozenDict_New(PyDict_New(0)));
150+
}
151+
141152
look_up_datetime();
142153

143154
UUID_TYPE = look_up_type_object(c"uuid", c"UUID");

‎test/test_frozendict.py‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# SPDX-License-Identifier: MPL-2.0
2+
# Copyright ijl (2018-2026)
3+
4+
import datetime
5+
6+
import pytest
7+
8+
import orjson
9+
10+
from .util import SUPPORTS_FROZENDICT
11+
12+
13+
@pytest.mark.skipif(SUPPORTS_FROZENDICT is False, reason="python3.15")
14+
class TestFrozenDict:
15+
def test_frozendict(self):
16+
"""
17+
frozendict
18+
"""
19+
obj = frozendict({"key": "value"}) # type: ignore[name-defined]
20+
ref = '{"key":"value"}'
21+
assert orjson.dumps(obj) == ref.encode("utf-8")
22+
assert orjson.loads(ref) == obj
23+
24+
def test_frozendict_immutable(self):
25+
"""
26+
frozendict immmutable
27+
"""
28+
obj = frozendict({"0": 0}) # type: ignore[name-defined]
29+
with pytest.raises(TypeError):
30+
obj["1"] = 1
31+
32+
def test_frozendict_unicode(self):
33+
"""
34+
frozendict unicode keys
35+
"""
36+
obj = frozendict({"🐈": "value"}) # type: ignore[name-defined]
37+
ref = b'{"\xf0\x9f\x90\x88":"value"}'
38+
assert orjson.dumps(obj) == ref
39+
assert orjson.loads(ref) == dict(obj)
40+
41+
def test_frozendict_large_dict(self):
42+
"""
43+
frozendict with >512 keys
44+
"""
45+
obj = frozendict( # type: ignore[name-defined]
46+
{f"key_{idx}": [{}, {"a": [{}, {}, {}]}, {}] for idx in range(513)},
47+
) # type: ignore
48+
assert len(obj) == 513
49+
assert orjson.loads(orjson.dumps(obj)) == obj
50+
51+
def test_frozendict_invalid_key_dumps(self):
52+
"""
53+
frozendict invalid key dumps()
54+
"""
55+
with pytest.raises(orjson.JSONEncodeError):
56+
orjson.dumps(frozendict({1: "value"})) # type: ignore[name-defined]
57+
with pytest.raises(orjson.JSONEncodeError):
58+
orjson.dumps(frozendict({b"key": "value"})) # type: ignore[name-defined]
59+
60+
def test_frozendict_non_str_and_sort_keys(self):
61+
obj = frozendict( # type: ignore[name-defined]
62+
{
63+
"other": 1,
64+
datetime.date(1970, 1, 5): 2,
65+
datetime.date(1970, 1, 3): 3,
66+
},
67+
)
68+
assert (
69+
orjson.dumps(
70+
obj,
71+
option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SORT_KEYS,
72+
)
73+
== b'{"1970-01-03":3,"1970-01-05":2,"other":1}'
74+
)
75+
76+
def test_frozendict_default(self):
77+
"""
78+
frozendict default
79+
"""
80+
ref = {"1": True}
81+
82+
class Custom:
83+
def __init__(self):
84+
self.data = frozendict(ref) # type: ignore[name-defined]
85+
86+
def default(obj, /):
87+
return obj.data
88+
89+
assert orjson.loads(orjson.dumps(Custom(), default=default)) == ref

‎test/test_subclass.py‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SPDX-License-Identifier: MPL-2.0
2-
# Copyright ijl (2018-2022)
2+
# Copyright ijl (2018-2026)
33

44
import collections
55
import json
@@ -8,6 +8,8 @@
88

99
import orjson
1010

11+
from .util import SUPPORTS_FROZENDICT
12+
1113

1214
class SubStr(str):
1315
pass
@@ -56,6 +58,21 @@ def test_subclass_int_53(self):
5658
def test_subclass_dict(self):
5759
assert orjson.dumps(SubDict({"a": "b"})) == b'{"a":"b"}'
5860

61+
@pytest.mark.skipif(SUPPORTS_FROZENDICT is False, reason="python3.15")
62+
def test_subclass_frozendict(self):
63+
"""
64+
frozendict subclasses are not supported
65+
66+
As of 3.15b2, these do not have Py_TPFLAGS_DICT_SUBCLASS and there
67+
is no equivalent such as Py_TPFLAGS_FROZENDICT_SUBCLASS.
68+
"""
69+
70+
class SubFrozenDict(frozendict): # type: ignore[name-defined]
71+
pass
72+
73+
with pytest.raises(orjson.JSONEncodeError):
74+
orjson.dumps(SubFrozenDict({"a": "b"}))
75+
5976
def test_subclass_list(self):
6077
assert orjson.dumps(SubList(["a", "b"])) == b'["a","b"]'
6178
ref = [True] * 512

‎test/util.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
SUPPORTS_GETREFCOUNT = sys.implementation == "cpython"
1818

19+
SUPPORTS_FROZENDICT = sys.version_info.minor >= 15
20+
1921
numpy = None # type: ignore
2022
try:
2123
import numpy # type: ignore # noqa: F401

0 commit comments

Comments
 (0)