Skip to content

Commit 431b5a7

Browse files
committed
feat: Print human readable type name in error messages
Based on <https://stackoverflow.com/a/13477954/96588>. Also add tests to verify the exception being thrown.
1 parent 53726f7 commit 431b5a7

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

pystac/extensions/sar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ def ext(cls, obj: T, add_if_missing: bool = False) -> "SarExtension[T]":
318318
return cast(SarExtension[T], AssetSarExtension(obj))
319319
else:
320320
raise pystac.ExtensionTypeError(
321-
f"SAR extension does not apply to type {type(obj)}"
321+
f"SAR extension does not apply to type '{type(obj).__name__}'"
322322
)
323323

324324

pystac/extensions/view.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def ext(cls, obj: T, add_if_missing: bool = False) -> "ViewExtension[T]":
167167
return cast(ViewExtension[T], AssetViewExtension(obj))
168168
else:
169169
raise pystac.ExtensionTypeError(
170-
f"View extension does not apply to type {type(obj)}"
170+
f"View extension does not apply to type '{type(obj).__name__}'"
171171
)
172172

173173
@staticmethod

tests/extensions/test_sar.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from string import ascii_letters
99

1010
import pystac
11+
from pystac import ExtensionTypeError
1112
from pystac.extensions import sar
1213
from pystac.extensions.sar import SarExtension
1314
from tests.utils import TestCases
@@ -193,6 +194,16 @@ def test_should_return_none_when_observation_direction_is_not_set(self) -> None:
193194
)
194195
self.assertIsNone(extension.observation_direction)
195196

197+
def test_should_raise_exception_when_passing_invalid_extension_object(
198+
self,
199+
) -> None:
200+
self.assertRaisesRegex(
201+
ExtensionTypeError,
202+
r"^SAR extension does not apply to type 'object'$",
203+
SarExtension.ext,
204+
object(),
205+
)
206+
196207

197208
if __name__ == "__main__":
198209
unittest.main()

tests/extensions/test_view.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import json
2+
3+
from pystac import ExtensionTypeError
24
from pystac.collection import Collection
35
import unittest
46

@@ -257,6 +259,16 @@ def test_asset_ext_add_to(self) -> None:
257259

258260
self.assertIn(ViewExtension.get_schema_uri(), item.stac_extensions)
259261

262+
def test_should_raise_exception_when_passing_invalid_extension_object(
263+
self,
264+
) -> None:
265+
self.assertRaisesRegex(
266+
ExtensionTypeError,
267+
r"^View extension does not apply to type 'object'$",
268+
ViewExtension.ext,
269+
object(),
270+
)
271+
260272

261273
class ViewSummariestest(unittest.TestCase):
262274
def setUp(self) -> None:

0 commit comments

Comments
 (0)