Skip to content
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
6 changes: 0 additions & 6 deletions src/argument_markers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,3 @@ impl PydanticUndefinedType {
"PydanticUndefined"
}
}

impl PydanticUndefinedType {
pub fn py_undefined() -> Py<Self> {
Python::with_gil(PydanticUndefinedType::new)
}
}
6 changes: 4 additions & 2 deletions src/validators/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub struct ModelValidator {
frozen: bool,
custom_init: bool,
root_model: bool,
undefined: PyObject,
name: String,
}

Expand Down Expand Up @@ -93,6 +94,7 @@ impl BuildValidator for ModelValidator {
frozen: schema.get_as(intern!(py, "frozen"))?.unwrap_or(false),
custom_init: schema.get_as(intern!(py, "custom_init"))?.unwrap_or(false),
root_model: schema.get_as(intern!(py, "root_model"))?.unwrap_or(false),
undefined: PydanticUndefinedType::new(py).to_object(py),
// Get the class's `__name__`, not using `class.name()` since it uses `__qualname__`
// which is not what we want here
name: class.getattr(intern!(py, "__name__"))?.extract()?,
Expand Down Expand Up @@ -229,7 +231,7 @@ impl ModelValidator {
let output = self.validator.validate(py, input, state)?;

if self.root_model {
let fields_set = if input.to_object(py).is(&PydanticUndefinedType::py_undefined()) {
let fields_set = if input.to_object(py).is(&self.undefined) {
PySet::empty(py)?
} else {
PySet::new(py, [&String::from(ROOT_FIELD)])?
Expand Down Expand Up @@ -270,7 +272,7 @@ impl ModelValidator {
let instance_ref = instance.as_ref(py);

if self.root_model {
let fields_set = if input.to_object(py).is(&PydanticUndefinedType::py_undefined()) {
let fields_set = if input.to_object(py).is(&self.undefined) {
PySet::empty(py)?
} else {
PySet::new(py, [&String::from(ROOT_FIELD)])?
Expand Down
4 changes: 3 additions & 1 deletion src/validators/with_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub struct WithDefaultValidator {
validate_default: bool,
copy_default: bool,
name: String,
undefined: PyObject,
}

impl BuildValidator for WithDefaultValidator {
Expand Down Expand Up @@ -118,6 +119,7 @@ impl BuildValidator for WithDefaultValidator {
validate_default: schema_or_config_same(schema, config, intern!(py, "validate_default"))?.unwrap_or(false),
copy_default,
name,
undefined: PydanticUndefinedType::new(py).to_object(py),
}
.into())
}
Expand All @@ -132,7 +134,7 @@ impl Validator for WithDefaultValidator {
input: &'data impl Input<'data>,
state: &mut ValidationState,
) -> ValResult<PyObject> {
if input.to_object(py).is(&PydanticUndefinedType::py_undefined()) {
if input.to_object(py).is(&self.undefined) {
Ok(self.default_value(py, None::<usize>, state)?.unwrap())
} else {
match self.validator.validate(py, input, state) {
Expand Down