Skip to content

Commit 0448aff

Browse files
committed
Issue #573 Skip pystac 1.9.0 features on Python 3.7/3.8
1 parent 08baa86 commit 0448aff

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

openeo/metadata.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ def is_band_asset(asset: pystac.Asset) -> bool:
578578
for asset_band in asset_bands:
579579
if asset_band.name not in get_band_names(bands):
580580
bands.append(asset_band)
581-
if collection.ext.has("item_assets"):
581+
if _PYSTAC_1_9_EXTENSION_INTERFACE and collection.ext.has("item_assets"):
582582
# TODO #575 support unordered band names and avoid conversion to a list.
583583
bands = list(_StacMetadataParser().get_bands_from_item_assets(collection.ext.item_assets))
584584

@@ -597,6 +597,11 @@ def is_band_asset(asset: pystac.Asset) -> bool:
597597
return metadata
598598

599599

600+
# Sniff for PySTAC extension API since version 1.9.0 (which is not available below Python 3.9)
601+
# TODO: remove this once support for Python 3.7 and 3.8 is dropped
602+
_PYSTAC_1_9_EXTENSION_INTERFACE = hasattr(pystac.Item, "ext")
603+
604+
600605
class _StacMetadataParser:
601606
"""
602607
Helper to extract openEO metadata from STAC metadata resource
@@ -635,12 +640,13 @@ def _get_bands_from_item_asset(
635640
self, item_asset: pystac.extensions.item_assets.AssetDefinition
636641
) -> Union[List[Band], None]:
637642
"""Get bands from a STAC 'item_assets' asset definition."""
638-
if item_asset.ext.has("eo"):
643+
if _PYSTAC_1_9_EXTENSION_INTERFACE and item_asset.ext.has("eo"):
639644
if item_asset.ext.eo.bands is not None:
640645
return self.get_bands_from_eo_bands(item_asset.ext.eo.bands)
641646
elif "eo:bands" in item_asset.properties:
642647
# TODO: skip this in strict mode?
643-
_log.warning("Extracting band info from 'eo:bands' metadata, but 'eo' STAC extension was not declared.")
648+
if _PYSTAC_1_9_EXTENSION_INTERFACE:
649+
_log.warning("Extracting band info from 'eo:bands' metadata, but 'eo' STAC extension was not declared.")
644650
return self.get_bands_from_eo_bands(item_asset.properties["eo:bands"])
645651

646652
def get_bands_from_item_assets(

tests/test_metadata.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pytest
88

99
from openeo.metadata import (
10+
_PYSTAC_1_9_EXTENSION_INTERFACE,
1011
Band,
1112
BandDimension,
1213
CollectionMetadata,
@@ -842,6 +843,7 @@ def test_metadata_from_stac(tmp_path, test_stac, expected):
842843
assert metadata.band_names == expected
843844

844845

846+
@pytest.mark.skipif(not _PYSTAC_1_9_EXTENSION_INTERFACE, reason="Requires PySTAC 1.9+ extension interface")
845847
@pytest.mark.parametrize("eo_extension_is_declared", [False, True])
846848
def test_metadata_from_stac_collection_bands_from_item_assets(test_data, tmp_path, eo_extension_is_declared, caplog):
847849
stac_data = test_data.load_json("stac/collections/agera5_daily01.json")

0 commit comments

Comments
 (0)