Skip to content

Commit fd37e1c

Browse files
committed
Use numbers instead of numpy module
1 parent 76c9125 commit fd37e1c

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

zarr/tests/test_util.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,7 @@ def test_all_equal():
244244

245245
def test_json_dumps_numpy_dtype():
246246
assert json_dumps(np.int64(0)) == json_dumps(0)
247-
assert json_dumps(np.float64(0)) == json_dumps(float(0))
248-
assert json_dumps(np.array([0, 1])) == json_dumps([0, 1])
247+
assert json_dumps(np.float32(0)) == json_dumps(float(0))
249248
# Check that we raise the error of the superclass for unsupported object
250249
with pytest.raises(TypeError):
251250
json_dumps(Array)

zarr/util.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,22 @@ def flatten(arg: Iterable) -> Iterable:
3333
}
3434

3535

36-
class NumpyEncoder(json.JSONEncoder):
36+
class NumberEncoder(json.JSONEncoder):
3737

3838
def default(self, o):
3939
# See json.JSONEncoder.default docstring for explanation
40-
if isinstance(o, np.integer):
40+
# This is necessary to encode numpy dtype
41+
if isinstance(o, numbers.Integral):
4142
return int(o)
42-
if isinstance(o, np.floating):
43+
if isinstance(o, numbers.Real):
4344
return float(o)
44-
if isinstance(o, np.ndarray):
45-
return o.tolist()
4645
return json.JSONEncoder.default(self, o)
4746

4847

4948
def json_dumps(o: Any) -> bytes:
5049
"""Write JSON in a consistent, human-readable way."""
5150
return json.dumps(o, indent=4, sort_keys=True, ensure_ascii=True,
52-
separators=(',', ': '), cls=NumpyEncoder).encode('ascii')
51+
separators=(',', ': '), cls=NumberEncoder).encode('ascii')
5352

5453

5554
def json_loads(s: str) -> Dict[str, Any]:

0 commit comments

Comments
 (0)