diff --git a/tests/test_schema.py b/tests/test_schema.py index f3bb313..7e025f0 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -4,6 +4,7 @@ import pystac import pytest +from jsonschema.exceptions import ValidationError from pystac.validation.stac_validator import STACValidator from stac_model.base import JSON @@ -11,6 +12,9 @@ from conftest import get_all_stac_item_examples +# ignore typing errors introduced by generic JSON manipulation errors +# mypy: disable_error_code="arg-type,call-overload,index,union-attr" + @pytest.mark.parametrize( "mlm_example", # value passed to 'mlm_example' fixture @@ -34,7 +38,7 @@ def test_mlm_schema( ) def test_mlm_no_undefined_prefixed_field_item_properties( mlm_validator: STACValidator, - mlm_example: Dict[str, JSON], + mlm_example: dict[str, JSON], ) -> None: mlm_data = copy.deepcopy(mlm_example) mlm_item = pystac.Item.from_dict(mlm_data) @@ -42,7 +46,7 @@ def test_mlm_no_undefined_prefixed_field_item_properties( # undefined property anywhere in the schema mlm_data = copy.deepcopy(mlm_example) - mlm_data["properties"]["mlm:unknown"] = "random" # type: ignore + mlm_data["properties"]["mlm:unknown"] = "random" with pytest.raises(pystac.errors.STACValidationError) as exc: mlm_item = pystac.Item.from_dict(mlm_data) pystac.validation.validate(mlm_item, validator=mlm_validator) @@ -53,12 +57,13 @@ def test_mlm_no_undefined_prefixed_field_item_properties( # defined property only allowed at the Asset level mlm_data = copy.deepcopy(mlm_example) - mlm_data["properties"]["mlm:artifact_type"] = "torch.save" # type: ignore + mlm_data["properties"]["mlm:artifact_type"] = "torch.save" with pytest.raises(pystac.errors.STACValidationError) as exc: mlm_item = pystac.Item.from_dict(mlm_data) pystac.validation.validate(mlm_item, validator=mlm_validator) - assert "mlm:artifact_type" in str(exc.value.source[0].validator_value) - assert exc.value.source[0].schema["description"] == "Fields that are disallowed under the Item properties." + errors = cast(list[ValidationError], exc.value.source) + assert "mlm:artifact_type" in str(errors[0].validator_value) + assert errors[0].schema["description"] == "Fields that are disallowed under the Item properties." @pytest.mark.parametrize( @@ -85,17 +90,17 @@ def test_mlm_no_undefined_prefixed_field_asset_properties( mlm_data = copy.deepcopy(mlm_example) mlm_item = pystac.Item.from_dict(mlm_data) pystac.validation.validate(mlm_item, validator=mlm_validator) # ensure original is valid - assert mlm_data["assets"]["weights"] # type: ignore + assert mlm_data["assets"]["weights"] mlm_data = copy.deepcopy(mlm_example) - mlm_data["assets"]["weights"][test_field] = test_value # type: ignore + mlm_data["assets"]["weights"][test_field] = test_value with pytest.raises(pystac.errors.STACValidationError) as exc: mlm_item = pystac.Item.from_dict(mlm_data) pystac.validation.validate(mlm_item, validator=mlm_validator) - assert len(exc.value.source) == 1 # type: ignore - schema_error = exc.value.source[0] # type: ignore - assert test_field in schema_error.instance - assert schema_error.schema["description"] in [ + assert len(exc.value.source) == 1 + errors = cast(list[ValidationError], exc.value.source) + assert test_field in errors[0].instance + assert errors[0].schema["description"] in [ "All possible MLM fields regardless of the level they apply (Collection, Item, Asset, Link).", "Fields that are disallowed under the Asset properties." ] @@ -112,7 +117,7 @@ def test_mlm_allowed_field_asset_properties_override( ) -> None: # defined property allowed both at the Item at the Asset level mlm_data = copy.deepcopy(mlm_example) - mlm_data["assets"]["weights"]["mlm:accelerator"] = "cuda" # type: ignore + mlm_data["assets"]["weights"]["mlm:accelerator"] = "cuda" mlm_item = pystac.Item.from_dict(mlm_data) pystac.validation.validate(mlm_item, validator=mlm_validator) @@ -130,7 +135,7 @@ def test_mlm_missing_bands_invalid_if_mlm_input_lists_bands( pystac.validation.validate(mlm_item, validator=mlm_validator) # ensure original is valid mlm_bands_bad_data = copy.deepcopy(mlm_example) - mlm_bands_bad_data["assets"]["weights"].pop("raster:bands") # type: ignore # no 'None' to raise in case modified + mlm_bands_bad_data["assets"]["weights"].pop("raster:bands") # no 'None' to raise in case missing with pytest.raises(pystac.errors.STACValidationError): mlm_bands_bad_item = pystac.Item.from_dict(mlm_bands_bad_data) pystac.validation.validate(mlm_bands_bad_item, validator=mlm_validator) @@ -149,7 +154,7 @@ def test_mlm_eo_bands_invalid_only_in_item_properties( pystac.validation.validate(mlm_item, validator=mlm_validator) # ensure original is valid mlm_eo_bands_bad_data = copy.deepcopy(mlm_example) - mlm_eo_bands_bad_data["assets"]["weights"].pop("eo:bands") # type: ignore # no 'None' to raise in case modified + mlm_eo_bands_bad_data["assets"]["weights"].pop("eo:bands") # no 'None' to raise in case missing with pytest.raises(pystac.errors.STACValidationError): mlm_eo_bands_bad_item = pystac.Item.from_dict(mlm_eo_bands_bad_data) pystac.validation.validate(mlm_eo_bands_bad_item, validator=mlm_validator) @@ -165,12 +170,12 @@ def test_mlm_no_input_allowed_but_explicit_empty_array_required( mlm_example: Dict[str, JSON], ) -> None: mlm_data = copy.deepcopy(mlm_example) - mlm_data["properties"]["mlm:input"] = [] # type: ignore + mlm_data["properties"]["mlm:input"] = [] mlm_item = pystac.Item.from_dict(mlm_data) pystac.validation.validate(mlm_item, validator=mlm_validator) with pytest.raises(pystac.errors.STACValidationError): - mlm_data["properties"].pop("mlm:input") # type: ignore # no 'None' to raise in case modified + mlm_data["properties"].pop("mlm:input") # no 'None' to raise in case missing mlm_item = pystac.Item.from_dict(mlm_data) pystac.validation.validate(mlm_item, validator=mlm_validator) @@ -230,13 +235,13 @@ def test_mlm_other_non_mlm_assets_allowed( mlm_item = pystac.Item.from_dict(mlm_data) pystac.validation.validate(mlm_item, validator=mlm_validator) # self-check valid beforehand - mlm_data["assets"]["sample"] = { # type: ignore + mlm_data["assets"]["sample"] = { "type": "image/jpeg", "href": "https://example.com/sample/output.jpg", "roles": ["preview"], "title": "Model Output Predictions Sample", } - mlm_data["assets"]["model-cart"] = { # type: ignore + mlm_data["assets"]["model-cart"] = { "type": "text/markdown", "href": "https://example.com/sample/model.md", "roles": ["metadata"], @@ -285,7 +290,8 @@ def test_mlm_at_least_one_asset_model( else: with pytest.raises(pystac.errors.STACValidationError) as exc: pystac.validation.validate(mlm_item, validator=mlm_validator) - assert exc.value.source[0].schema["$comment"] in [ # type: ignore + errors = cast(list[ValidationError], exc.value.source) + assert errors[0].schema["$comment"] in [ "At least one Asset must provide the model definition indicated by the 'mlm:model' role.", "Used to check the artifact type property that is required by a Model Asset annotated by 'mlm:model' role." ]