-
Notifications
You must be signed in to change notification settings - Fork 842
[type validation] skip unresolved forward ref #3376
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
Conversation
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
| validator(name, value, args) | ||
| elif isinstance(expected_type, type): # simple types | ||
| _validate_simple_type(name, value, expected_type) | ||
| elif isinstance(expected_type, ForwardRef) or isinstance(expected_type, str): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The clause above, isinstance(expected_type, type), is True when expected_type is str. This means or isinstance(expected_type, str) can be removed, correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not really. The above clause is triggered if exp_type = <string> and the one I added is for cases such as exp_type = "torch.tensor"
In other words, the type is not resolved and remains in string version
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, derp, of course -- expected_type is a string instance here, not the type string 🤦
|
(LGTM, but tagging a proper reviewer: @Wauplin ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems reasonable! Could you add a unit test in ./tests/test_utils_strict_dataclass.py for it? Would be good to test both with a ForwardRef and with a string ref.
(note: failing CI is unrelated)
Co-authored-by: Lucain <[email protected]>
|
Failing tests do not look related, so ig this one is ready @Wauplin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of modifying existing tests I would prefer that you keep everything as it is + add a new class ConfigWithForwardRef specifically for this PR. Also you are testing on dtype: Union[ForwardRef("torch.dtype"), str] = "float32" which is not really conclusive since even without the forward ref, "float32" is already a valid string value (so str allows it).
So can you create a config like this:
@strict
@dataclass
class ConfigWithForwardRef:
explicitForwardRef: ForwardRef("torch.dtype")
implicitForwardRef: "torch.dtype"and test both cases?
|
@Wauplin oke, made the test which would fail on main branch. We will test only "forwardRef" and "forwardref" with manual validation fn |
Wauplin
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I pushed a last commit. Let's merge it now. Thanks @zucchini-nlp!
* skip if unresolved forward ref * make style * Update src/huggingface_hub/dataclasses.py Co-authored-by: Lucain <[email protected]> * add forward ref in test cases * update the test * fix * maybe like this? * update tests --------- Co-authored-by: Lucain <[email protected]> Co-authored-by: Lucain <[email protected]>
|
@zucchini-nlp @gante made a 0.35.1 release for you: https://github.com/huggingface/huggingface_hub/releases/tag/v0.35.1 |
|
Amazing, thanks a lot! |
As per title, skip if the typing hint is a
ForwardRef. In transformers we have cases when the type hints are defined with safe guards, which we can't always resolve. It is safer to keep them asForwardRefin those cases and skip strict validationIf we want those fields to be checked strictly, we will add a custom validation function with safe imports in transformers. This PR will unblock huggingface/transformers#40793, where some image typing hints require
Pillow/torchcc @gante