Skip to content

Commit

Permalink
Add support for Pydantic V2
Browse files Browse the repository at this point in the history
See https://docs.pydantic.dev/latest/migration/

Signed-off-by: Emanuele Giaquinta <[email protected]>
  • Loading branch information
exg committed Sep 24, 2023
1 parent b01be5d commit d8b85e9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/serialize/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ pub fn pyobject_to_obtype_unlikely(obj: *mut pyo3::ffi::PyObject, opts: Opt) ->
} else if opts & SERIALIZE_NUMPY != 0 && is_numpy_array(ob_type) {
ObType::NumpyArray
} else if opts & SERIALIZE_PYDANTIC != 0
&& ffi!(PyDict_Contains((*ob_type).tp_dict, PYDANTIC_FIELDS_STR)) == 1
&& (ffi!(PyDict_Contains((*ob_type).tp_dict, PYDANTIC_FIELDS_STR)) == 1
|| ffi!(PyDict_Contains((*ob_type).tp_dict, PYDANTIC2_FIELDS_STR)) == 1)
{
ObType::Pydantic
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/typeref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub static mut DICT_STR: *mut PyObject = 0 as *mut PyObject;
pub static mut DATACLASS_FIELDS_STR: *mut PyObject = 0 as *mut PyObject;
pub static mut SLOTS_STR: *mut PyObject = 0 as *mut PyObject;
pub static mut PYDANTIC_FIELDS_STR: *mut PyObject = 0 as *mut PyObject;
pub static mut PYDANTIC2_FIELDS_STR: *mut PyObject = 0 as *mut PyObject;
pub static mut FIELD_TYPE_STR: *mut PyObject = 0 as *mut PyObject;
pub static mut ARRAY_STRUCT_STR: *mut PyObject = 0 as *mut PyObject;
pub static mut VALUE_STR: *mut PyObject = 0 as *mut PyObject;
Expand Down Expand Up @@ -127,6 +128,8 @@ pub fn init_typerefs() {
PyUnicode_InternFromString("__dataclass_fields__\0".as_ptr() as *const c_char);
SLOTS_STR = PyUnicode_InternFromString("__slots__\0".as_ptr() as *const c_char);
PYDANTIC_FIELDS_STR = PyUnicode_InternFromString("__fields__\0".as_ptr() as *const c_char);
PYDANTIC2_FIELDS_STR =
PyUnicode_InternFromString("model_fields\0".as_ptr() as *const c_char);
FIELD_TYPE_STR = PyUnicode_InternFromString("_field_type\0".as_ptr() as *const c_char);
ARRAY_STRUCT_STR =
pyo3::ffi::PyUnicode_InternFromString("__array_struct__\0".as_ptr() as *const c_char);
Expand Down
2 changes: 1 addition & 1 deletion tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ pytz
typing_extensions;python_version>="3.6" and python_version<"3.8"
xxhash==1.4.3;sys_platform!="windows" and python_version<"3.9" # creates non-compact ASCII for test_str_ascii
msgpack
pydantic==1.10.12
pydantic

0 comments on commit d8b85e9

Please sign in to comment.