Skip to content
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

Use pydantic for easy runtime data validation #734

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

HU90m
Copy link

@HU90m HU90m commented Feb 25, 2025

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 to unique_dirs. I've also made Library a data class with runtime type checking on initialization and assignment. (I normally like to use frozen=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 to Union[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.

Python 3.10 has a nice syntax features for type annotations, such as `|`
unions, and 3.9 will be end of life by the end of 2025.

Pydantic has been added to allow runtime type checking.
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.

1 participant