Skip to content

Commit 513ddf1

Browse files
authored
Merge pull request #1904 from braingram/datatype_validator_fix
Datatype validator fix
2 parents 291528b + 6988c91 commit 513ddf1

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

asdf/_tests/tags/core/tests/test_ndarray.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ def with_custom_extension():
9999
datatype: int16
100100
- name: b
101101
datatype: ['ascii', 16]
102-
exact_datatype: true""",
102+
exact_datatype: true
103+
e:
104+
anyOf:
105+
- type: "null"
106+
- datatype: int16""",
103107
"http://nowhere.org/schemas/custom/ndim-1.0.0": """%YAML 1.1
104108
---
105109
$schema: "http://stsci.edu/schemas/asdf/asdf-schema-1.1.0"
@@ -803,6 +807,15 @@ def test_datatype_validation(tmp_path):
803807
):
804808
pass
805809

810+
content = """
811+
obj: !<tag:nowhere.org:custom/datatype-1.0.0>
812+
e: null
813+
"""
814+
buff = helpers.yaml_to_asdf(content)
815+
816+
with asdf.open(buff):
817+
pass
818+
806819

807820
@with_custom_extension()
808821
def test_structured_datatype_validation(tmp_path):

asdf/tags/core/ndarray.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,12 +528,14 @@ def validate_datatype(validator, datatype, instance, schema):
528528
in_datatype, _ = numpy_dtype_to_asdf_datatype(array.dtype)
529529
else:
530530
msg = "Not an array"
531-
raise ValidationError(msg)
531+
yield ValidationError(msg)
532+
return
532533
elif isinstance(instance, (np.ndarray, NDArrayType)):
533534
in_datatype, _ = numpy_dtype_to_asdf_datatype(instance.dtype)
534535
else:
535536
msg = "Not an array"
536-
raise ValidationError(msg)
537+
yield ValidationError(msg)
538+
return
537539

538540
# We are only concerned with some fields from the datatype
539541
# object in the schema so if the schema datatype is structured
@@ -547,23 +549,28 @@ def validate_datatype(validator, datatype, instance, schema):
547549

548550
if schema.get("exact_datatype", False):
549551
yield ValidationError(f"Expected datatype '{datatype}', got '{in_datatype}'")
552+
return
550553

551554
np_datatype = asdf_datatype_to_numpy_dtype(datatype)
552555
np_in_datatype = asdf_datatype_to_numpy_dtype(in_datatype)
553556

554557
if not np_datatype.fields:
555558
if np_in_datatype.fields:
556559
yield ValidationError(f"Expected scalar datatype '{datatype}', got '{in_datatype}'")
560+
return
557561

558562
if not np.can_cast(np_in_datatype, np_datatype, "safe"):
559563
yield ValidationError(f"Can not safely cast from '{in_datatype}' to '{datatype}' ")
564+
return
560565

561566
else:
562567
if not np_in_datatype.fields:
563568
yield ValidationError(f"Expected structured datatype '{datatype}', got '{in_datatype}'")
569+
return
564570

565571
if len(np_in_datatype.fields) != len(np_datatype.fields):
566572
yield ValidationError(f"Mismatch in number of columns: Expected {len(datatype)}, got {len(in_datatype)}")
573+
return
567574

568575
for i in range(len(np_datatype.fields)):
569576
in_type = np_in_datatype[i]
@@ -574,3 +581,4 @@ def validate_datatype(validator, datatype, instance, schema):
574581
f"Expected {numpy_dtype_to_asdf_datatype(out_type)[0]}, "
575582
f"got {numpy_dtype_to_asdf_datatype(in_type)[0]}",
576583
)
584+
return

changes/1904.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
yield instead of raise ValidationError in validate_datatype to allow use in schema combiners

0 commit comments

Comments
 (0)