Skip to content

Commit a0651ef

Browse files
committed
feat: permissive extent deserialization
1 parent fa00c5d commit a0651ef

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

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: 17 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,19 @@ 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+
Collection.from_dict(d)
832+
833+
834+
def test_from_dict_missing_extent(collection: Collection) -> None:
835+
d = collection.to_dict()
836+
del d["extent"]
837+
with pytest.warns(UserWarning):
838+
Collection.from_dict(d)

0 commit comments

Comments
 (0)