Skip to content

Commit ae379b0

Browse files
committed
precommits and consistent stubs
precommits and consistent stubs
1 parent 9090e32 commit ae379b0

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

python/pydantic_core/_pydantic_core.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ def to_json(
427427
serialize_as_any: Whether to serialize fields with duck-typing serialization behavior.
428428
context: The context to use for serialization, this is passed to functional serializers as
429429
[`info.context`][pydantic_core.core_schema.SerializationInfo.context].
430+
sort_keys: Whether to sort the keys of the serialized object.
430431
431432
Raises:
432433
PydanticSerializationError: If serialization fails and no `fallback` function is provided.
@@ -479,6 +480,7 @@ def to_jsonable_python(
479480
fallback: Callable[[Any], Any] | None = None,
480481
serialize_as_any: bool = False,
481482
context: Any | None = None,
483+
sort_keys: bool = False,
482484
) -> Any:
483485
"""
484486
Serialize/marshal a Python object to a JSON-serializable Python object including transforming and filtering data.
@@ -503,7 +505,7 @@ def to_jsonable_python(
503505
serialize_as_any: Whether to serialize fields with duck-typing serialization behavior.
504506
context: The context to use for serialization, this is passed to functional serializers as
505507
[`info.context`][pydantic_core.core_schema.SerializationInfo.context].
506-
508+
sort_keys: Whether to sort the keys of the serialized object.
507509
Raises:
508510
PydanticSerializationError: If serialization fails and no `fallback` function is provided.
509511

tests/test.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ a = A()
104104
None,
105105
false,
106106
None,
107+
false,
107108
)
108109
.unwrap();
109110
let serialized: &[u8] = serialized.extract(py).unwrap();
@@ -212,6 +213,7 @@ dump_json_input_2 = {'a': 'something'}
212213
None,
213214
false,
214215
None,
216+
false,
215217
)
216218
.unwrap();
217219
let repr = format!("{}", serialization_result.bind(py).repr().unwrap());
@@ -233,6 +235,7 @@ dump_json_input_2 = {'a': 'something'}
233235
None,
234236
false,
235237
None,
238+
false,
236239
)
237240
.unwrap();
238241
let repr = format!("{}", serialization_result.bind(py).repr().unwrap());

tests/test_json.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import platform
33
import re
4+
from typing import Any
45

56
import pytest
67
from dirty_equals import IsFloatNan, IsList
@@ -253,7 +254,7 @@ def test_to_json_fallback():
253254
),
254255
],
255256
)
256-
def test_to_json_sort_keys(input_value, unsorted_output_value, sorted_output_value):
257+
def test_to_json_sort_keys(input_value: dict[str, Any], unsorted_output_value: bytes, sorted_output_value: bytes):
257258
assert to_json(input_value) == unsorted_output_value
258259
assert to_json(input_value, sort_keys=True) == sorted_output_value
259260

@@ -274,6 +275,21 @@ def test_to_jsonable_python_fallback():
274275
assert to_jsonable_python(Foobar(), fallback=fallback_func) == 'fallback:Foobar'
275276

276277

278+
@pytest.mark.parametrize(
279+
'input_value,unsorted_output_value,sorted_output_value',
280+
[
281+
({'b': 2, 'a': 1}, {'b': 2, 'a': 1}, {'a': 1, 'b': 2}),
282+
({'b': {'d': 4, 'c': 3}}, {'b': {'d': 4, 'c': 3}}, {'b': {'c': 3, 'd': 4}}),
283+
({'b': {'d': 4, 'c': 3}, 'a': 1}, {'b': {'d': 4, 'c': 3}, 'a': 1}, {'a': 1, 'b': {'c': 3, 'd': 4}}),
284+
],
285+
)
286+
def test_to_jsonable_python_sort_keys(
287+
input_value: dict[str, Any], unsorted_output_value: dict[str, Any], sorted_output_value: dict[str, Any]
288+
):
289+
assert to_jsonable_python(input_value) == unsorted_output_value
290+
assert to_jsonable_python(input_value, sort_keys=True) == sorted_output_value
291+
292+
277293
def test_to_jsonable_python_schema_serializer():
278294
class Foobar:
279295
def __init__(self, my_foo: int, my_inners: list['Foobar']):

0 commit comments

Comments
 (0)