Skip to content

Commit 2c83833

Browse files
authored
Merge pull request #485 from bioimage-io/forward_compat
Report on forward compatibility attempt in validation errors
2 parents 662c33c + 068a0b6 commit 2c83833

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

bioimageio/spec/io_.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from types import ModuleType
1313
from typing import Dict, IO, Optional, Sequence, Tuple, Union
1414

15-
from marshmallow import missing
15+
from marshmallow import ValidationError, missing
1616
from packaging.version import Version
1717

1818
from bioimageio.spec.shared import RDF_NAMES, raw_nodes, resolve_rdf_source, resolve_rdf_source_and_type, resolve_source
@@ -199,7 +199,8 @@ def load_raw_resource_description(
199199
# => 'format_version' may be invalid or the issue lies with 'type_'...
200200
raise e
201201

202-
if odv and Version(sub_spec.format_version) < odv:
202+
downgrade_format_version = odv and Version(sub_spec.format_version) < odv
203+
if downgrade_format_version:
203204
warnings.warn(
204205
f"Loading future {type_} format version {original_data_version} as (latest known) "
205206
f"{sub_spec.format_version}."
@@ -218,7 +219,16 @@ def load_raw_resource_description(
218219
schema: SharedBioImageIOSchema = getattr(sub_spec.schema, class_name)()
219220

220221
data = sub_spec.converters.maybe_convert(data)
221-
raw_rd = schema.load(data)
222+
try:
223+
raw_rd = schema.load(data)
224+
except ValidationError as e:
225+
if downgrade_format_version:
226+
e.messages["format_version"] = (
227+
f"Other errors may be caused by a possibly incompatible future format version {original_data_version} "
228+
f"treated as {sub_spec.format_version}."
229+
)
230+
231+
raise e
222232

223233
if isinstance(root, pathlib.Path):
224234
root = root.resolve()

tests/test_validation_errors.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ def test_list_instead_of_nested_schema(unet2d_nuclei_broad_latest):
1111
data["run_mode"] = [{"name": "something"}]
1212

1313
error = validate(data)["error"]
14-
print(error)
1514
assert len(error) == 1
1615
assert error["run_mode"] == ["Expected dictionary, but got list."]
16+
17+
18+
def test_forward_compatibility_error(unet2d_fixed_shape):
19+
from bioimageio.spec.commands import validate
20+
21+
data = yaml.load(unet2d_fixed_shape)
22+
23+
data["authors"] = 42 # make sure rdf is invalid
24+
data["format_version"] = "9999.0.0" # assume it is valid in a future format version
25+
26+
error = validate(data)["error"]
27+
28+
# even though the format version is correctly formatted, it should be mentioned here as we treat the future format
29+
# version as the current latest. If this attempted forward compatibility fails we have to report that we did it.
30+
assert "format_version" in error

0 commit comments

Comments
 (0)