Skip to content
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

Fix typing.get_type_hints call on a ModelHubMixin #2729

Merged

Conversation

aliberts
Copy link
Contributor

@aliberts aliberts commented Jan 3, 2025

Description

Proposed fix for #2727

typing.get_type_hints tries to resolve the type of an object at runtime. This means that the global and local namespaces this function uses won't have access to whatever has been defined only for static type checkers (typically using typing.TYPE_CHECKING).

In this case, DataclassInstance is only defined in such a way.
This proposes to add a default fallback definition of DataclassInstance for runtime type checking.

@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.

Copy link
Contributor

@hanouticelina hanouticelina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @aliberts for the fix! all good to me :)
failing tests are unrelated.

Comment on lines 28 to 36
from _typeshed import DataclassInstance # type: ignore
else:

class DataclassInstance(Protocol): # type: ignore
__dataclass_fields__: ClassVar[Dict[str, Field]]


Dataclass = TypeVar("Dataclass", bound=DataclassInstance)
DataclassType = Type[Dataclass]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from _typeshed import DataclassInstance # type: ignore
else:
class DataclassInstance(Protocol): # type: ignore
__dataclass_fields__: ClassVar[Dict[str, Field]]
Dataclass = TypeVar("Dataclass", bound=DataclassInstance)
DataclassType = Type[Dataclass]
# Type alias for dataclass instances
class DataclassInstance(Protocol):
__dataclass_fields__: ClassVar[dict[str, Field[Any]]]

not a strong opinion on that and feel free to ignore, but maybe we can just define DataclassInstance directly here and remove the if/else and _typeshed dependency.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, fixed: b1b9f5f

@hanouticelina
Copy link
Contributor

Added a test in a5c49e4. thanks again @aliberts for the PR, we're good to merge once the CI is 🟢 I will release a patch with this fix shortly!

@aliberts
Copy link
Contributor Author

aliberts commented Jan 6, 2025

@hanouticelina not sure why but that definition doesn't work with python 3.8 as it's saying that dataclass.Field is not subscriptable. I've replaced Field[Any] with Field (93b53a7), should be good this time.

@hanouticelina hanouticelina merged commit 41694bd into huggingface:main Jan 6, 2025
14 checks passed
hanouticelina added a commit that referenced this pull request Jan 6, 2025
* Add DataclassInstance for runtime type_checking

* Add suggestion from code review

* Fix type annotation

* Add test

* Actually fix type annotation (3.8)

---------

Co-authored-by: Celina Hanouti <hanouticelina@gmail.com>
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.

3 participants