Skip to content

Commit 2bb16a1

Browse files
committed
Remove ItemCollection from top-level functions
1 parent 97354cd commit 2bb16a1

File tree

5 files changed

+36
-66
lines changed

5 files changed

+36
-66
lines changed

pystac/__init__.py

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
STACValidationError,
1313
)
1414

15-
from typing import Any, Dict, Optional, Union
15+
from typing import Any, Dict, Optional
1616
from pystac.version import (
1717
__version__,
1818
get_stac_version,
@@ -72,7 +72,7 @@
7272
)
7373

7474

75-
def read_file(href: str) -> Union[STACObject, ItemCollection]:
75+
def read_file(href: str) -> STACObject:
7676
"""Reads a STAC object from a file.
7777
7878
This method will return either a Catalog, a Collection, or an Item based on what the
@@ -86,15 +86,18 @@ def read_file(href: str) -> Union[STACObject, ItemCollection]:
8686
Returns:
8787
The specific STACObject implementation class that is represented
8888
by the JSON read from the file located at HREF.
89+
90+
Raises:
91+
STACTypeError : If the file at ``href`` does not represent a valid
92+
:class:`~pystac.STACObject`. Note that an :class:`~pystac.ItemCollection` is not
93+
a :class:`~pystac.STACObject` and must be read using
94+
:meth:`ItemCollection.from_file <pystac.ItemCollection.from_file>`
8995
"""
90-
try:
91-
return STACObject.from_file(href)
92-
except STACTypeError:
93-
return ItemCollection.from_file(href)
96+
return STACObject.from_file(href)
9497

9598

