@@ -426,19 +426,25 @@ def parse_metadata_field_v3(value: object) -> ZarrV3MetadataFieldJSON:
426426 return cast (ZarrV3MetadataFieldJSON , normalized )
427427
428428
429- def _is_int_sequence (value : object ) -> bool :
430- """Whether `value` is a non-string sequence of integers.
429+ def _is_array (value : object ) -> TypeGuard [Sequence [object ]]:
430+ """Whether `value` reads as a JSON array: a sequence that is not a string or bytes.
431+
432+ `str`, `bytes` and `bytearray` are sequences to Python, and none of them
433+ is an array to JSON. A `TypeGuard`, not a `TypeIs`: a `str` is a
434+ `Sequence[object]` this says no to.
435+ """
436+ return isinstance (value , Sequence ) and not isinstance (value , (str , bytes , bytearray ))
437+
438+
439+ def _is_int_sequence (value : object ) -> TypeGuard [Sequence [int ]]:
440+ """Whether `value` is a JSON array of integers.
431441
432442 JSON booleans decode to `bool`, which is an `int` subclass in Python but
433- is not an integer in a metadata document, so booleans are excluded.
443+ is not an integer in a metadata document, so booleans are excluded. A
444+ `TypeGuard`, not a `TypeIs`: `bytes` is a `Sequence[int]` this says no to.
434445 """
435- return (
436- not isinstance (value , (str , bytes , bytearray ))
437- and isinstance (value , Sequence )
438- and all (
439- isinstance (item , int ) and not isinstance (item , bool )
440- for item in cast ("Sequence[object]" , value )
441- )
446+ return _is_array (value ) and all (
447+ isinstance (item , int ) and not isinstance (item , bool ) for item in value
442448 )
443449
444450
@@ -452,7 +458,7 @@ def _validate_dim_sequence(doc: Mapping[object, object], key: str) -> tuple[Vali
452458 value = doc [key ]
453459 if not _is_int_sequence (value ):
454460 return (ValidationProblem ((key ,), "expected a sequence of int" , "invalid_type" ),)
455- if any (item < 0 for item in cast ( "Sequence[int]" , value ) ):
461+ if any (item < 0 for item in value ):
456462 return (ValidationProblem ((key ,), "expected non-negative integers" , "invalid_value" ),)
457463 return ()
458464
@@ -467,19 +473,16 @@ def _is_dtype_v2(value: object) -> bool:
467473 """
468474 if isinstance (value , str ):
469475 return True
470- if not isinstance (value , Sequence ):
476+ if not _is_array (value ):
471477 return False
472- for record in cast ("Sequence[object]" , value ):
473- if isinstance (record , str ) or not isinstance (record , Sequence ):
474- return False
475- fields = cast ("Sequence[object]" , record )
476- if len (fields ) not in (2 , 3 ):
478+ for record in value :
479+ if not _is_array (record ) or len (record ) not in (2 , 3 ):
477480 return False
478- if not isinstance (fields [0 ], str ):
481+ if not isinstance (record [0 ], str ):
479482 return False
480- if not _is_dtype_v2 (fields [1 ]):
483+ if not _is_dtype_v2 (record [1 ]):
481484 return False
482- if len (fields ) == 3 and not _is_int_sequence (fields [2 ]):
485+ if len (record ) == 3 and not _is_int_sequence (record [2 ]):
483486 return False
484487 return True
485488
@@ -619,16 +622,16 @@ def validate_array_metadata_v3(value: object) -> tuple[ValidationProblem, ...]:
619622 for key in ("codecs" , "storage_transformers" ):
620623 if key in doc :
621624 entries = doc [key ]
622- if isinstance ( entries , str ) or not isinstance (entries , Sequence ):
625+ if not _is_array (entries ):
623626 problems .append (ValidationProblem ((key ,), "expected a sequence" , "invalid_type" ))
624627 else :
625- if key == "codecs" and len (cast ( "Sequence[object]" , entries ) ) == 0 :
628+ if key == "codecs" and len (entries ) == 0 :
626629 problems .append (
627630 ValidationProblem (
628631 ("codecs" ,), "expected at least one codec" , "invalid_value"
629632 )
630633 )
631- for index , entry in enumerate (cast ( "Sequence[object]" , entries ) ):
634+ for index , entry in enumerate (entries ):
632635 problems .extend (_prefix (key , _prefix (index , validate_metadata_field_v3 (entry ))))
633636 if "attributes" in doc :
634637 problems .extend (_validate_attributes (doc ["attributes" ]))
@@ -637,21 +640,18 @@ def validate_array_metadata_v3(value: object) -> tuple[ValidationProblem, ...]:
637640 # field-level loc, not per-bad-item locs; per-index locs are reserved for
638641 # the metadata-field lists (codecs, storage_transformers).
639642 names = doc ["dimension_names" ]
640- if isinstance (names , str ) or not isinstance (names , Sequence ):
643+ shape = doc .get ("shape" )
644+ if not _is_array (names ):
641645 problems .append (
642646 ValidationProblem (("dimension_names" ,), "expected a sequence" , "invalid_type" )
643647 )
644- elif not all (
645- item is None or isinstance (item , str ) for item in cast ("Sequence[object]" , names )
646- ):
648+ elif not all (item is None or isinstance (item , str ) for item in names ):
647649 problems .append (
648650 ValidationProblem (
649651 ("dimension_names" ,), "expected items of str or None" , "invalid_type"
650652 )
651653 )
652- elif _is_int_sequence (doc .get ("shape" )) and len (cast ("Sequence[object]" , names )) != len (
653- cast ("Sequence[int]" , doc ["shape" ])
654- ):
654+ elif _is_int_sequence (shape ) and len (names ) != len (shape ):
655655 problems .append (
656656 ValidationProblem (
657657 ("dimension_names" ,),
@@ -704,22 +704,22 @@ def validate_array_metadata_v2(value: object) -> tuple[ValidationProblem, ...]:
704704 chunks_problems = _validate_dim_sequence (doc , "chunks" )
705705 problems .extend (shape_problems )
706706 problems .extend (chunks_problems )
707+ shape = doc .get ("shape" )
708+ chunks = doc .get ("chunks" )
707709 if (
708710 len (shape_problems ) == 0
709711 and len (chunks_problems ) == 0
710- and _is_int_sequence (doc .get ("shape" ))
711- and _is_int_sequence (doc .get ("chunks" ))
712+ and _is_int_sequence (shape )
713+ and _is_int_sequence (chunks )
714+ and len (shape ) != len (chunks )
712715 ):
713- shape = cast ("Sequence[int]" , doc ["shape" ])
714- chunks = cast ("Sequence[int]" , doc ["chunks" ])
715- if len (shape ) != len (chunks ):
716- problems .append (
717- ValidationProblem (
718- ("chunks" ,),
719- "expected the same number of dimensions as shape" ,
720- "invalid_value" ,
721- )
716+ problems .append (
717+ ValidationProblem (
718+ ("chunks" ,),
719+ "expected the same number of dimensions as shape" ,
720+ "invalid_value" ,
722721 )
722+ )
723723 if "dtype" in doc and not _is_dtype_v2 (doc ["dtype" ]):
724724 problems .append (
725725 ValidationProblem (
@@ -741,9 +741,7 @@ def validate_array_metadata_v2(value: object) -> tuple[ValidationProblem, ...]:
741741 if "filters" in doc :
742742 filters = doc ["filters" ]
743743 if filters is not None and (
744- isinstance (filters , str )
745- or not isinstance (filters , Sequence )
746- or not all (_is_codec_v2 (item ) for item in cast ("Sequence[object]" , filters ))
744+ not _is_array (filters ) or not all (_is_codec_v2 (item ) for item in filters )
747745 ):
748746 problems .append (
749747 ValidationProblem (
@@ -752,10 +750,10 @@ def validate_array_metadata_v2(value: object) -> tuple[ValidationProblem, ...]:
752750 "invalid_type" ,
753751 )
754752 )
755- elif filters is not None :
753+ elif _is_array ( filters ) :
756754 # "A list of JSON objects providing codec configurations, or
757755 # null" (https://github.com/zarr-developers/zarr-specs/blob/fc7dd9c9beb5a50b87f9b08b00bf50fc0048482f/docs/v2/v2.0.rst#L76-L79): an empty list is a list.
758- for index , item in enumerate (cast ( "Sequence[object]" , filters ) ):
756+ for index , item in enumerate (filters ):
759757 problems .extend (_prefix ("filters" , _prefix (index , validate_json (item ))))
760758 if "dimension_separator" in doc and doc ["dimension_separator" ] not in ("." , "/" ):
761759 problems .append (
0 commit comments