Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support allow_partial='trailing-strings' #1390

Merged
merged 3 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/pydantic_core/_pydantic_core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def from_json(
*,
allow_inf_nan: bool = True,
cache_strings: bool | Literal['all', 'keys', 'none'] = True,
allow_partial: bool = False,
allow_partial: bool | Literal['off', 'on', 'trailing-strings'] = False,
) -> Any:
"""
Deserialize JSON data to a Python object.
Expand Down
25 changes: 5 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,23 @@ pub use validators::{validate_core_schema, PySome, SchemaValidator};

use crate::input::Input;

#[derive(FromPyObject)]
pub enum CacheStringsArg {
Bool(bool),
Literal(StringCacheMode),
}

#[pyfunction(signature = (data, *, allow_inf_nan=true, cache_strings=CacheStringsArg::Bool(true), allow_partial=false))]
#[pyfunction(signature = (data, *, allow_inf_nan=true, cache_strings=StringCacheMode::All, allow_partial=PartialMode::Off))]
pub fn from_json<'py>(
py: Python<'py>,
data: &Bound<'_, PyAny>,
allow_inf_nan: bool,
cache_strings: CacheStringsArg,
allow_partial: bool,
cache_strings: StringCacheMode,
allow_partial: PartialMode,
) -> PyResult<Bound<'py, PyAny>> {
let v_match = data
.validate_bytes(false, ValBytesMode { ser: BytesMode::Utf8 })
.map_err(|_| PyTypeError::new_err("Expected bytes, bytearray or str"))?;
let json_either_bytes = v_match.into_inner();
let json_bytes = json_either_bytes.as_slice();
let cache_mode = match cache_strings {
CacheStringsArg::Bool(b) => b.into(),
CacheStringsArg::Literal(mode) => mode,
};
let partial_mode = if allow_partial {
PartialMode::On
} else {
PartialMode::Off
};
let parse_builder = PythonParse {
allow_inf_nan,
cache_mode,
partial_mode,
cache_mode: cache_strings,
partial_mode: allow_partial,
catch_duplicate_keys: false,
lossless_floats: false,
};
Expand Down
Loading