Skip to content

Conversation

@DrPaulSharp
Copy link
Collaborator

This PR adds a context manager to the set_fields routine that should suppress errors in custom validation (i.e., field validators and model validators that we write) for pydantic models included within a ClassList.

@DrPaulSharp DrPaulSharp requested a review from alexhroom January 9, 2025 13:34
Copy link
Collaborator

@alexhroom alexhroom left a comment

Choose a reason for hiding this comment

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

looks good apart from some code comments. i also tested it with the backgrounds PR (#100, with the frozen fields changes reverted of course) and it works as we want it to!

Comment on lines 268 to 269
# Pydantic is installed, so set up a context manager that will
# suppress validation errors until all fields have been set.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
# Pydantic is installed, so set up a context manager that will
# suppress validation errors until all fields have been set.
# Pydantic is installed, so set up a context manager that will
# suppress custom validation errors until all fields have been set.

Comment on lines 320 to 325

if pydantic_object:
self._class_handle.model_validate(self.data[index])
Copy link
Collaborator

Choose a reason for hiding this comment

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

i think this needs a code comment for why we're revalidating (i.e. "to catch any actual validation issues")

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've gone for

        # We have suppressed custom validation errors for pydantic objects.
        # We now must revalidate the pydantic model outside the validation context
        # to catch any errors that remain after setting all of the fields.

@DrPaulSharp DrPaulSharp merged commit 973ff1d into RascalSoftware:main Jan 9, 2025
7 checks passed
@DrPaulSharp DrPaulSharp deleted the context branch January 9, 2025 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants