Skip to content

Commit 83196bb

Browse files
committed
Implement #370 for Label Extension
1 parent 8e8cb56 commit 83196bb

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

pystac/extensions/label.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,13 @@ def ext(cls, obj: pystac.Item) -> "LabelExtension":
645645
646646
This extension can be applied to instances of :class:`~pystac.Item`.
647647
"""
648-
return cls(obj)
648+
if isinstance(obj, pystac.Item):
649+
cls.validate_has_extension(obj)
650+
return cls(obj)
651+
else:
652+
raise pystac.ExtensionTypeError(
653+
f"Label extension does not apply to type {type(obj)}"
654+
)
649655

650656

651657
class LabelExtensionHooks(ExtensionHooks):

tests/extensions/test_label.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,18 @@ def test_merge_label_overviews_error(self) -> None:
361361

362362
with self.assertRaises(AssertionError):
363363
_ = overview_1.merge_counts(overview_2)
364+
365+
def test_extension_type_error(self) -> None:
366+
collection = pystac.Collection.from_file(
367+
TestCases.get_path("data-files/collections/with-assets.json")
368+
)
369+
with self.assertRaises(pystac.ExtensionTypeError):
370+
_ = LabelExtension.ext(collection) # type: ignore
371+
372+
def test_extension_not_implemented(self) -> None:
373+
# Should raise exception if Item does not include extension URI
374+
item = pystac.Item.from_file(self.label_example_1_uri)
375+
item.stac_extensions.remove(LabelExtension.get_schema_uri())
376+
377+
with self.assertRaises(pystac.ExtensionNotImplemented):
378+
_ = LabelExtension.ext(item)

0 commit comments

Comments
 (0)