Skip to content

feat: Print human readable type name in error messages #498

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

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
2 changes: 1 addition & 1 deletion pystac/extensions/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__}'"
)


Expand Down
2 changes: 1 addition & 1 deletion pystac/extensions/eo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pystac/extensions/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__}'"
)


Expand Down
2 changes: 1 addition & 1 deletion pystac/extensions/item_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__}'"
)


Expand Down
2 changes: 1 addition & 1 deletion pystac/extensions/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pystac/extensions/pointcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__}'"
)


Expand Down
2 changes: 1 addition & 1 deletion pystac/extensions/projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pystac/extensions/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pystac/extensions/sat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__}'"
)


Expand Down
2 changes: 1 addition & 1 deletion pystac/extensions/scientific.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pystac/extensions/timestamps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__}'"
)


Expand Down
2 changes: 1 addition & 1 deletion pystac/extensions/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__}'"
)


Expand Down
2 changes: 1 addition & 1 deletion tests/extensions/test_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/extensions/test_datacube.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
import pystac
from pystac import ExtensionTypeError
from pystac.extensions.datacube import DatacubeExtension

from tests.utils import TestCases
Expand Down Expand Up @@ -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(),
)
12 changes: 11 additions & 1 deletion tests/extensions/test_eo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
)
11 changes: 11 additions & 0 deletions tests/extensions/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(),
)
12 changes: 11 additions & 1 deletion tests/extensions/test_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
)
8 changes: 6 additions & 2 deletions tests/extensions/test_pointcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/extensions/test_projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
12 changes: 11 additions & 1 deletion tests/extensions/test_raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
)
11 changes: 11 additions & 0 deletions tests/extensions/test_sat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
)
12 changes: 12 additions & 0 deletions tests/extensions/test_scientific.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions tests/extensions/test_timestamps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
)
13 changes: 13 additions & 0 deletions tests/extensions/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"

Expand Down
11 changes: 11 additions & 0 deletions tests/serialization/test_migrate.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pystac import ExtensionTypeError
from pystac.extensions.item_assets import ItemAssetsExtension
from pystac.extensions.view import ViewExtension
import unittest
Expand Down Expand Up @@ -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(),
)