Issue #57 - Introducing DataClasses and Pydantic (Static Type) Validation #99
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.
N.B. This PR relies on 3 separate bug fixes to the pydantic library which are currently stored in the forked repo at https://github.com/robons/pydantic/
N.B. - This does not allow us to start using specific validators on a per-attribute basis. This is something we might want to look into in future.
Dataclasses
This PR makes quite a change to our code-base. It enables us to easily validate that all of a class' attributes match the associated static type annotations. To support this, and to make a bit easier, it switches the majority of csvqb's models over to being dataclasses.
Dataclasses give us a simpler way of defining classes and makes our lives easier by implementing common functions you might want on your class including init, repr, str, eq, etc.
Dataclasses also provide an easy way for converting your model to a dictionary and specifying what the default values are for your parameters.
And all you have to do is annotate the model with the
@dataclass
attribute...Note that I haven't had to write the
__init__
function, or any other the other functions that the dataclasses will implement for me.Examples
Example of validation process:
Example where user has incorrectly defined columns:
It's also possible to directly call the
pydantic_validation()
method on any model which extendsPydanticModel
.This satisfies Issue #57