Skip to content

Commit b0c4f25

Browse files
jsignellgadomski
authored andcommitted
feat: copy assets out of get_assets
1 parent 0f90e7a commit b0c4f25

File tree

5 files changed

+19
-18
lines changed

5 files changed

+19
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
### Changed
1212

1313
- Include a copy of the `fields.json` file (for summaries) with each distribution of PySTAC ([#1045](https://github.com/stac-utils/pystac/pull/1045))
14+
- Make Catalog, Collection `.get_assets()` return a deepcopy ([#1087](https://github.com/stac-utils/pystac/pull/1087))
1415
- Removed documentation references to `to_dict` methods returning JSON ([#1074](https://github.com/stac-utils/pystac/pull/1074))
1516

1617
### Deprecated

pystac/collection.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -725,15 +725,12 @@ def get_assets(
725725
Dict[str, Asset]: A dictionary of assets that match ``media_type``
726726
and/or ``role`` if set or else all of this collection's assets.
727727
"""
728-
if media_type is None and role is None:
729-
return dict(self.assets.items())
730-
assets = dict()
731-
for key, asset in self.assets.items():
732-
if (media_type is None or asset.media_type == media_type) and (
733-
role is None or asset.has_role(role)
734-
):
735-
assets[key] = asset
736-
return assets
728+
return {
729+
k: deepcopy(v)
730+
for k, v in self.assets.items()
731+
if (media_type is None or v.media_type == media_type)
732+
and (role is None or v.has_role(role))
733+
}
737734

738735
def add_asset(self, key: str, asset: Asset) -> None:
739736
"""Adds an Asset to this item.

pystac/item.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -256,15 +256,12 @@ def get_assets(
256256
Dict[str, Asset]: A dictionary of assets that match ``media_type``
257257
and/or ``role`` if set or else all of this item's assets.
258258
"""
259-
if media_type is None and role is None:
260-
return dict(self.assets.items())
261-
assets = dict()
262-
for key, asset in self.assets.items():
263-
if (media_type is None or asset.media_type == media_type) and (
264-
role is None or asset.has_role(role)
265-
):
266-
assets[key] = asset
267-
return assets
259+
return {
260+
k: deepcopy(v)
261+
for k, v in self.assets.items()
262+
if (media_type is None or v.media_type == media_type)
263+
and (role is None or v.has_role(role))
264+
}
268265

269266
def add_asset(self, key: str, asset: Asset) -> None:
270267
"""Adds an Asset to this item.

tests/test_collection.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,11 @@ def test_get_assets(self) -> None:
246246
self.assertCountEqual(multi_filter.keys(), ["thumbnail"])
247247

248248
no_filter = collection.get_assets()
249+
self.assertIsNot(no_filter, collection.assets)
249250
self.assertCountEqual(no_filter.keys(), ["thumbnail"])
251+
no_filter["thumbnail"].description = "foo"
252+
assert collection.assets["thumbnail"].description != "foo"
253+
250254
no_assets = collection.get_assets(media_type=pystac.MediaType.HDF)
251255
self.assertEqual(no_assets, {})
252256

tests/test_item.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ def test_get_assets(self) -> None:
179179
media_type=pystac.MediaType.PNG, role="thumbnail"
180180
)
181181
self.assertCountEqual(multi_filter.keys(), ["thumbnail"])
182+
multi_filter["thumbnail"].description = "foo"
183+
assert item.assets["thumbnail"].description != "foo"
182184

183185
no_filter = item.get_assets()
184186
self.assertCountEqual(no_filter.keys(), ["analytic", "thumbnail"])

0 commit comments

Comments
 (0)