feat(validate): accept dataclass instances as input - #118
Merged
Conversation
Previously validate() required dict input even when the schema type was a dataclass. Now dataclass instances are auto-converted via vars() at each nesting level, enabling recursive validation of nested dataclass trees and cross-type validation (DC instance against TypedDict schema).
…e conversion
Addresses review feedback:
1. vars() returns __dict__ reference — mutating the returned dict would
alter the original instance. Now builds a fresh dict via
{f.name: getattr(value, f.name) for f in dataclasses.fields(value)}.
2. dataclass(slots=True) removes __dict__, causing vars() to raise
TypeError. dataclasses.fields() + getattr() works with both regular
and slotted dataclasses.
Adds tests for slots=True support and mutation safety.
Oaklight
commented
Jul 16, 2026
Oaklight
left a comment
Owner
Author
There was a problem hiding this comment.
Review feedback addressed:
__dict__aliasing — switched fromvars()to{f.name: getattr(value, f.name) for f in dataclasses.fields(value)}, returns a fresh dict that won't mutate the original instance.slots=Truesupport —dataclasses.fields()+getattr()works regardless of whether__dict__exists.
Both issues covered by new tests (test_instance_slots, test_instance_no_mutation). All 129 tests pass, pre-commit clean. Bilingual docs updated with corrected implementation details and pushed to docs_en/docs_zh branches.
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
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.
Summary
validate()now accepts dataclass instances directly — no need to manually convert to dict firstdataclasses.fields()+getattr()at each nesting level, so nested dataclass trees are validated recursivelyslots=TruedataclassesValidationError: Expected dict, got <ClassName>Implementation
2-line change in
_validate_struct_fields: check if value is a dataclass instance, convert via{f.name: getattr(value, f.name) for f in dataclasses.fields(value)}before the existingisinstance(value, dict)guard. Each nesting level does its own lazy conversion. This approach:__dict__aliasing — safe to mutate)slots=Truedataclasses (no__dict__needed)Test plan
slots=True, no-mutation safetyDocs
Bilingual docs updated in docs_en/docs_zh branches:
dataparameter description[Unreleased]