@@ -216,8 +216,7 @@ def from_dict(cls, data: dict[str, JSON]) -> Self:
216216 _ = parse_node_type_array (_data .pop ("node_type" ))
217217
218218 # check that the data_type attribute is valid
219- if _data ["data_type" ] not in DataType :
220- raise ValueError (f"Invalid V3 data_type: { _data ['data_type' ]} " )
219+ _ = DataType (_data ["data_type" ])
221220
222221 # dimension_names key is optional, normalize missing to `None`
223222 _data ["dimension_names" ] = _data .pop ("dimension_names" , None )
@@ -264,23 +263,38 @@ def update_attributes(self, attributes: dict[str, JSON]) -> Self:
264263
265264
266265@overload
267- def parse_fill_value (fill_value : object , dtype : BOOL_DTYPE ) -> BOOL : ...
266+ def parse_fill_value (
267+ fill_value : int | float | complex | str | bytes | np .generic | Sequence [Any ] | bool | None ,
268+ dtype : BOOL_DTYPE ,
269+ ) -> BOOL : ...
268270
269271
270272@overload
271- def parse_fill_value (fill_value : object , dtype : INTEGER_DTYPE ) -> INTEGER : ...
273+ def parse_fill_value (
274+ fill_value : int | float | complex | str | bytes | np .generic | Sequence [Any ] | bool | None ,
275+ dtype : INTEGER_DTYPE ,
276+ ) -> INTEGER : ...
272277
273278
274279@overload
275- def parse_fill_value (fill_value : object , dtype : FLOAT_DTYPE ) -> FLOAT : ...
280+ def parse_fill_value (
281+ fill_value : int | float | complex | str | bytes | np .generic | Sequence [Any ] | bool | None ,
282+ dtype : FLOAT_DTYPE ,
283+ ) -> FLOAT : ...
276284
277285
278286@overload
279- def parse_fill_value (fill_value : object , dtype : COMPLEX_DTYPE ) -> COMPLEX : ...
287+ def parse_fill_value (
288+ fill_value : int | float | complex | str | bytes | np .generic | Sequence [Any ] | bool | None ,
289+ dtype : COMPLEX_DTYPE ,
290+ ) -> COMPLEX : ...
280291
281292
282293@overload
283- def parse_fill_value (fill_value : object , dtype : np .dtype [Any ]) -> Any :
294+ def parse_fill_value (
295+ fill_value : int | float | complex | str | bytes | np .generic | Sequence [Any ] | bool | None ,
296+ dtype : np .dtype [Any ],
297+ ) -> Any :
284298 # This dtype[Any] is unfortunately necessary right now.
285299 # See https://github.com/zarr-developers/zarr-python/issues/2131#issuecomment-2318010899
286300 # for more details, but `dtype` here (which comes from `parse_dtype`)
@@ -292,7 +306,7 @@ def parse_fill_value(fill_value: object, dtype: np.dtype[Any]) -> Any:
292306
293307
294308def parse_fill_value (
295- fill_value : object ,
309+ fill_value : int | float | complex | str | bytes | np . generic | Sequence [ Any ] | bool | None ,
296310 dtype : BOOL_DTYPE | INTEGER_DTYPE | FLOAT_DTYPE | COMPLEX_DTYPE | np .dtype [Any ],
297311) -> BOOL | INTEGER | FLOAT | COMPLEX | Any :
298312 """
@@ -326,11 +340,11 @@ def parse_fill_value(
326340 else :
327341 msg = (
328342 f"Got an invalid fill value for complex data type { dtype } ."
329- f"Expected a sequence with 2 elements, but { fill_value } has "
343+ f"Expected a sequence with 2 elements, but { fill_value !r } has "
330344 f"length { len (fill_value )} ."
331345 )
332346 raise ValueError (msg )
333- msg = f"Cannot parse non-string sequence { fill_value } as a scalar with type { dtype } ."
347+ msg = f"Cannot parse non-string sequence { fill_value !r } as a scalar with type { dtype } ."
334348 raise TypeError (msg )
335349
336350 # Cast the fill_value to the given dtype
@@ -339,7 +353,7 @@ def parse_fill_value(
339353 except (ValueError , OverflowError , TypeError ) as e :
340354 raise ValueError (f"fill value { fill_value !r} is not valid for dtype { dtype } " ) from e
341355 # Check if the value is still representable by the dtype
342- if fill_value != casted_value :
356+ if fill_value != casted_value and not ( np . isnan ( fill_value ) and np . isnan ( casted_value )) :
343357 raise ValueError (f"fill value { fill_value !r} is not valid for dtype { dtype } " )
344358
345359 return casted_value
@@ -434,7 +448,7 @@ def from_dtype(cls, dtype: np.dtype[Any]) -> DataType:
434448def parse_dtype (data : npt .DTypeLike ) -> np .dtype [Any ]:
435449 try :
436450 dtype = np .dtype (data )
437- except TypeError as e :
451+ except ( ValueError , TypeError ) as e :
438452 raise ValueError (f"Invalid V3 data_type: { data } " ) from e
439453 # check that this is a valid v3 data_type
440454 try :
0 commit comments