Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion asdf/_tests/tags/core/tests/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ def with_custom_extension():
datatype: int16
- name: b
datatype: ['ascii', 16]
exact_datatype: true""",
exact_datatype: true
e:
anyOf:
- type: "null"
- datatype: int16""",
"http://nowhere.org/schemas/custom/ndim-1.0.0": """%YAML 1.1
---
$schema: "http://stsci.edu/schemas/asdf/asdf-schema-1.1.0"
Expand Down Expand Up @@ -803,6 +807,15 @@ def test_datatype_validation(tmp_path):
):
pass

content = """
obj: !<tag:nowhere.org:custom/datatype-1.0.0>
e: null
"""
buff = helpers.yaml_to_asdf(content)

with asdf.open(buff):
pass


@with_custom_extension()
def test_structured_datatype_validation(tmp_path):
Expand Down
12 changes: 10 additions & 2 deletions asdf/tags/core/ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,12 +528,14 @@
in_datatype, _ = numpy_dtype_to_asdf_datatype(array.dtype)
else:
msg = "Not an array"
raise ValidationError(msg)
yield ValidationError(msg)
return

Check warning on line 532 in asdf/tags/core/ndarray.py

View check run for this annotation

Codecov / codecov/patch

asdf/tags/core/ndarray.py#L531-L532

Added lines #L531 - L532 were not covered by tests
elif isinstance(instance, (np.ndarray, NDArrayType)):
in_datatype, _ = numpy_dtype_to_asdf_datatype(instance.dtype)
else:
msg = "Not an array"
raise ValidationError(msg)
yield ValidationError(msg)
return

Check warning on line 538 in asdf/tags/core/ndarray.py

View check run for this annotation

Codecov / codecov/patch

asdf/tags/core/ndarray.py#L537-L538

Added lines #L537 - L538 were not covered by tests

# We are only concerned with some fields from the datatype
# object in the schema so if the schema datatype is structured
Expand All @@ -547,23 +549,28 @@

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

Check warning on line 552 in asdf/tags/core/ndarray.py

View check run for this annotation

Codecov / codecov/patch

asdf/tags/core/ndarray.py#L552

Added line #L552 was not covered by tests

np_datatype = asdf_datatype_to_numpy_dtype(datatype)
np_in_datatype = asdf_datatype_to_numpy_dtype(in_datatype)

if not np_datatype.fields:
if np_in_datatype.fields:
yield ValidationError(f"Expected scalar datatype '{datatype}', got '{in_datatype}'")
return

Check warning on line 560 in asdf/tags/core/ndarray.py

View check run for this annotation

Codecov / codecov/patch

asdf/tags/core/ndarray.py#L560

Added line #L560 was not covered by tests

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

else:
if not np_in_datatype.fields:
yield ValidationError(f"Expected structured datatype '{datatype}', got '{in_datatype}'")
return

Check warning on line 569 in asdf/tags/core/ndarray.py

View check run for this annotation

Codecov / codecov/patch

asdf/tags/core/ndarray.py#L569

Added line #L569 was not covered by tests

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

Check warning on line 573 in asdf/tags/core/ndarray.py

View check run for this annotation

Codecov / codecov/patch

asdf/tags/core/ndarray.py#L573

Added line #L573 was not covered by tests

for i in range(len(np_datatype.fields)):
in_type = np_in_datatype[i]
Expand All @@ -574,3 +581,4 @@
f"Expected {numpy_dtype_to_asdf_datatype(out_type)[0]}, "
f"got {numpy_dtype_to_asdf_datatype(in_type)[0]}",
)
return

Check warning on line 584 in asdf/tags/core/ndarray.py

View check run for this annotation

Codecov / codecov/patch

asdf/tags/core/ndarray.py#L584

Added line #L584 was not covered by tests
1 change: 1 addition & 0 deletions changes/1904.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yield instead of raise ValidationError in validate_datatype to allow use in schema combiners
Loading