-
Notifications
You must be signed in to change notification settings - Fork 590
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
Fix typing.get_type_hints
call on a ModelHubMixin
#2729
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. |
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.
thanks @aliberts for the fix! all good to me :)
failing tests are unrelated.
src/huggingface_hub/hub_mixin.py
Outdated
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] |
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.
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.
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.
Agreed, fixed: b1b9f5f
@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 |
* 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>
Description
Proposed fix for #2727
typing.get_type_hints
tries to resolve the type of an object at runtime. This means that theglobal
andlocal
namespaces this function uses won't have access to whatever has been defined only for static type checkers (typically usingtyping.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.