1212 STACValidationError ,
1313)
1414
15- from typing import Any , Dict , Optional , Union
15+ from typing import Any , Dict , Optional
1616from pystac .version import (
1717 __version__ ,
1818 get_stac_version ,
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
9699def 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
131126def 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 )
0 commit comments