-
Notifications
You must be signed in to change notification settings - Fork 3
update Pydantic #115
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
update Pydantic #115
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Pydandtic uses these attrs internally, can't override
This is a welcome addition, yet I suspect that most common users will not want to go so deep into writing their own typing classes to better formulate their problems. Still, having this option certainly does not hurt, thank you for the PR. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR makes us use the new pydantic version and then uses some of the new features to make defining problems much easier. No idea why some of the diffs are so massive when only a couple lines in the files changed, most of the changes in match.py and util.py aren't actually real.
There are two big changes: the first is that pydantic now uses rust internally, which gives us a speedup of about 20x (not 20%, 20x), though that's not super relevant since most of our runtime is spent in student code anyways. The other is that pydantic now uses much simpler ways to define validation properties. For example, if a problem instance contains a list of numbers that each should be bigger than 4 we can now do this:
the
Annotated
lets us add metadata to a type, in this case that it should be greater or equal to 4. You can also specify more complex logic like saying a dictionary should map english words to how often they are used:This makes some stuff nicer, but not a whole lot in our use case since most of our custom validation logic is things like "should be less than the instance size" or "encodes an edge that is in the instance graph". For this I added some fairly complex logic that lets you specify references to model fields and which are then evaluated during validation. So the first case would simply be:
To make things even simpler I've compiled many common use cases into the
algobattle.types
module. For example, the solution to a graph automorphism problem encoded as a permutation of the vertices can now simply be defined asand it will automatically be validated that every entry is a valid vertex, that none appear twice, and that every vertex is assigned an image. The only thing you have to manually validate is that the mapping preserves edges.
There's still a lot more cool stuff that can be added, but this should be a good start and make things nice enough.