-
Notifications
You must be signed in to change notification settings - Fork 5
feat: type-check-only dtype protocol #31
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
__all__ = ("DType",) | ||
|
||
from typing import Protocol, type_check_only | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Dagnabbit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I honestly have no idea why that is... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I agree |
||
|
||
|
||
@type_check_only | ||
class DType(Protocol): | ||
"""Protocol for classes that represent a data type. | ||
|
||
This `typing.Protocol` is `typing.type_check_only` and cannot be used at | ||
runtime. This limitation is intentional since the array API structurally | ||
defines a ``dtype`` object as anything with an ``__eq__`` method that | ||
compares to another ``dtype`` object. This broad definition means that most | ||
Python objects will satisfy this protocol and can be erroneously considered | ||
a ``dtype``. | ||
|
||
""" | ||
|
||
def __eq__(self, other: object, /) -> bool: | ||
"""Computes the truth value of ``self == other`` in order to test for data type object equality.""" # noqa: E501 | ||
... | ||
Comment on lines
+6
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is equivalent to class DType(Protocol):
__hash__ = None The If you were to add a And just to be clear, I'm not saying that we should replace this protocol with a type alias to So although I certainly appreciate your work here, I just don't think this is a good idea. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are dtypes not guaranteed to be hashable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They neither are nor aren't guaranteed to be hashable. But there's no way to express that in Python's type system. |
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.
this is a pretty good check actually:
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.
As in, we should add specific ignores for this rather a blanket ignore, like I'm doing here?
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.
We should not prohibit dtypes from being unhashable, which is what this does (but not on mypy: python/mypy#18622)