Skip to content

Fix Validator::complete not being implemented properly for all validator types #494

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

Merged
merged 1 commit into from
Mar 29, 2023
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 generate_self_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def type_dict_schema(typed_dict) -> dict[str, Any]: # noqa: C901
return {'type': 'typed-dict', 'fields': fields, 'extra_behavior': 'forbid'}


def union_schema(union_type: UnionType) -> core_schema.UnionSchema | core_schema.RecursiveReferenceSchema:
def union_schema(union_type: UnionType) -> core_schema.UnionSchema | core_schema.DefinitionReferenceSchema:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed a few out-of-date references to RecursiveReferenceSchema and fixed those up as well.

return {'type': 'union', 'choices': [get_schema(arg) for arg in union_type.__args__]}


Expand Down
2 changes: 1 addition & 1 deletion src/build_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl<T: Clone + std::fmt::Debug> BuildContext<T> {
}

/// find a validator/serializer by `slot_id` - this used in `Validator.complete`,
/// specifically `RecursiveRefValidator` to set its name
/// specifically `DefinitionRefValidator` to set its name
pub fn find_validator(&self, slot_id: usize) -> PyResult<&T> {
match self.slots.get(slot_id) {
Some(slot) => match slot.op_val_ser {
Expand Down
13 changes: 13 additions & 0 deletions src/validators/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,17 @@ impl Validator for ArgumentsValidator {
fn get_name(&self) -> &str {
Self::EXPECTED_TYPE
}

fn complete(&mut self, build_context: &BuildContext<CombinedValidator>) -> PyResult<()> {
self.parameters
.iter_mut()
.try_for_each(|parameter| parameter.validator.complete(build_context))?;
if let Some(v) = &mut self.var_args_validator {
v.complete(build_context)?;
}
if let Some(v) = &mut self.var_kwargs_validator {
v.complete(build_context)?;
};
Ok(())
}
}
8 changes: 8 additions & 0 deletions src/validators/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,12 @@ impl Validator for CallValidator {
fn get_name(&self) -> &str {
&self.name
}

fn complete(&mut self, build_context: &BuildContext<CombinedValidator>) -> PyResult<()> {
self.arguments_validator.complete(build_context)?;
match &mut self.return_validator {
Some(v) => v.complete(build_context),
None => Ok(()),
}
}
}
6 changes: 6 additions & 0 deletions src/validators/dataclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,12 @@ impl Validator for DataclassArgsValidator {
&self.validator_name
}

fn complete(&mut self, build_context: &BuildContext<CombinedValidator>) -> PyResult<()> {
self.fields
.iter_mut()
.try_for_each(|field| field.validator.complete(build_context))
}

fn validate_assignment<'s, 'data: 's>(
&'s self,
py: Python<'data>,
Expand Down
2 changes: 1 addition & 1 deletion src/validators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ pub trait Validator: Send + Sync + Clone + Debug {
}

/// this method must be implemented for any validator which holds references to other validators,
/// it is used by `RecursiveRefValidator` to set its name
/// it is used by `DefinitionRefValidator` to set its name
fn complete(&mut self, _build_context: &BuildContext<CombinedValidator>) -> PyResult<()> {
Ok(())
}
Expand Down
4 changes: 4 additions & 0 deletions src/validators/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ impl Validator for ModelValidator {
&self.name
}

fn complete(&mut self, build_context: &BuildContext<CombinedValidator>) -> PyResult<()> {
self.validator.complete(build_context)
}

fn validate_assignment<'s, 'data: 's>(
&'s self,
py: Python<'data>,
Expand Down
6 changes: 5 additions & 1 deletion src/validators/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ impl Validator for TuplePositionalValidator {
fn complete(&mut self, build_context: &BuildContext<CombinedValidator>) -> PyResult<()> {
self.items_validators
.iter_mut()
.try_for_each(|v| v.complete(build_context))
.try_for_each(|v| v.complete(build_context))?;
match &mut self.extra_validator {
Some(v) => v.complete(build_context),
None => Ok(()),
}
}
}
6 changes: 5 additions & 1 deletion src/validators/typed_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,11 @@ impl Validator for TypedDictValidator {
fn complete(&mut self, build_context: &BuildContext<CombinedValidator>) -> PyResult<()> {
self.fields
.iter_mut()
.try_for_each(|f| f.validator.complete(build_context))
.try_for_each(|f| f.validator.complete(build_context))?;
match &mut self.extra_validator {
Some(v) => v.complete(build_context),
None => Ok(()),
}
}

fn validate_assignment<'s, 'data: 's>(
Expand Down