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

Experiment with making float/int types inherit from np.floating/np.integer. #259

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ml_dtypes/_src/custom_float.h
Original file line number Diff line number Diff line change
Expand Up @@ -849,8 +849,9 @@ template <typename T>
bool RegisterFloatDtype(PyObject* numpy) {
// bases must be a tuple for Python 3.9 and earlier. Change to just pass
// the base type directly when dropping Python 3.9 support.
// Change to PyFloatingArrType_Type once we drop support for NumPy 1.X.
Safe_PyObjectPtr bases(
PyTuple_Pack(1, reinterpret_cast<PyObject*>(&PyGenericArrType_Type)));
PyTuple_Pack(1, reinterpret_cast<PyObject*>(&PyNumberArrType_Type)));
PyObject* type =
PyType_FromSpecWithBases(&CustomFloatType<T>::type_spec, bases.get());
if (!type) {
Expand Down
4 changes: 3 additions & 1 deletion ml_dtypes/_src/intn_numpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -768,8 +768,10 @@ template <typename T>
bool RegisterIntNDtype(PyObject* numpy) {
// bases must be a tuple for Python 3.9 and earlier. Change to just pass
// the base type directly when dropping Python 3.9 support.
// Change to PySignedIntegerArrType_Type/PyUnsignedIntegerArrType_Type once
// we drop support for NumPy 1.X.
Safe_PyObjectPtr bases(
PyTuple_Pack(1, reinterpret_cast<PyObject*>(&PyGenericArrType_Type)));
PyTuple_Pack(1, reinterpret_cast<PyObject*>(&PyNumberArrType_Type)));
PyObject* type =
PyType_FromSpecWithBases(&IntNTypeDescriptor<T>::type_spec, bases.get());
if (!type) {
Expand Down
5 changes: 5 additions & 0 deletions ml_dtypes/tests/custom_float_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,11 @@ def testArgminOnPositiveInfinity(self, float_type):
def testDtypeFromString(self, float_type):
assert np.dtype(float_type.__name__) == np.dtype(float_type)

def testIssubdtype(self, float_type):
# We should switch to np.floating once we drop support for NumPy 1.X.
self.assertTrue(np.issubdtype(float_type, np.number))
self.assertTrue(np.issubdtype(np.dtype(float_type), np.number))


BinaryOp = collections.namedtuple("BinaryOp", ["op"])

Expand Down
7 changes: 7 additions & 0 deletions ml_dtypes/tests/intn_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@ def testCanCast(self, a, b):
((a, b) in allowed_casts), np.can_cast(a, b, casting="safe")
)

@parameterized.product(scalar_type=INTN_TYPES)
def testIssubdtype(self, scalar_type):
# We should switch to np.signedinteger/np.unsignedinteger once we drop
# support for NumPy 1.X.
self.assertTrue(np.issubdtype(scalar_type, np.number))
self.assertTrue(np.issubdtype(np.dtype(scalar_type), np.number))


# Tests for the Python scalar type
@multi_threaded(num_workers=3, skip_tests=["testBinaryUfuncs"])
Expand Down
Loading