Skip to content

Conversation

@zucchini-nlp
Copy link
Member

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 as ForwardRef in those cases and skip strict validation

If 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/torch

cc @gante

@HuggingFaceDocBuilderDev

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):
Copy link
Contributor

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?

Copy link
Member Author

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

Copy link
Contributor

@gante gante Sep 19, 2025

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 🤦

@gante
Copy link
Contributor

gante commented Sep 19, 2025

(LGTM, but tagging a proper reviewer: @Wauplin )

@gante gante requested a review from Wauplin September 19, 2025 10:30
Copy link
Contributor

@Wauplin Wauplin left a 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)

@zucchini-nlp
Copy link
Member Author

Failing tests do not look related, so ig this one is ready @Wauplin

Copy link
Contributor

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?

@zucchini-nlp
Copy link
Member Author

@Wauplin oke, made the test which would fail on main branch. We will test only "forwardRef" and "forwardref" with manual validation fn

Copy link
Contributor

@Wauplin Wauplin left a 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!

@Wauplin Wauplin merged commit fd294b4 into huggingface:main Sep 23, 2025
20 of 21 checks passed
Wauplin added a commit that referenced this pull request Sep 23, 2025
* 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]>
@Wauplin
Copy link
Contributor

Wauplin commented Sep 23, 2025

@zucchini-nlp @gante made a 0.35.1 release for you: https://github.com/huggingface/huggingface_hub/releases/tag/v0.35.1

@zucchini-nlp
Copy link
Member Author

Amazing, thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants