Skip to content

Commit 1826471

Browse files
authored
Merge pull request #468 from stac-utils/fix/rde/preserve-dict-itemcollection
Add preserve_dict parameter to ItemCollection.from_dict
2 parents d51dd28 + 2beaccd commit 1826471

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Added
66

7+
- Add a `preserve_dict` parameter to `ItemCollection.from_dict` and set it to False when using `ItemCollection.from_file`. ([#468](https://github.com/stac-utils/pystac/pull/468))
8+
79
### Changed
810

911
### Fixed

pystac/item_collection.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,26 @@ def clone(self) -> "ItemCollection":
134134
)
135135

136136
@classmethod
137-
def from_dict(cls, d: Dict[str, Any]) -> "ItemCollection":
137+
def from_dict(
138+
cls, d: Dict[str, Any], preserve_dict: bool = True
139+
) -> "ItemCollection":
138140
"""Creates a :class:`ItemCollection` instance from a dictionary.
139141
140142
Arguments:
141143
d : The dictionary from which the :class:`~ItemCollection` will be created
144+
preserve_dict: If False, the dict parameter ``d`` may be modified
145+
during this method call. Otherwise the dict is not mutated.
146+
Defaults to True, which results results in a deepcopy of the
147+
parameter. Set to False when possible to avoid the performance
148+
hit of a deepcopy.
142149
"""
143150
if not cls.is_item_collection(d):
144151
raise STACTypeError("Dict is not a valid ItemCollection")
145152

146-
items = [pystac.Item.from_dict(item) for item in d.get("features", [])]
153+
items = [
154+
pystac.Item.from_dict(item, preserve_dict=preserve_dict)
155+
for item in d.get("features", [])
156+
]
147157
extra_fields = {k: v for k, v in d.items() if k not in ("features", "type")}
148158

149159
return cls(items=items, extra_fields=extra_fields)
@@ -166,7 +176,7 @@ def from_file(
166176

167177
d = stac_io.read_json(href)
168178

169-
return cls.from_dict(d)
179+
return cls.from_dict(d, preserve_dict=False)
170180

171181
def save_object(
172182
self,

tests/test_collection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def test_assets(self) -> None:
183183
collection = pystac.Collection.from_dict(data)
184184
collection.validate()
185185

186-
def test_to_dict_preserves_dict(self) -> None:
186+
def test_from_dict_preserves_dict(self) -> None:
187187
path = TestCases.get_path("data-files/collections/with-assets.json")
188188
with open(path) as f:
189189
collection_dict = json.load(f)

tests/test_item_collection.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
from copy import deepcopy
12
import json
3+
from pystac.item_collection import ItemCollection
24
import unittest
35
import pystac
46

@@ -149,3 +151,15 @@ def test_identify_0_9_itemcollection(self) -> None:
149151
pystac.ItemCollection.is_item_collection(itemcollection_dict),
150152
msg="Did not correctly identify valid STAC 0.9 ItemCollection.",
151153
)
154+
155+
def test_from_dict_preserves_dict(self) -> None:
156+
param_dict = deepcopy(self.item_collection_dict)
157+
158+
# test that the parameter is preserved
159+
_ = ItemCollection.from_dict(param_dict)
160+
self.assertEqual(param_dict, self.item_collection_dict)
161+
162+
# assert that the parameter is not preserved with
163+
# non-default parameter
164+
_ = ItemCollection.from_dict(param_dict, preserve_dict=False)
165+
self.assertNotEqual(param_dict, self.item_collection_dict)

0 commit comments

Comments
 (0)