diff --git a/pystac/extensions/datacube.py b/pystac/extensions/datacube.py index e43b8de4c..f470d0145 100644 --- a/pystac/extensions/datacube.py +++ b/pystac/extensions/datacube.py @@ -356,7 +356,7 @@ def ext(cls, obj: T, add_if_missing: bool = False) -> "DatacubeExtension[T]": return cast(DatacubeExtension[T], AssetDatacubeExtension(obj)) else: raise pystac.ExtensionTypeError( - f"Datacube extension does not apply to type {type(obj)}" + f"Datacube extension does not apply to type '{type(obj).__name__}'" ) diff --git a/pystac/extensions/eo.py b/pystac/extensions/eo.py index 576d1c6ba..e0a69d849 100644 --- a/pystac/extensions/eo.py +++ b/pystac/extensions/eo.py @@ -370,7 +370,7 @@ def ext(cls, obj: T, add_if_missing: bool = False) -> "EOExtension[T]": return cast(EOExtension[T], AssetEOExtension(obj)) else: raise pystac.ExtensionTypeError( - f"EO extension does not apply to type {type(obj)}" + f"EO extension does not apply to type '{type(obj).__name__}'" ) @staticmethod diff --git a/pystac/extensions/file.py b/pystac/extensions/file.py index deb1df4b0..6d8a76830 100644 --- a/pystac/extensions/file.py +++ b/pystac/extensions/file.py @@ -206,7 +206,7 @@ def ext(cls, obj: pystac.Asset, add_if_missing: bool = False) -> "FileExtension" return cls(obj) else: raise pystac.ExtensionTypeError( - f"File Info extension does not apply to type {type(obj)}" + f"File Info extension does not apply to type '{type(obj).__name__}'" ) diff --git a/pystac/extensions/item_assets.py b/pystac/extensions/item_assets.py index f85451c0f..dea93ece8 100644 --- a/pystac/extensions/item_assets.py +++ b/pystac/extensions/item_assets.py @@ -126,7 +126,7 @@ def ext( return cls(obj) else: raise pystac.ExtensionTypeError( - f"Item Assets extension does not apply to type {type(obj)}" + f"Item Assets extension does not apply to type '{type(obj).__name__}'" ) diff --git a/pystac/extensions/label.py b/pystac/extensions/label.py index 9600299d3..5ad899dff 100644 --- a/pystac/extensions/label.py +++ b/pystac/extensions/label.py @@ -710,7 +710,7 @@ def ext(cls, obj: pystac.Item, add_if_missing: bool = False) -> "LabelExtension" return cls(obj) else: raise pystac.ExtensionTypeError( - f"Label extension does not apply to type {type(obj)}" + f"Label extension does not apply to type '{type(obj).__name__}'" ) @staticmethod diff --git a/pystac/extensions/pointcloud.py b/pystac/extensions/pointcloud.py index 804fed2c2..092a2ea22 100644 --- a/pystac/extensions/pointcloud.py +++ b/pystac/extensions/pointcloud.py @@ -536,7 +536,7 @@ def ext(cls, obj: T, add_if_missing: bool = False) -> "PointcloudExtension[T]": return cast(PointcloudExtension[T], AssetPointcloudExtension(obj)) else: raise pystac.ExtensionTypeError( - f"Pointcloud extension does not apply to type {type(obj)}" + f"Pointcloud extension does not apply to type '{type(obj).__name__}'" ) diff --git a/pystac/extensions/projection.py b/pystac/extensions/projection.py index 1c68c9fa0..a615d6500 100644 --- a/pystac/extensions/projection.py +++ b/pystac/extensions/projection.py @@ -261,7 +261,7 @@ def ext(cls, obj: T, add_if_missing: bool = False) -> "ProjectionExtension[T]": return cast(ProjectionExtension[T], AssetProjectionExtension(obj)) else: raise pystac.ExtensionTypeError( - f"Projection extension does not apply to type {type(obj)}" + f"Projection extension does not apply to type '{type(obj).__name__}'" ) @staticmethod diff --git a/pystac/extensions/raster.py b/pystac/extensions/raster.py index 551474b17..55e8d475c 100644 --- a/pystac/extensions/raster.py +++ b/pystac/extensions/raster.py @@ -706,7 +706,7 @@ def ext(cls, obj: pystac.Asset, add_if_missing: bool = False) -> "RasterExtensio return cls(obj) else: raise pystac.ExtensionTypeError( - f"Raster extension does not apply to type {type(obj)}" + f"Raster extension does not apply to type '{type(obj).__name__}'" ) @staticmethod diff --git a/pystac/extensions/sat.py b/pystac/extensions/sat.py index a84cb0b14..c775f56c2 100644 --- a/pystac/extensions/sat.py +++ b/pystac/extensions/sat.py @@ -109,7 +109,7 @@ def ext(cls, obj: T, add_if_missing: bool = False) -> "SatExtension[T]": return cast(SatExtension[T], AssetSatExtension(obj)) else: raise pystac.ExtensionTypeError( - f"File extension does not apply to type {type(obj)}" + f"Satellite extension does not apply to type '{type(obj).__name__}'" ) diff --git a/pystac/extensions/scientific.py b/pystac/extensions/scientific.py index fff9f3ce1..f27cea902 100644 --- a/pystac/extensions/scientific.py +++ b/pystac/extensions/scientific.py @@ -248,7 +248,7 @@ def ext(cls, obj: T, add_if_missing: bool = False) -> "ScientificExtension[T]": return cast(ScientificExtension[T], ItemScientificExtension(obj)) else: raise pystac.ExtensionTypeError( - f"File extension does not apply to type {type(obj)}" + f"Scientific extension does not apply to type '{type(obj).__name__}'" ) @staticmethod diff --git a/pystac/extensions/timestamps.py b/pystac/extensions/timestamps.py index f4de30851..6eef84969 100644 --- a/pystac/extensions/timestamps.py +++ b/pystac/extensions/timestamps.py @@ -130,7 +130,7 @@ def ext(cls, obj: T, add_if_missing: bool = False) -> "TimestampsExtension[T]": return cast(TimestampsExtension[T], AssetTimestampsExtension(obj)) else: raise pystac.ExtensionTypeError( - f"File extension does not apply to type {type(obj)}" + f"Timestamps extension does not apply to type '{type(obj).__name__}'" ) diff --git a/pystac/extensions/version.py b/pystac/extensions/version.py index ac01dbdc2..00dc671d1 100644 --- a/pystac/extensions/version.py +++ b/pystac/extensions/version.py @@ -219,7 +219,7 @@ def ext(cls, obj: T, add_if_missing: bool = False) -> "VersionExtension[T]": return cast(VersionExtension[T], ItemVersionExtension(obj)) else: raise pystac.ExtensionTypeError( - f"File extension does not apply to type {type(obj)}" + f"Version extension does not apply to type '{type(obj).__name__}'" ) diff --git a/tests/extensions/test_custom.py b/tests/extensions/test_custom.py index 4c9d9c889..4e22b34a3 100644 --- a/tests/extensions/test_custom.py +++ b/tests/extensions/test_custom.py @@ -62,7 +62,7 @@ def custom_ext(obj: T) -> "CustomExtension[T]": return cast(CustomExtension[T], CatalogCustomExtension(obj)) raise pystac.ExtensionTypeError( - f"Custom extension does not apply to {type(obj)}" + f"Custom extension does not apply to {type(obj).__name__}" ) @staticmethod diff --git a/tests/extensions/test_datacube.py b/tests/extensions/test_datacube.py index 251bd7f83..68ea84e43 100644 --- a/tests/extensions/test_datacube.py +++ b/tests/extensions/test_datacube.py @@ -1,5 +1,6 @@ import unittest import pystac +from pystac import ExtensionTypeError from pystac.extensions.datacube import DatacubeExtension from tests.utils import TestCases @@ -50,3 +51,13 @@ def test_asset_ext_add_to(self) -> None: _ = DatacubeExtension.ext(asset, add_if_missing=True) self.assertIn(DatacubeExtension.get_schema_uri(), item.stac_extensions) + + def test_should_raise_exception_when_passing_invalid_extension_object( + self, + ) -> None: + self.assertRaisesRegex( + ExtensionTypeError, + r"^Datacube extension does not apply to type 'object'$", + DatacubeExtension.ext, + object(), + ) diff --git a/tests/extensions/test_eo.py b/tests/extensions/test_eo.py index 6cf3b0af8..c1ba016cb 100644 --- a/tests/extensions/test_eo.py +++ b/tests/extensions/test_eo.py @@ -2,7 +2,7 @@ import unittest import pystac -from pystac import Item +from pystac import ExtensionTypeError, Item from pystac.summaries import RangeSummary from pystac.utils import get_opt from pystac.extensions.eo import EOExtension, Band @@ -298,3 +298,13 @@ def test_asset_ext_add_to(self) -> None: _ = EOExtension.ext(asset, add_if_missing=True) self.assertIn(EOExtension.get_schema_uri(), item.stac_extensions) + + def test_should_raise_exception_when_passing_invalid_extension_object( + self, + ) -> None: + self.assertRaisesRegex( + ExtensionTypeError, + r"^EO extension does not apply to type 'object'$", + EOExtension.ext, + object(), + ) diff --git a/tests/extensions/test_file.py b/tests/extensions/test_file.py index 35164b536..e73f83806 100644 --- a/tests/extensions/test_file.py +++ b/tests/extensions/test_file.py @@ -2,6 +2,7 @@ import unittest import pystac +from pystac import ExtensionTypeError from tests.utils import TestCases, assert_to_from_dict from pystac.extensions.file import FileExtension, ByteOrder, MappingObject @@ -218,3 +219,13 @@ def test_ext_add_to(self) -> None: _ = FileExtension.ext(asset, add_if_missing=True) self.assertIn(FileExtension.get_schema_uri(), item.stac_extensions) + + def test_should_raise_exception_when_passing_invalid_extension_object( + self, + ) -> None: + self.assertRaisesRegex( + ExtensionTypeError, + r"^File Info extension does not apply to type 'object'$", + FileExtension.ext, + object(), + ) diff --git a/tests/extensions/test_label.py b/tests/extensions/test_label.py index f46afb7c6..e7d20cdbe 100644 --- a/tests/extensions/test_label.py +++ b/tests/extensions/test_label.py @@ -5,7 +5,7 @@ from typing import List, Union import pystac -from pystac import Catalog, Collection, Item, CatalogType +from pystac import Catalog, Collection, ExtensionTypeError, Item, CatalogType from pystac.extensions.label import ( LabelExtension, LabelClasses, @@ -540,3 +540,13 @@ def test_label_methods_summary(self) -> None: label_methods_summary_ext = label_ext_summaries.label_methods assert label_methods_summary_ext is not None self.assertListEqual(label_methods, label_methods_summary_ext) + + def test_should_raise_exception_when_passing_invalid_extension_object( + self, + ) -> None: + self.assertRaisesRegex( + ExtensionTypeError, + r"^Label extension does not apply to type 'object'$", + LabelExtension.ext, + object(), + ) diff --git a/tests/extensions/test_pointcloud.py b/tests/extensions/test_pointcloud.py index 1a71cd057..863f4c4f4 100644 --- a/tests/extensions/test_pointcloud.py +++ b/tests/extensions/test_pointcloud.py @@ -291,8 +291,12 @@ def test_ext(self) -> None: class RandomObject: pass - with self.assertRaises(ExtensionTypeError): - PointcloudExtension.ext(RandomObject()) # type: ignore + self.assertRaisesRegex( + ExtensionTypeError, + r"^Pointcloud extension does not apply to type 'RandomObject'$", + PointcloudExtension.ext, + RandomObject(), + ) def test_extension_not_implemented(self) -> None: # Should raise exception if Item does not include extension URI diff --git a/tests/extensions/test_projection.py b/tests/extensions/test_projection.py index e28dc2e37..731441414 100644 --- a/tests/extensions/test_projection.py +++ b/tests/extensions/test_projection.py @@ -4,6 +4,7 @@ from copy import deepcopy import pystac +from pystac import ExtensionTypeError from pystac.extensions.projection import ProjectionExtension from pystac.utils import get_opt from tests.utils import TestCases, assert_to_from_dict @@ -443,6 +444,16 @@ def test_asset_ext_add_to(self) -> None: self.assertIn(ProjectionExtension.get_schema_uri(), item.stac_extensions) + def test_should_raise_exception_when_passing_invalid_extension_object( + self, + ) -> None: + self.assertRaisesRegex( + ExtensionTypeError, + r"^Projection extension does not apply to type 'object'$", + ProjectionExtension.ext, + object(), + ) + class ProjectionSummariesTest(unittest.TestCase): def setUp(self) -> None: diff --git a/tests/extensions/test_raster.py b/tests/extensions/test_raster.py index 825a6b86f..b2df3ffdb 100644 --- a/tests/extensions/test_raster.py +++ b/tests/extensions/test_raster.py @@ -2,7 +2,7 @@ import unittest import pystac -from pystac import Item +from pystac import ExtensionTypeError, Item from pystac.utils import get_opt from pystac.extensions.raster import ( Histogram, @@ -222,3 +222,13 @@ def test_ext_add_to(self) -> None: _ = RasterExtension.ext(asset, add_if_missing=True) self.assertIn(RasterExtension.get_schema_uri(), item.stac_extensions) + + def test_should_raise_exception_when_passing_invalid_extension_object( + self, + ) -> None: + self.assertRaisesRegex( + ExtensionTypeError, + r"^Raster extension does not apply to type 'object'$", + RasterExtension.ext, + object(), + ) diff --git a/tests/extensions/test_sat.py b/tests/extensions/test_sat.py index d55fd3e9e..686ec4e4d 100644 --- a/tests/extensions/test_sat.py +++ b/tests/extensions/test_sat.py @@ -5,6 +5,7 @@ import unittest import pystac +from pystac import ExtensionTypeError from pystac.extensions import sat from pystac.extensions.sat import SatExtension from tests.utils import TestCases @@ -160,3 +161,13 @@ def test_asset_ext_add_to(self) -> None: _ = SatExtension.ext(asset, add_if_missing=True) self.assertIn(SatExtension.get_schema_uri(), item.stac_extensions) + + def test_should_raise_exception_when_passing_invalid_extension_object( + self, + ) -> None: + self.assertRaisesRegex( + ExtensionTypeError, + r"^Satellite extension does not apply to type 'object'$", + SatExtension.ext, + object(), + ) diff --git a/tests/extensions/test_scientific.py b/tests/extensions/test_scientific.py index 99154c199..2897f4a21 100644 --- a/tests/extensions/test_scientific.py +++ b/tests/extensions/test_scientific.py @@ -1,6 +1,8 @@ """Tests for pystac.tests.extensions.scientific.""" import datetime + +from pystac import ExtensionTypeError from pystac.link import Link from pystac.collection import Summaries import unittest @@ -406,6 +408,16 @@ def test_ext_add_to(self) -> None: self.assertIn(ScientificExtension.get_schema_uri(), collection.stac_extensions) + def test_should_raise_exception_when_passing_invalid_extension_object( + self, + ) -> None: + self.assertRaisesRegex( + ExtensionTypeError, + r"^Scientific extension does not apply to type 'object'$", + ScientificExtension.ext, + object(), + ) + class SummariesScientificTest(unittest.TestCase): def setUp(self) -> None: diff --git a/tests/extensions/test_timestamps.py b/tests/extensions/test_timestamps.py index 6f1904965..5884a23b7 100644 --- a/tests/extensions/test_timestamps.py +++ b/tests/extensions/test_timestamps.py @@ -3,6 +3,7 @@ from datetime import datetime import pystac +from pystac import ExtensionTypeError from pystac.extensions.timestamps import TimestampsExtension from pystac.utils import get_opt, str_to_datetime, datetime_to_str from tests.utils import TestCases, assert_to_from_dict @@ -222,3 +223,13 @@ def test_asset_ext_add_to(self) -> None: _ = TimestampsExtension.ext(asset, add_if_missing=True) self.assertIn(TimestampsExtension.get_schema_uri(), item.stac_extensions) + + def test_should_raise_exception_when_passing_invalid_extension_object( + self, + ) -> None: + self.assertRaisesRegex( + ExtensionTypeError, + r"^Timestamps extension does not apply to type 'object'$", + TimestampsExtension.ext, + object(), + ) diff --git a/tests/extensions/test_version.py b/tests/extensions/test_version.py index c3aabd860..6a48d1698 100644 --- a/tests/extensions/test_version.py +++ b/tests/extensions/test_version.py @@ -5,6 +5,7 @@ from typing import List, Optional import pystac +from pystac import ExtensionTypeError from pystac.extensions import version from pystac.extensions.version import VersionExtension, VersionRelType from tests.utils import TestCases @@ -27,6 +28,18 @@ def make_item(year: int) -> pystac.Item: return item +class VersionExtensionTest(unittest.TestCase): + def test_should_raise_exception_when_passing_invalid_extension_object( + self, + ) -> None: + self.assertRaisesRegex( + ExtensionTypeError, + r"^Version extension does not apply to type 'object'$", + VersionExtension.ext, + object(), + ) + + class ItemVersionExtensionTest(unittest.TestCase): version: str = "1.2.3" diff --git a/tests/serialization/test_migrate.py b/tests/serialization/test_migrate.py index ec7a47951..59254028e 100644 --- a/tests/serialization/test_migrate.py +++ b/tests/serialization/test_migrate.py @@ -1,3 +1,4 @@ +from pystac import ExtensionTypeError from pystac.extensions.item_assets import ItemAssetsExtension from pystac.extensions.view import ViewExtension import unittest @@ -96,3 +97,13 @@ def test_migrates_pre_1_0_0_rc1_stats_summary(self) -> None: ) self.assertEqual(datetime_summary.minimum, "2015-06-23T00:00:00Z") self.assertEqual(datetime_summary.maximum, "2019-07-10T13:44:56Z") + + def test_should_raise_exception_when_passing_invalid_extension_object( + self, + ) -> None: + self.assertRaisesRegex( + ExtensionTypeError, + r"^Item Assets extension does not apply to type 'object'$", + ItemAssetsExtension.ext, + object(), + )