9699
def write_file(
97-
obj: Union[STACObject, ItemCollection],
100+
obj: STACObject,
98101
include_self_link: bool = True,
99102
dest_href: Optional[str] = None,
100103
) -> None:
@@ -113,51 +116,44 @@ def write_file(
113116
Args:
114117
obj : The STACObject to save.
115118
include_self_link : If ``True``, include the ``"self"`` link with this object.
116-
Otherwise, leave out the self link. Ignored for :class:~ItemCollection`
117-
instances.
119+
Otherwise, leave out the self link.
118120
dest_href : Optional HREF to save the file to. If ``None``, the object will be
119-
saved to the object's ``"self"`` href (for :class:`~STACObject` sub-classes)
120-
or a :exc:`~STACError` will be raised (for :class:`~ItemCollection`
121-
instances).
121+
saved to the object's ``"self"`` href.
122122
"""
123-
if isinstance(obj, ItemCollection):
124-
if dest_href is None:
125-
raise STACError("Must provide dest_href when saving and ItemCollection.")
126-
obj.save_object(dest_href=dest_href)
127-
else:
128-
obj.save_object(include_self_link=include_self_link, dest_href=dest_href)
123+
obj.save_object(include_self_link=include_self_link, dest_href=dest_href)
129124

130125

131126
def read_dict(
132127
d: Dict[str, Any],
133128
href: Optional[str] = None,
134129
root: Optional[Catalog] = None,
135130
stac_io: Optional[StacIO] = None,
136-
) -> Union[STACObject, ItemCollection]:
131+
) -> STACObject:
137132
"""Reads a :class:`~STACObject` or :class:`~ItemCollection` from a JSON-like dict
138133
representing a serialized STAC object.
139134
140135
This method will return either a :class:`~Catalog`, :class:`~Collection`,
141-
:class`~Item`, or :class:`~ItemCollection` based on the contents of the dict.
136+
or :class`~Item` based on the contents of the dict.
142137
143138
This is a convenience method for either
144-
:meth:`stac_io.stac_object_from_dict <stac_io.stac_object_from_dict>` or
145-
:meth:`ItemCollection.from_dict <ItemCollection.from_dict>`.
139+
:meth:`stac_io.stac_object_from_dict <stac_io.stac_object_from_dict>`.
146140
147141
Args:
148142
d : The dict to parse.
149143
href : Optional href that is the file location of the object being
150-
parsed. Ignored if the dict represents an :class:`~ItemCollection`.
144+
parsed.
151145
root : Optional root of the catalog for this object.
152146
If provided, the root's resolved object cache can be used to search for
153-
previously resolved instances of the STAC object. Ignored if the dict
154-
represents an :class:`~ItemCollection`.
147+
previously resolved instances of the STAC object.
155148
stac_io: Optional :class:`~StacIO` instance to use for reading. If ``None``,
156149
the default instance will be used.
150+
151+
Raises:
152+
STACTypeError : If the ``d`` dictionary does not represent a valid
153+
:class:`~pystac.STACObject`. Note that an :class:`~pystac.ItemCollection` is not
154+
a :class:`~pystac.STACObject` and must be read using
155+
:meth:`ItemCollection.from_dict <pystac.ItemCollection.from_dict>`
157156
"""
158157
if stac_io is None:
159158
stac_io = StacIO.default()
160-
try:
161-
return stac_io.stac_object_from_dict(d, href, root)
162-
except STACTypeError:
163-
return ItemCollection.from_dict(d)
159+
return stac_io.stac_object_from_dict(d, href, root)

tests/data-files/change_stac_version.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ def migrate(path: str) -> None:
2929
)
3030
)
3131
obj = pystac.read_dict(stac_json, href=path)
32-
if not isinstance(obj, pystac.ItemCollection):
33-
migrated = obj.to_dict(include_self_link=False)
34-
with open(path, "w") as f:
35-
json.dump(migrated, f, indent=2)
32+
migrated = obj.to_dict(include_self_link=False)
33+
with open(path, "w") as f:
34+
json.dump(migrated, f, indent=2)
3635

3736

3837
if __name__ == "__main__":

tests/test_item_collection.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import json
2-
from typing import cast
32
import unittest
43
import pystac
54

@@ -80,19 +79,6 @@ def test_item_collection_from_dict(self) -> None:
8079
self.assertEqual(expected, len(item_collection.items))
8180
self.assertEqual(item_collection.extra_fields.get("custom_field"), "My value")
8281

83-
def test_item_collection_from_dict_top_level(self) -> None:
84-
features = [item.to_dict() for item in self.items]
85-
d = {
86-
"type": "FeatureCollection",
87-
"features": features,
88-
"custom_field": "My value",
89-
}
90-
item_collection = pystac.read_dict(d)
91-
item_collection = cast(pystac.ItemCollection, item_collection)
92-
expected = len(features)
93-
self.assertEqual(expected, len(item_collection.items))
94-
self.assertEqual(item_collection.extra_fields.get("custom_field"), "My value")
95-
9682
def test_clone_item_collection(self) -> None:
9783
item_collection_1 = pystac.ItemCollection.from_file(self.ITEM_COLLECTION)
9884
item_collection_2 = item_collection_1.clone()

tests/test_stac_io.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,10 @@ def test_read_write_catalog(self) -> None:
4646
pystac.write_file(catalog, dest_href=dest_href)
4747
self.assertTrue(os.path.exists(dest_href), msg="File was not written.")
4848

49-
def test_read_write_item_collection(self) -> None:
50-
item_collection = pystac.read_file(
51-
TestCases.get_path("data-files/item-collection/sample-item-collection.json")
52-
)
53-
with get_temp_dir() as tmp_dir:
54-
dest_href = os.path.join(tmp_dir, "item-collection.json")
55-
pystac.write_file(item_collection, dest_href=dest_href)
56-
self.assertTrue(os.path.exists(dest_href), msg="File was not written.")
57-
58-
def test_write_item_collection_needs_href(self) -> None:
59-
item_collection = pystac.read_file(
60-
TestCases.get_path("data-files/item-collection/sample-item-collection.json")
61-
)
62-
with self.assertRaises(pystac.STACError):
63-
pystac.write_file(item_collection)
49+
def test_read_item_collection_raises_exception(self) -> None:
50+
with self.assertRaises(pystac.STACTypeError):
51+
_ = pystac.read_file(
52+
TestCases.get_path(
53+
"data-files/item-collection/sample-item-collection.json"
54+
)
55+
)

tests/validation/test_validate.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import datetime
22
import json
33
import os
4-
from typing import Any, Dict, cast
4+
from typing import Any, Dict
55
from pystac.utils import get_opt
66
import shutil
77
import unittest
@@ -20,7 +20,6 @@ def test_validate_current_version(self) -> None:
2020
catalog = pystac.read_file(
2121
TestCases.get_path("data-files/catalogs/test-case-1/" "catalog.json")
2222
)
23-
catalog = cast(pystac.STACObject, catalog)
2423
catalog.validate()
2524

2625
collection = pystac.read_file(
@@ -30,11 +29,9 @@ def test_validate_current_version(self) -> None:
3029
"collection.json"
3130
)
3231
)
33-
collection = cast(pystac.Collection, collection)
3432
collection.validate()
3533

3634
item = pystac.read_file(TestCases.get_path("data-files/item/sample-item.json"))
37-
item = cast(pystac.Item, item)
3835
item.validate()
3936

4037
def test_validate_examples(self) -> None:

0 commit comments

Comments
 (0)