Use pydantic for easy runtime data validation #734
+28
−29
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.
I've found writing more data oriented python has made my projects less bug prone and easily maintainable. Pydantic is a very popular library that can provide runtime data validation from standard type hints.
This PR shows how Pydantic could be introduced incrementally. I've used the
@validate_call
decorator to check the arguments provided tounique_dirs
. I've also madeLibrary
a data class with runtime type checking on initialization and assignment. (I normally like to usefrozen=True
, but was trying to keep changes to a minimum.)I have also bumped the python version to 3.10. I chose 3.10 because it adds nice ergonomic improvements to type hints, e.g.
str | list
as oppose toUnion[str, list]
. Version 3.9 will also become end-of-life before the end of the year. The latest Pydantic only requires python version 3.8, though so we don't have to jump to 3.10 in order to use it.