New @strict_dataclass
decorator for dataclass validation
#2895
+513
−1
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.
Follow-up after huggingface/transformers#36329 and slack discussions (private).
The idea is to add a layer of validation on top of Python's built-in dataclasses.
Example
What it does ?
@strict_dataclass
built on top of@dataclass
. When decorated, class values are validated.Dict[str, List[Optional[DummyClass]]
is correctly validated)validated_field
(built on top offield
)What it doesn't do (yet) ?
__post_init__
and then "on-demand" with a.validate()
method?. We cannot run them on each field-assignment as it would prevent modifying related value (if values A and B must be coherent, we want to be able to change both A and B and then validate).Why not chose pydantic ? (or
attrs
? ormarshmallow_dataclass
?)pydantic
as a base requirement to 🤗transformers
transformers#36329 related to adding pydantic as a new dependency. Would be an heavy addition + require careful logic to support both v1 and v2.@strict_dataclass
is not meant for heavy load where performances is critical. Common use case will be to validate a model configuration (only done once and very neglectable compared to running a model). This allows us to keep code minimal.Plan:
transformers
@gante)We won't push for it / release it until we are sure at least the
transformers
use case is covered.Notes:
This
@strict_dataclass
might be useful inhuggingface_hub
itself in the future but that's not its primary goal for now.