feat: added Pydantic validation to prevent type crashes from YAML files #176
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.
Enhance Type Safety and YAML Schema Validation
Description
This PR introduces a structured validation layer for loading
YAMLconfiguration files, specifically addressing the"Any"loophole that allows malformed data to cause runtime crashes.The Problem
Previously,
GatorGradeloadedYAMLdata as anAnytype. If a user provided a list where a string was expected (e.g., in adescriptionfield), the error would propagate silently throughmain.pyandparse_config.py, only crashing with a crypticTypeErrordeep inside the recursive logic ofin_file_path.py.The Solution
Pydanticmodels) that checks the structure and types of theYAMLdata immediately after loading.Anywith structured types/models in thein_file_path.pyThis enables static analysis tools likeMypyto verify the code's logic during development.I have chosen
Pydanticbecause:Pydantichas excellent support for type checkers, ensuring that the safety gained at the parsing stage carries through to the rest of the application.If we run
uv run gatorgrade --config yaml_type_safety_test.ymlafter my applied changes we get the following result:Now we are also able to analyze files with different type checkers.
I have implemented the following code snippet for
gatorgrade/input/in_file_path.py-before my changes- and ranmypyon it:The output of
mypy:Success: no issues found in 1 source fileThis code examples clearly states that before my changes it was not possible to detect type related errors in the codebase.
I have implemented the same "buggy" code after my changes and this is the result I have got:
The output of
mypy:Now we are able to detect type related errors in the codebase. To replicate the changes copy the two code examples into the
in_file_path.pyfile (one for the main repository and the other one for my fork) and test it with using the latest version ofmypythat I have used to test it.As of right now, I have only did the changes for the
in_file_path.pyfile to show that by simply replacingAnywith a concrete model, the current implementation immediately benefits from static analysis, allowing type checkers to catch type related errors that were previously invisible during development.Linked Issues
closes: #175
Type of Change
Contributors
Reminder