Skip to content

Commit 7d9d897

Browse files
committed
fixup
1 parent e427c7a commit 7d9d897

File tree

2 files changed

+23
-35
lines changed

2 files changed

+23
-35
lines changed

src/zarr/core/array.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,9 @@ async def _create_v3(
318318
await ensure_no_existing_node(store_path, zarr_format=3)
319319

320320
shape = parse_shapelike(shape)
321-
codecs = list(codecs) if codecs is not None else [get_default_array_bytes_codec(dtype)]
321+
codecs = (
322+
list(codecs) if codecs is not None else [get_default_array_bytes_codec(np.dtype(dtype))]
323+
)
322324

323325
if chunk_key_encoding is None:
324326
chunk_key_encoding = ("default", "/")

src/zarr/core/metadata/v3.py

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def __init__(
218218
fill_value = default_fill_value(data_type_parsed)
219219
# we pass a string here rather than an enum to make mypy happy
220220
fill_value_parsed = parse_fill_value(
221-
fill_value, dtype_value=cast(ALL_DTYPES, data_type_parsed.value)
221+
fill_value, dtype=cast(ALL_DTYPES, data_type_parsed.value)
222222
)
223223
attributes_parsed = parse_attributes(attributes)
224224
codecs_parsed_partial = parse_codecs(codecs)
@@ -362,7 +362,7 @@ def update_attributes(self, attributes: dict[str, JSON]) -> Self:
362362
BYTES_DTYPE = Literal["bytes"]
363363
BYTES = np.bytes_
364364

365-
ALL_DTYPES = INTEGER_DTYPE | FLOAT_DTYPE | COMPLEX_DTYPE | STRING_DTYPE | BYTES_DTYPE
365+
ALL_DTYPES = BOOL_DTYPE | INTEGER_DTYPE | FLOAT_DTYPE | COMPLEX_DTYPE | STRING_DTYPE | BYTES_DTYPE
366366

367367

368368
@overload
@@ -407,25 +407,10 @@ def parse_fill_value(
407407
) -> BYTES: ...
408408

409409

410-
# @overload
411-
# def parse_fill_value(
412-
# fill_value: complex | str | bytes | np.generic | Sequence[Any] | bool | None,
413-
# dtype: np.dtype[Any],
414-
# ) -> Any:
415-
# # This dtype[Any] is unfortunately necessary right now.
416-
# # See https://github.com/zarr-developers/zarr-python/issues/2131#issuecomment-2318010899
417-
# # for more details, but `dtype` here (which comes from `parse_dtype`)
418-
# # is np.dtype[Any].
419-
# #
420-
# # If you want the specialized types rather than Any, you need to use `np.dtypes.<dtype>`
421-
# # rather than np.dtypes(<type>)
422-
# ...
423-
424-
425410
def parse_fill_value(
426-
fill_value: complex | str | bytes | np.generic | Sequence[Any] | bool,
427-
dtype_value: ALL_DTYPES,
428-
) -> BOOL | INTEGER | FLOAT | COMPLEX | Any:
411+
fill_value: Any,
412+
dtype: ALL_DTYPES,
413+
) -> Any:
429414
"""
430415
Parse `fill_value`, a potential fill value, into an instance of `dtype`, a data type.
431416
If `fill_value` is `None`, then this function will return the result of casting the value 0
@@ -446,34 +431,32 @@ def parse_fill_value(
446431
-------
447432
A scalar instance of `dtype`
448433
"""
449-
print("dtype_value", dtype_value)
450-
dtype = DataType(dtype_value)
434+
print("dtype_value", dtype)
435+
data_type = DataType(dtype)
451436
if fill_value is None:
452437
raise ValueError("Fill value cannot be None")
453-
if dtype == DataType.string:
438+
if data_type == DataType.string:
454439
return np.str_(fill_value)
455-
if dtype == DataType.bytes:
440+
if data_type == DataType.bytes:
456441
return np.bytes_(fill_value)
457442

458443
# the rest are numeric types
459-
np_dtype = dtype.to_numpy()
444+
np_dtype = data_type.to_numpy()
460445

461446
if isinstance(fill_value, Sequence) and not isinstance(fill_value, str):
462-
if dtype in (DataType.complex64, DataType.complex128):
447+
if data_type in (DataType.complex64, DataType.complex128):
463448
# dtype = cast(np.dtypes.Complex64DType | np.dtypes.Complex128DType, np_dtype)
464449
if len(fill_value) == 2:
465450
# complex datatypes serialize to JSON arrays with two elements
466451
return np_dtype.type(complex(*fill_value))
467452
else:
468453
msg = (
469-
f"Got an invalid fill value for complex data type {dtype.value}."
454+
f"Got an invalid fill value for complex data type {data_type.value}."
470455
f"Expected a sequence with 2 elements, but {fill_value!r} has "
471456
f"length {len(fill_value)}."
472457
)
473458
raise ValueError(msg)
474-
msg = (
475-
f"Cannot parse non-string sequence {fill_value!r} as a scalar with type {dtype.value}."
476-
)
459+
msg = f"Cannot parse non-string sequence {fill_value!r} as a scalar with type {data_type.value}."
477460
raise TypeError(msg)
478461

479462
# Cast the fill_value to the given dtype
@@ -486,7 +469,7 @@ def parse_fill_value(
486469
warnings.filterwarnings("ignore", category=DeprecationWarning)
487470
casted_value = np.dtype(np_dtype).type(fill_value)
488471
except (ValueError, OverflowError, TypeError) as e:
489-
raise ValueError(f"fill value {fill_value!r} is not valid for dtype {dtype}") from e
472+
raise ValueError(f"fill value {fill_value!r} is not valid for dtype {data_type}") from e
490473
# Check if the value is still representable by the dtype
491474
if fill_value == "NaN" and np.isnan(casted_value):
492475
pass
@@ -497,15 +480,18 @@ def parse_fill_value(
497480
# so we us np.isclose for this comparison.
498481
# this also allows us to compare nan fill_values
499482
if not np.isclose(fill_value, casted_value, equal_nan=True):
500-
raise ValueError(f"fill value {fill_value!r} is not valid for dtype {dtype}")
483+
raise ValueError(f"fill value {fill_value!r} is not valid for dtype {data_type}")
501484
else:
502485
if fill_value != casted_value:
503-
raise ValueError(f"fill value {fill_value!r} is not valid for dtype {dtype}")
486+
raise ValueError(f"fill value {fill_value!r} is not valid for dtype {data_type}")
504487

505488
return casted_value
506489

507490

508-
def default_fill_value(dtype: DataType) -> str | bytes | np.generic:
491+
def default_fill_value(dtype: DataType) -> Any:
492+
# TODO: the static types could maybe be narrowed here.
493+
# mypy knows that np.dtype("int64").type(0) is an int64.
494+
# so maybe DataType needs to be generic?
509495
if dtype == DataType.string:
510496
return ""
511497
elif dtype == DataType.bytes:

0 commit comments

Comments
 (0)