Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datatype validator fix #1904

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 @@ -526,35 +526,42 @@
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 530 in asdf/tags/core/ndarray.py

View check run for this annotation

Codecov / codecov/patch

asdf/tags/core/ndarray.py#L529-L530

Added lines #L529 - L530 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 536 in asdf/tags/core/ndarray.py

View check run for this annotation

Codecov / codecov/patch

asdf/tags/core/ndarray.py#L535-L536

Added lines #L535 - L536 were not covered by tests

if datatype == in_datatype:
return

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

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

View check run for this annotation

Codecov / codecov/patch

asdf/tags/core/ndarray.py#L543

Added line #L543 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 551 in asdf/tags/core/ndarray.py

View check run for this annotation

Codecov / codecov/patch

asdf/tags/core/ndarray.py#L551

Added line #L551 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 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 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 564 in asdf/tags/core/ndarray.py

View check run for this annotation

Codecov / codecov/patch

asdf/tags/core/ndarray.py#L564

Added line #L564 was not covered by tests

for i in range(len(np_datatype.fields)):
in_type = np_in_datatype[i]
Expand All @@ -565,3 +572,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 575 in asdf/tags/core/ndarray.py

View check run for this annotation

Codecov / codecov/patch

asdf/tags/core/ndarray.py#L575

Added line #L575 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