Skip to content

Commit e570d18

Browse files
committed
json_encoder dtype handling
1 parent 6a0b19e commit e570d18

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/thalas/utilities/json_encoder.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,19 @@ def default(self, obj):
2626
return {"days": obj.days, "seconds": obj.seconds, "microseconds": obj.microseconds}
2727
if isinstance(obj, Path):
2828
return str(obj)
29-
# Detect any array adhering to the Python Array API Standard;
30-
# see https://github.com/data-apis/array-api/issues/150
31-
if hasattr(obj, "__array_namespace__"):
29+
# Handle dtype objects from numpy/jax/torch/etc
30+
# These are type objects, not instances, so check for dtype-like attributes
31+
if self._is_dtype(obj):
3232
return str(obj)
3333
try:
3434
return super().default(obj)
3535
except TypeError:
3636
logger.warning(f"Could not serialize object of type {type(obj)}: {obj}")
3737
return str(obj)
38+
39+
@staticmethod
40+
def _is_dtype(obj):
41+
"""Check if obj is a dtype object from numpy/jax/torch/etc."""
42+
# Dtype objects have 'name' and 'itemsize' attributes but no 'shape'
43+
# This distinguishes them from array instances
44+
return hasattr(obj, "name") and hasattr(obj, "itemsize") and not hasattr(obj, "shape")

0 commit comments

Comments
 (0)