Skip to content

Commit 97354cd

Browse files
committed
Default to not cloning Items on instantiation
1 parent 1ead0d5 commit 97354cd

File tree

2 files changed

+14
-26
lines changed

2 files changed

+14
-26
lines changed

pystac/item_collection.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ class ItemCollection(Collection[pystac.Item]):
3232
:class:`~ItemCollection`.
3333
clone_items : Optional flag indicating whether :class:`~pystac.Item` instances
3434
should be cloned before storing in the :class:`~ItemCollection`. Setting to
35-
``False`` will result in faster instantiation, but any changes made to
36-
:class:`~pystac.Item` instances in the :class:`~ItemCollection` will also
37-
mutate the original :class:`~pystac.Item`. Defaults to ``True``.
35+
``True`` ensures that changes made to :class:`~pystac.Item` instances in
36+
the :class:`~ItemCollection` will not mutate the original ``Item``, but
37+
will result in slower instantiation. Defaults to ``False``.
3838
3939
Examples:
4040
@@ -50,27 +50,21 @@ class ItemCollection(Collection[pystac.Item]):
5050
>>> length: int = len(item_collection)
5151
5252
Check if an :class:`~pystac.Item` is in the :class:`~ItemCollection`. Note
53-
that you must use `clone_items=False` for this to return ``True``, since
54-
equality of PySTAC objects is currently evaluated using default object
55-
equality (i.e. ``item_1 is item_2``).
53+
that the ``clone_items`` argument must be ``False`` for this to return
54+
``True``, since equality of PySTAC objects is currently evaluated using default
55+
object equality (i.e. ``item_1 is item_2``).
5656
5757
>>> item: Item = ...
58-
>>> item_collection = ItemCollection(items=[item], clone_items=False)
58+
>>> item_collection = ItemCollection(items=[item])
5959
>>> assert item in item_collection
6060
6161
Combine :class:`~ItemCollection` instances
6262
6363
>>> item_1: Item = ...
6464
>>> item_2: Item = ...
6565
>>> item_3: Item = ...
66-
>>> item_collection_1 = ItemCollection(
67-
... items=[item_1, item_2],
68-
... clone_items=False
69-
... )
70-
>>> item_collection_2 = ItemCollection(
71-
... items=[item_2, item_3],
72-
... clone_items=False
73-
... )
66+
>>> item_collection_1 = ItemCollection(items=[item_1, item_2])
67+
>>> item_collection_2 = ItemCollection(items=[item_2, item_3])
7468
>>> combined = item_collection_1 + item_collection_2
7569
>>> assert len(combined) == 3
7670
# If an item is present in both ItemCollections it will only be added once
@@ -87,7 +81,7 @@ def __init__(
8781
self,
8882
items: Iterable[ItemLike],
8983
extra_fields: Optional[Dict[str, Any]] = None,
90-
clone_items: bool = True,
84+
clone_items: bool = False,
9185
):
9286
def map_item(item_or_dict: ItemLike) -> pystac.Item:
9387
# Converts dicts to pystac.Items and clones if necessary
@@ -152,9 +146,7 @@ def from_dict(cls, d: Dict[str, Any]) -> "ItemCollection":
152146
items = [pystac.Item.from_dict(item) for item in d.get("features", [])]
153147
extra_fields = {k: v for k, v in d.items() if k not in ("features", "type")}
154148

155-
# Since we are reading these Items from a dict within this method, there will be
156-
# no other references and we do not need to clone them.
157-
return cls(items=items, extra_fields=extra_fields, clone_items=False)
149+
return cls(items=items, extra_fields=extra_fields)
158150

159151
@classmethod
160152
def from_file(

tests/test_item_collection.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_item_collection_get_item_by_index(self) -> None:
4747

4848
def test_item_collection_contains(self) -> None:
4949
item = pystac.Item.from_file(self.SIMPLE_ITEM)
50-
item_collection = pystac.ItemCollection(items=[item], clone_items=False)
50+
item_collection = pystac.ItemCollection(items=[item])
5151

5252
self.assertIn(item, item_collection)
5353

@@ -127,12 +127,8 @@ def test_add_item_collections(self) -> None:
127127
item_2 = pystac.Item.from_file(self.EXTENDED_ITEM)
128128
item_3 = pystac.Item.from_file(self.CORE_ITEM)
129129

130-
item_collection_1 = pystac.ItemCollection(
131-
items=[item_1, item_2], clone_items=False
132-
)
133-
item_collection_2 = pystac.ItemCollection(
134-
items=[item_2, item_3], clone_items=False
135-
)
130+
item_collection_1 = pystac.ItemCollection(items=[item_1, item_2])
131+
item_collection_2 = pystac.ItemCollection(items=[item_2, item_3])
136132

137133
combined = item_collection_1 + item_collection_2
138134

0 commit comments

Comments
 (0)