Skip to content

Commit 97956b8

Browse files
gadomskijsignell
andauthored
feat: permissive extent deserialization (#1559)
* feat: permissive extent deserialization * chore: update changelog * Update tests/test_collection.py Co-authored-by: Julia Signell <jsignell@gmail.com> * fix: tests --------- Co-authored-by: Julia Signell <jsignell@gmail.com>
1 parent fa00c5d commit 97956b8

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
media type value for these types and new media types COPC and VND_PMTILES
99
([#1554](https://github.com/stac-utils/pystac/pull/1554))
1010

11+
### Fixed
12+
13+
- More permissive collection extent deserialization ([#1559](https://github.com/stac-utils/pystac/pull/1559))
14+
1115
## [v1.13.0] - 2025-04-15
1216

1317
### Added

pystac/collection.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import warnings
4-
from collections.abc import Iterable
4+
from collections.abc import Iterable, Sequence
55
from copy import deepcopy
66
from datetime import datetime, timezone
77
from typing import (
@@ -71,7 +71,7 @@ class SpatialExtent:
7171

7272
def __init__(
7373
self,
74-
bboxes: Bboxes | list[float | int],
74+
bboxes: Bboxes | Sequence[float | int],
7575
extra_fields: dict[str, Any] | None = None,
7676
) -> None:
7777
if not isinstance(bboxes, list):
@@ -199,7 +199,7 @@ class TemporalExtent:
199199

200200
def __init__(
201201
self,
202-
intervals: TemporalIntervals | list[datetime | None],
202+
intervals: TemporalIntervals | Sequence[datetime | None],
203203
extra_fields: dict[str, Any] | None = None,
204204
):
205205
if not isinstance(intervals, list):
@@ -652,7 +652,17 @@ def from_dict(
652652
id = d.pop("id")
653653
description = d.pop("description")
654654
license = d.pop("license")
655-
extent = Extent.from_dict(d.pop("extent"))
655+
if extent_dict := d.pop("extent", None):
656+
extent = Extent.from_dict(extent_dict)
657+
else:
658+
warnings.warn(
659+
"Collection is missing extent, setting default spatial and "
660+
"temporal extents"
661+
)
662+
extent = Extent(
663+
spatial=SpatialExtent([-90, -180, 90, 180]),
664+
temporal=TemporalExtent([None, None]),
665+
)
656666
title = d.pop("title", None)
657667
stac_extensions = d.pop("stac_extensions", None)
658668
keywords = d.pop("keywords", None)

tests/test_collection.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ def test_temporal_extent_allows_single_interval() -> None:
400400
end_datetime = str_to_datetime("2022-01-31T23:59:59Z")
401401

402402
interval = [start_datetime, end_datetime]
403-
temporal_extent = TemporalExtent(intervals=interval) # type: ignore
403+
temporal_extent = TemporalExtent(intervals=interval)
404404

405405
assert temporal_extent.intervals == [interval]
406406

@@ -820,3 +820,25 @@ def test_from_items_with_providers(sample_item_collection: ItemCollection) -> No
820820

821821
provider = collection.providers[0]
822822
assert provider and provider.name == "pystac"
823+
824+
825+
def test_from_dict_null_extent(collection: Collection) -> None:
826+
# https://github.com/stac-utils/pystac/issues/1558
827+
# https://github.com/EOPF-Sample-Service/eopf-stac/issues/18
828+
d = collection.to_dict()
829+
d["extent"] = None
830+
with pytest.warns(UserWarning):
831+
c = Collection.from_dict(d)
832+
833+
assert c.extent.spatial.to_dict()["bbox"] == [[-90, -180, 90, 180]]
834+
assert c.extent.temporal.to_dict()["interval"] == [[None, None]]
835+
836+
837+
def test_from_dict_missing_extent(collection: Collection) -> None:
838+
d = collection.to_dict()
839+
del d["extent"]
840+
with pytest.warns(UserWarning):
841+
c = Collection.from_dict(d)
842+
843+
assert c.extent.spatial.to_dict()["bbox"] == [[-90, -180, 90, 180]]
844+
assert c.extent.temporal.to_dict()["interval"] == [[None, None]]

0 commit comments

Comments
 (0)