Skip to content

Commit 099a8e1

Browse files
Support allow_partial='trailing-strings' (#1390)
1 parent cc2c46c commit 099a8e1

File tree

2 files changed

+6
-21
lines changed

2 files changed

+6
-21
lines changed

python/pydantic_core/_pydantic_core.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ def from_json(
395395
*,
396396
allow_inf_nan: bool = True,
397397
cache_strings: bool | Literal['all', 'keys', 'none'] = True,
398-
allow_partial: bool = False,
398+
allow_partial: bool | Literal['off', 'on', 'trailing-strings'] = False,
399399
) -> Any:
400400
"""
401401
Deserialize JSON data to a Python object.

src/lib.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,38 +42,23 @@ pub use validators::{validate_core_schema, PySome, SchemaValidator};
4242

4343
use crate::input::Input;
4444

45-
#[derive(FromPyObject)]
46-
pub enum CacheStringsArg {
47-
Bool(bool),
48-
Literal(StringCacheMode),
49-
}
50-
51-
#[pyfunction(signature = (data, *, allow_inf_nan=true, cache_strings=CacheStringsArg::Bool(true), allow_partial=false))]
45+
#[pyfunction(signature = (data, *, allow_inf_nan=true, cache_strings=StringCacheMode::All, allow_partial=PartialMode::Off))]
5246
pub fn from_json<'py>(
5347
py: Python<'py>,
5448
data: &Bound<'_, PyAny>,
5549
allow_inf_nan: bool,
56-
cache_strings: CacheStringsArg,
57-
allow_partial: bool,
50+
cache_strings: StringCacheMode,
51+
allow_partial: PartialMode,
5852
) -> PyResult<Bound<'py, PyAny>> {
5953
let v_match = data
6054
.validate_bytes(false, ValBytesMode { ser: BytesMode::Utf8 })
6155
.map_err(|_| PyTypeError::new_err("Expected bytes, bytearray or str"))?;
6256
let json_either_bytes = v_match.into_inner();
6357
let json_bytes = json_either_bytes.as_slice();
64-
let cache_mode = match cache_strings {
65-
CacheStringsArg::Bool(b) => b.into(),
66-
CacheStringsArg::Literal(mode) => mode,
67-
};
68-
let partial_mode = if allow_partial {
69-
PartialMode::On
70-
} else {
71-
PartialMode::Off
72-
};
7358
let parse_builder = PythonParse {
7459
allow_inf_nan,
75-
cache_mode,
76-
partial_mode,
60+
cache_mode: cache_strings,
61+
partial_mode: allow_partial,
7762
catch_duplicate_keys: false,
7863
lossless_floats: false,
7964
};

0 commit comments

Comments
 (0)