Skip to content

Commit 3e21956

Browse files
committed
Use Union[Catalog, Collection] as child type in Catalog.
This makes it more explicit that children are either a Catalog or a Collection, even though in PySTAC a Collection inherits from a Catalog.
1 parent af2a6e4 commit 3e21956

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

pystac/catalog.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
if TYPE_CHECKING:
2929
from pystac.item import Asset as Asset_Type, Item as Item_Type
30+
from pystac.collection import Collection as Collection_Type
3031

3132

3233
class CatalogType(str, Enum):
@@ -187,7 +188,7 @@ def is_relative(self) -> bool:
187188

188189
def add_child(
189190
self,
190-
child: "Catalog",
191+
child: Union["Catalog", "Collection_Type"],
191192
title: Optional[str] = None,
192193
strategy: Optional[HrefLayoutStrategy] = None,
193194
) -> None:
@@ -220,7 +221,9 @@ def add_child(
220221

221222
self.add_link(Link.child(child, title=title))
222223

223-
def add_children(self, children: Iterable["Catalog"]) -> None:
224+
def add_children(
225+
self, children: Iterable[Union["Catalog", "Collection_Type"]]
226+
) -> None:
224227
"""Adds links to multiple :class:`~pystac.Catalog` or `~pystac.Collection` objects.
225228
This method will set each child's parent to this object, and their root to
226229
this Catalog's root.
@@ -275,7 +278,9 @@ def add_items(self, items: Iterable["Item_Type"]) -> None:
275278
for item in items:
276279
self.add_item(item)
277280

278-
def get_child(self, id: str, recursive: bool = False) -> Optional["Catalog"]:
281+
def get_child(
282+
self, id: str, recursive: bool = False
283+
) -> Optional[Union["Catalog", "Collection_Type"]]:
279284
"""Gets the child of this catalog with the given ID, if it exists.
280285
281286
Args:
@@ -285,7 +290,8 @@ def get_child(self, id: str, recursive: bool = False) -> Optional["Catalog"]:
285290
to False.
286291
287292
Return:
288-
Item or None: The item with the given ID, or None if not found.
293+
Optional Catalog or Collection: The child with the given ID,
294+
or None if not found.
289295
"""
290296
if not recursive:
291297
return next((c for c in self.get_children() if c.id == id), None)
@@ -296,14 +302,17 @@ def get_child(self, id: str, recursive: bool = False) -> Optional["Catalog"]:
296302
return child
297303
return None
298304

299-
def get_children(self) -> Iterable["Catalog"]:
305+
def get_children(self) -> Iterable[Union["Catalog", "Collection_Type"]]:
300306
"""Return all children of this catalog.
301307
302308
Return:
303-
Iterable[Catalog]: Generator of children who's parent
309+
Iterable[Catalog or Collection]: Iterable of children who's parent
304310
is this catalog.
305311
"""
306-
return map(lambda x: cast(pystac.Catalog, x), self.get_stac_objects("child"))
312+
return map(
313+
lambda x: cast(Union[pystac.Catalog, pystac.Collection], x),
314+
self.get_stac_objects("child"),
315+
)
307316

308317
def get_child_links(self) -> List[Link]:
309318
"""Return all child links of this catalog.

0 commit comments

Comments
 (0)