ExplicitEnum subclass str (JSON dump compatible)#17933
Merged
sgugger merged 2 commits intohuggingface:mainfrom Jun 29, 2022
BramVanroy:patch-1
Merged
ExplicitEnum subclass str (JSON dump compatible)#17933sgugger merged 2 commits intohuggingface:mainfrom BramVanroy:patch-1
sgugger merged 2 commits intohuggingface:mainfrom
BramVanroy:patch-1
Conversation
|
The documentation is not available anymore as the PR was closed or merged. |
Collaborator
Author
|
The following tests are failing but that seems unrelated: tests/pipelines/test_pipelines_object_detection.py::ObjectDetectionPipelineTests::test_small_model_pt |
Collaborator
|
Yes, I skipped those tests on main for now. Let me play a little bit with this, it seems like a good idea but I want to make sure it doesn't break anything before merging. |
sgugger
approved these changes
Jun 29, 2022
Collaborator
|
Tested and it all looks good, thanks a lot! |
5 tasks
viclzhu
pushed a commit
to viclzhu/transformers
that referenced
this pull request
Jul 18, 2022
* ExplicitEnum subclass str (JSON dump compatible) * allow union if one of the types is str
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.
I found that when I wanted to write the parsed dataclasses that I get from
HfArgumentParser.parse_args_into_dataclasses()to JSON, that I would get JSON errors. The reason being thatTypeError: Object of type IntervalStrategy is not JSON serializable. While this is understandable (Enum members are not serializable), this is not ideal withintransformers.I checked all items in
transformersthat subclassExplicitEnumand it seems that they are allstr-only Enums. That would allow us to have them inherit fromstr, too, which solves the JSON issue. JSON can then make use of itsstrclass for serialization. Below is a minimal - but full - example to show how this would work:A consequence is that now these ExplicitEnums will have a Union type, which originally lead to issues when using
HfArgumentParser._parse_dataclass_field. Therefore, I added an exception to_parse_dataclass_fieldto allow for a Union if one of the types isstr, assuming that a given string value to the argparser will be resolved correctly, because it is one of the accepted types.Who can review?
@sgugger